socket 简单c/s通信

1.serv.cpp

#include <stdio.h>  
#include <iostream>  
#include <stdlib.h>  
#include <netinet/in.h>  
#include <unistd.h>  
#include <sys/socket.h>  
#include <arpa/inet.h>  
#include <string.h>  
using namespace std;  
  
#define PORT 10001  
  
int main()  
{  
    struct sockaddr_in s_in;//server address structure  
    struct sockaddr_in c_in;//client address structure  
    int l_fd,c_fd;  
    socklen_t len;  
    char buf[100];//content buff area  
    string tem;  
    float x_value = 0.0;  
    float y_value = 0.0;  
    memset((void *)&s_in,0,sizeof(s_in));  
  
    s_in.sin_family = AF_INET;//IPV4 communication domain  
    s_in.sin_addr.s_addr = INADDR_ANY;//accept any address  
    s_in.sin_port = htons(PORT);//change port to netchar  
  
    l_fd = socket(AF_INET,SOCK_STREAM,0);//socket(int domain, int type, int protocol)  
    bind(l_fd,(struct sockaddr *)&s_in,sizeof(s_in));  
    listen(l_fd,1);//同时只能有一个连接  
  
    cout<<"begin"<<endl;  
    //while(1){  
        c_fd = accept(l_fd,(struct sockaddr *)&c_in,&len);  
        while(1)
        {  
            for(int j=0;j<100;j++)
            {  
                buf[j] = 0;  
            }  
            int n = read(c_fd,buf,100);//read the message send by client  
            if(!strcmp(buf, "q\n") || !strcmp(buf, "Q\n"))  //输入q/Q则断开连接,推出程序
            {  
                cout << "q pressed\n";  
                close(c_fd);  
                break;  
            }  
            // inet_ntop(AF_INET,&c_in.sin_addr,addr_p,16);//“二进制整数” -> “点分十进制  
            cout<< buf << endl;
            write(c_fd,buf,n);//sent message back to client  
            
        }  
    //}  
    return 0;  
}  

2.client.cpp

 1 #include <stdio.h>  
 2 #include <string.h>  
 3 #include <iostream>  
 4 #include <stdlib.h>  
 5 #include <string>  
 6 #include <unistd.h>  
 7 #include <arpa/inet.h>  
 8 #include <sys/socket.h>  
 9 #define BUF_SIZE 100  
10 #define ADDR "192.168.132.131" //在本机测试用这个地址,如果连接其他电脑需要更换IP  
11 #define SERVERPORT 10001  
12 using namespace std;  
13 int main(int argc, char *argv[])  
14 {  
15     int sock;  
16     char opmsg[BUF_SIZE];  
17     char get_msg[BUF_SIZE] = {0};  
18     int result, opnd_cnt, i;  
19     int len;  
20     bool if_first = true;  
21     struct sockaddr_in serv_addr;  
22   
23     sock = socket(PF_INET, SOCK_STREAM, 0);  
24     if(sock == -1){  
25         return -1;  
26     }  
27     memset(&serv_addr, 0, sizeof(serv_addr));  
28     serv_addr.sin_family = AF_INET;  
29     serv_addr.sin_addr.s_addr = inet_addr(ADDR);  // 注释1  
30     serv_addr.sin_port = htons(SERVERPORT);  
31     if(connect(sock, (struct sockaddr*) &serv_addr, sizeof(serv_addr))==-1){ // 注释2  
32         cout << "connect error\n";  
33         return -1;  
34     }  
35     else{  
36         cout << "connected ...\n" << endl;  //注释3  
37     }  
38   
39     while(1){  
40         fgets(opmsg, BUF_SIZE, stdin);          
41         len = write(sock, opmsg, strlen(opmsg)); // 注释4  
42         if(!strcmp(opmsg, "q\n") || !strcmp(opmsg, "Q\n"))  //输入q/Q则断开连接,推出程序
43         {  
44             puts("q pressed\n");  
45             break;  
46         }  
47         else  
48         {  
49             int read_msg = read(sock, get_msg, len);  
50             cout << "send length:" << len << " get:" << get_msg << endl;  
51         }  
52     }  
53     close(sock);  
54     return 0;  
55 }  

3.g++

g++ -o serv serv.cpp

g++ -o client client.cpp

4.execute

./serv

./client

猜你喜欢

转载自www.cnblogs.com/capri-tu/p/10538652.html