基于linux或windows平台上的c/s简单通信

血红的双手。 提交于 2019-12-04 11:55:14

linux:

 tcpclient.cpp

  1 #include<iostream>
  2 #include<unistd.h>
  3 #include<sys/types.h>
  4 #include<sys/socket.h>
  5 #include<netdb.h>
  6 #include<arpa/inet.h>
  7 #include<cstring>
  8 #include<sstream>
  9 
 10 using namespace std;
 11 
 12 #define BUFSIZE 512
 13 
 14 // #define SERVERIP "192.168.41.32" 
 15 // #define SERVERPORT 4140
 16 
 17 /*error report*/
 18 static void bail(const char *on_what){
 19     fputs(strerror(errno), stderr);
 20     fputs(": ", stderr);
 21     fputs(on_what, stderr);
 22     fputc('\n', stderr);  
 23     exit(1);
 24 }
 25 
 26 void getarg(int argc,char* argv[],const char** SERVERIP,int* SERVERPORT)
 27 {
 28     for(int i=0;i<argc;i++)
 29     {
 30         istringstream iss(argv[i]);
 31         string str;
 32         iss>>str;
 33         if(str=="ip")
 34         {
 35             *SERVERIP=argv[i+1];
 36         }
 37         else if(str=="port")
 38         {
 39             istringstream sts(argv[i+1]);
 40             string s_port;
 41             sts>>s_port;
 42             *SERVERPORT=stoi(s_port);
 43         }
 44         else
 45         {
 46             
 47         }
 48         
 49     }
 50 }
 51 
 52 int main(int argc,char *argv[])
 53 {
 54     const char* SERVERIP="192.168.43.230";
 55     int SERVERPORT=4140;
 56 
 57     getarg(argc,argv,&SERVERIP,&SERVERPORT);
 58 
 59     int sockfd;
 60     struct sockaddr_in server_addr;
 61     const char* sendbuf = (char*)"hello,this is client";
 62     char recvbuf[BUFSIZE];
 63 
 64     //create socket
 65     if ((sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) <0)//创建套接字
 66     {
 67         fprintf(stderr,"Socket error %s\n",strerror(errno));
 68         exit(-1);
 69     }
 70 
 71     memset(&server_addr,0, sizeof(server_addr));
 72     server_addr.sin_family = AF_INET;
 73     server_addr.sin_port = htons(SERVERPORT);
 74     server_addr.sin_addr.s_addr = inet_addr(SERVERIP);
 75 
 76     if ((connect(sockfd, (struct sockaddr*) & server_addr, sizeof(struct sockaddr))) < 0)//连接远程对等实体
 77     {
 78         fprintf(stderr,"connect error %s\n",strerror(errno));
 79         exit(-1);
 80     }
 81 
 82     if ((send(sockfd, sendbuf, strlen(sendbuf), 0)) != strlen(sendbuf))//发送数据
 83     {
 84         fprintf(stderr,"send error %s\n",strerror(errno));
 85         exit(-1);
 86     }
 87 
 88     memset(recvbuf, '\0',sizeof(recvbuf));
 89     while (true)
 90     {
 91         int num = recv(sockfd, recvbuf, sizeof(recvbuf), 0);//接受数据
 92         if (num < 0)
 93         {
 94         fprintf(stderr,"recv error %s\n",strerror(errno));
 95         exit(-1);
 96         }
 97         else
 98         {
 99             cout << recvbuf << endl;
100             break;
101         }
102     }
103 
104     cout<<"exit..."<<endl;
105 
106     close(sockfd);//终止通信并释放套接字描述符
107     
108     return 0;
109 }
View Code

tcpserv.cpp

  1 #include<iostream>
  2 #include<string.h>
  3 #include <unistd.h>
  4 #include<sys/types.h>
  5 #include<sys/socket.h>
  6 #include<netdb.h>
  7 #include<arpa/inet.h>
  8 
  9 #define BUFSIZE 512
 10 #define PORT 4140
 11 #define MAXLISTEN 128
 12 
 13 /*error report*/
 14 static void bail(const char *on_what){
 15     fputs(strerror(errno), stderr);
 16     fputs(": ", stderr);
 17     fputs(on_what, stderr);
 18     fputc('\n', stderr);  
 19     exit(1);
 20 }
 21 
 22 int main()
 23 {
 24     int sockfd;//server fd
 25     // int port;
 26     int newfd;//connect fd
 27     struct sockaddr_in server_addr;
 28     struct sockaddr_in client_addr;
 29 
 30     char reqbuf[BUFSIZE];
 31 
 32     /*create server socket*/
 33     if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0)//创建套接字
 34     {
 35         fprintf(stderr,"Socket error %s\n",strerror(errno));
 36         exit(-1);
 37     }
 38 
 39     /*ready for server addr*/
 40     memset(&server_addr,0,sizeof(server_addr));
 41     server_addr.sin_family=AF_INET;
 42     server_addr.sin_addr.s_addr=htonl(INADDR_ANY);
 43     server_addr.sin_port=htons(PORT);
 44 
 45     /*bind socket addr*/
 46     int tmp=bind(sockfd,(struct sockaddr*)&server_addr,sizeof(server_addr));//将ip和port绑定到套接字上
 47     if(tmp<0)
 48     {
 49         fprintf(stderr,"Bind error %s\n",strerror(errno));
 50         exit(-1);
 51     }
 52 
 53     /*listen*/
 54     if((listen(sockfd,MAXLISTEN))<0)//将套接字设置为被动模式
 55     {
 56         fprintf(stderr,"Listen error %s\n",strerror(errno));
 57         exit(-1);
 58     }
 59     std::cout<<"waiting for client ..."<<std::endl;
 60 
 61     /*server main while*/
 62     // while(true)
 63     // {
 64         socklen_t size;
 65         size=sizeof(struct sockaddr_in);
 66 
 67         /*accept client & create new fd*/
 68         if((newfd=accept(sockfd,(struct sockaddr*)&client_addr,&size))<0)//接受传入连接请求
 69         {
 70             fprintf(stderr,"Accept error %s\n",strerror(errno));
 71             exit(-1);
 72         }
 73         std::cout<<"Server got connect from "<<inet_ntoa(client_addr.sin_addr)<<std::endl;
 74 
 75         /*recv data from client*/
 76         if((recv(newfd,reqbuf,sizeof(reqbuf),0))<0)//获取数据
 77         {
 78             fprintf(stderr,"Recv error %s\n",strerror(errno));
 79             exit(-1);
 80         }
 81         std::cout<<reqbuf<<std::endl;
 82         
 83         /*send data to client*/
 84         char *sendbuf=(char*)"hello,this is server.";
 85         if((send(newfd,sendbuf,strlen(sendbuf),0))!=strlen(sendbuf))//发送数据
 86         {
 87             fprintf(stderr,"Send error %s\n",strerror(errno));
 88             exit(-1);
 89         }
 90 
 91         /*close new fd*/
 92         close(newfd);//终止通信并释放套接字描述符
 93     // }
 94 
 95     /*close server fd*/
 96     close(sockfd);//释放套接字描述符
 97 
 98     std::cout<<"exit"<<std::endl;
 99 
100     return 0;
101 }
View Code

udpclient.cpp

  1 #include<iostream>
  2 #include <unistd.h>
  3 #include<sys/types.h>
  4 #include<sys/socket.h>
  5 #include<netdb.h>
  6 #include<arpa/inet.h>
  7 #include<cstring>
  8 #include<sstream>
  9 
 10 using namespace std;
 11 
 12 #define BUFSIZE 512
 13 
 14 // #define SERVERIP "192.168.2.169"
 15 // #define SERVERPORT 4140
 16 
 17 /*error report*/
 18 static void bail(const char *on_what){
 19     fputs(strerror(errno), stderr);
 20     fputs(": ", stderr);
 21     fputs(on_what, stderr);
 22     fputc('\n', stderr);  
 23     exit(1);
 24 }
 25 
 26 void getarg(int argc,char* argv[],const char** SERVERIP,int* SERVERPORT)
 27 {
 28     for(int i=0;i<argc;i++)
 29     {
 30         istringstream iss(argv[i]);
 31         string str;
 32         iss>>str;
 33         if(str=="ip")
 34         {
 35             *SERVERIP=argv[i+1];
 36         }
 37         else if(str=="port")
 38         {
 39             istringstream sts(argv[i+1]);
 40             string s_port;
 41             sts>>s_port;
 42             *SERVERPORT=stoi(s_port);
 43         }
 44         else
 45         {
 46             
 47         }
 48         
 49     }
 50 }
 51 
 52 int main(int argc,char* argv[])
 53 {
 54     const char* SERVERIP="192.168.41.32";
 55     int SERVERPORT=4140;
 56 
 57     getarg(argc,argv,&SERVERIP,&SERVERPORT);
 58 
 59     int sockfd;
 60     struct sockaddr_in server_addr;
 61     struct sockaddr_in client_addr;
 62     char* sendbuf = (char*)"hello,this is client";
 63     char recvbuf[BUFSIZE];
 64 
 65     //create socket
 66     if ((sockfd = socket(AF_INET, SOCK_DGRAM,0)) <0)//创建套接字描述符
 67     {
 68         fprintf(stderr,"socket error %s\n",strerror(errno));
 69         exit(-1);
 70     }
 71 
 72     memset(&server_addr, 0,sizeof(server_addr));
 73     server_addr.sin_family = AF_INET;
 74     server_addr.sin_port = htons(SERVERPORT);
 75     server_addr.sin_addr.s_addr = inet_addr(SERVERIP);
 76 
 77     // if ((connect(sockfd, (struct sockaddr*) & server_addr, sizeof(struct sockaddr))) < 0)
 78     // {
 79     //     cout << "Connect Error::" << GetLastError() << endl;
 80     //     return -1;
 81     // }
 82 
 83     if ((sendto(sockfd, sendbuf, strlen(sendbuf), 0,(struct sockaddr*)&server_addr,sizeof(struct sockaddr_in))) != strlen(sendbuf))//发送数据
 84     {
 85         fprintf(stderr,"sendto error %s\n",strerror(errno));
 86         exit(-1);;
 87     }
 88 
 89     memset(&client_addr,0,sizeof(client_addr));
 90     memset(recvbuf, '\0',sizeof(recvbuf));
 91     while (true)
 92     {
 93         socklen_t len=sizeof(struct sockaddr_in);
 94         int num = recvfrom(sockfd, recvbuf, sizeof(recvbuf), 0,(struct sockaddr*)&client_addr,&len);//获取数据
 95         if (num < 0)
 96         {
 97         fprintf(stderr,"Socket error %s\n",strerror(errno));
 98         exit(-1);
 99         }
100         else
101         {
102             cout << recvbuf << endl;
103             break;
104         }
105     }
106 
107     close(sockfd);//终止通信并释放套接字描述符
108 
109     return 0;
110 }
View Code

udpserv.cpp

 1 #include<iostream>
 2 #include<string.h>
 3 #include <unistd.h>
 4 #include<sys/types.h>
 5 #include<sys/socket.h>
 6 #include<netdb.h>
 7 #include<arpa/inet.h>
 8 // #include<signal.h>
 9 
10 #define BUFSIZE 512
11 #define PORT 4140
12 #define MAXLISTEN 128
13 
14 
15 /*error report*/
16 static void bail(const char *on_what){
17     fputs(strerror(errno), stderr);
18     fputs(": ", stderr);
19     fputs(on_what, stderr);
20     fputc('\n', stderr);  
21     exit(1);
22 }
23 
24 int main()
25 {
26     int sockfd;//server fd
27     // int port;
28     int newfd;//connect fd
29     struct sockaddr_in server_addr;
30     struct sockaddr_in client_addr;
31 
32     char reqbuf[BUFSIZE];
33 
34 
35     /*create server socket*/
36     if((sockfd=socket(AF_INET,SOCK_DGRAM,0))<0)//创建套接字描述符
37     {
38         fprintf(stderr,"Socket error %s\n",strerror(errno));
39         exit(-1);
40     }
41 
42     /*ready for server addr*/
43     memset(&server_addr,0,sizeof(server_addr));
44     server_addr.sin_family=AF_INET;
45     server_addr.sin_addr.s_addr=htonl(INADDR_ANY);
46     server_addr.sin_port=htons(PORT);
47 
48     /*bind socket addr*/
49     if((bind(sockfd,(struct sockaddr*)&server_addr,sizeof(server_addr)))<0)//将ip和port绑定到套接字上 
50     {
51         fprintf(stderr,"Bind error %s\n",strerror(errno));
52         exit(-1);
53     }
54 
55     /*server main while*/
56     // while(true)
57     // {
58         socklen_t size;
59         size=sizeof(struct sockaddr_in);
60         memset(&client_addr,0,sizeof(client_addr));
61 
62         /*recv data from client*/
63         if((recvfrom(sockfd,reqbuf,sizeof(reqbuf),0,(struct sockaddr*)&client_addr,&size))<0)//获取数据
64         {
65             fprintf(stderr,"Recvfrom error %s\n",strerror(errno));
66             exit(-1);
67         }
68         std::cout<<reqbuf<<std::endl;
69         
70         /*send data to client*/
71         char *sendbuf=(char*)"hello,this is server.";
72         if((sendto(sockfd,sendbuf,strlen(sendbuf),0,(struct sockaddr*)&client_addr,size))!=strlen(sendbuf))//发送数据
73         {
74             fprintf(stderr,"Sendto error %s\n",strerror(errno));
75             exit(-1);
76         }
77 
78     // }
79 
80     /*close server fd*/
81     close(sockfd);//终止通信并释放套接字描述符
82 
83     std::cout<<"exit"<<std::endl;
84 
85     return 0;
86 }
View Code

windows:

tcpclient.cpp

  1 #include<iostream>
  2 #include<WinSock2.h>
  3 #include<cstring>
  4 #include<sstream>
  5 
  6 #pragma comment(lib, "ws2_32")
  7 
  8 using namespace std;
  9 
 10 // #define SERVERIP "192.168.41.32" 
 11 // #define SERVERPORT 4140
 12 
 13 void getarg(int argc,char* argv[],const char** SERVERIP,int* SERVERPORT)
 14 {
 15     for(int i=0;i<argc;i++)
 16     {
 17         istringstream iss(argv[i]);
 18         string str;
 19         iss>>str;
 20         if(str=="ip")
 21         {
 22             *SERVERIP=argv[i+1];
 23         }
 24         else if(str=="port")
 25         {
 26             istringstream sts(argv[i+1]);
 27             string s_port;
 28             sts>>s_port;
 29             *SERVERPORT=stoi(s_port);
 30         }
 31         else
 32         {
 33             
 34         }
 35         
 36     }
 37 }
 38 
 39 int main(int argc,char *argv[])
 40 {
 41     const char* SERVERIP="192.168.41.32";
 42     int SERVERPORT=4140;
 43 
 44     getarg(argc,argv,&SERVERIP,&SERVERPORT);
 45 
 46     SOCKET sockfd;
 47     struct sockaddr_in server_addr;
 48     WSADATA ws;
 49     const char* sendbuf = (char*)"hello,this is client";
 50     char recvbuf[MAX_PATH];
 51 
 52     //init windows socket
 53     if (WSAStartup(MAKEWORD(2, 2), &ws) != 0)
 54     {
 55         cout << "Init Windows Socket Failed::" << GetLastError() << endl;
 56         return -1;
 57     }
 58 
 59     //create socket
 60     if ((sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == INVALID_SOCKET)//创建套接字描述符
 61     {
 62         cout << "Create Socket Failed::" << GetLastError() << endl;
 63         return -1;
 64     }
 65 
 66     ZeroMemory(&server_addr, sizeof(server_addr));
 67     server_addr.sin_family = AF_INET;
 68     server_addr.sin_port = htons(SERVERPORT);
 69     server_addr.sin_addr.S_un.S_addr = inet_addr(SERVERIP);
 70 
 71     if ((connect(sockfd, (struct sockaddr*) & server_addr, sizeof(struct sockaddr))) < 0)//连接远程对等实体
 72     {
 73         cout << "Connect Error::" << GetLastError() << endl;
 74         return -1;
 75     }
 76 
 77     if ((send(sockfd, sendbuf, strlen(sendbuf), 0)) != strlen(sendbuf))//发送数据
 78     {
 79         cout << "Send Error::" << GetLastError() << endl;
 80         return -1;
 81     }
 82 
 83     ZeroMemory(recvbuf, sizeof(recvbuf));
 84     while (true)
 85     {
 86         int num = recv(sockfd, recvbuf, sizeof(recvbuf), 0);//获取数据
 87         if (num < 0)
 88         {
 89             cout << "Recv Error::" << GetLastError() << endl;
 90             return -1;
 91         }
 92         else
 93         {
 94             cout << recvbuf << endl;
 95             break;
 96         }
 97     }
 98 
 99     cout<<"exit..."<<endl;
100 
101     closesocket(sockfd);//终止通信并释放套接字
102     WSACleanup();
103 
104     system("pause");
105     
106     return 0;
107 }
View Code

tcpserver.cpp

  1 #include<iostream>
  2 #include<WinSock2.h>
  3 // #include<cstring>
  4 // #include<unistd.h>
  5 
  6 #pragma comment(lib, "ws2_32")
  7 
  8 using namespace std;
  9 
 10 #define BUFSIZE 512
 11 #define PORT 4140
 12 #define MAXLISTEN 128
 13 
 14 /*error report*/
 15 static void bail(const char *on_what){
 16     fputs(strerror(errno), stderr);
 17     fputs(": ", stderr);
 18     fputs(on_what, stderr);
 19     fputc('\n', stderr);  
 20     exit(1);
 21 }
 22 
 23 int main()
 24 {
 25     SOCKET sockfd;//server fd
 26     // int port;
 27     SOCKET newfd;//connect fd
 28     struct sockaddr_in server_addr;
 29     struct sockaddr_in client_addr;
 30     WSADATA ws;
 31 
 32     char reqbuf[BUFSIZE];
 33 
 34     //init windows socket
 35     if(WSAStartup(MAKEWORD(2,2),&ws)!=0)
 36     {
 37         cout << "Init Windows Socket Failed::" << GetLastError() << endl;
 38         exit(-1);
 39     }
 40 
 41     /*create server socket*/
 42     if((sockfd=socket(AF_INET,SOCK_STREAM,0))==INVALID_SOCKET)//创建套接字描述符
 43     {
 44         fprintf(stderr,"Socket error %s\n",strerror(errno));
 45         exit(-1);
 46     }
 47 
 48     /*ready for server addr*/
 49     ZeroMemory(&server_addr,sizeof(server_addr));
 50     ZeroMemory(&reqbuf,sizeof(reqbuf));
 51     server_addr.sin_family=AF_INET;
 52     server_addr.sin_addr.S_un.S_addr=htonl(INADDR_ANY);
 53     server_addr.sin_port=htons(PORT);
 54 
 55     /*bind socket addr*/
 56     int tmp=bind(sockfd,(struct sockaddr*)&server_addr,sizeof(server_addr));//将ip和port绑定到套接字上
 57     if(tmp<0)
 58     {
 59         fprintf(stderr,"Bind error %s\n",strerror(errno));
 60         exit(-1);
 61     }
 62 
 63     /*listen*/
 64     if((listen(sockfd,MAXLISTEN))<0)//设置为被动模式
 65     {
 66         fprintf(stderr,"Listen error %s\n",strerror(errno));
 67         exit(-1);
 68     }
 69     cout<<"waiting for client ..."<<endl;
 70 
 71     /*server main while*/
 72     // while(true)
 73     // {
 74         int size;
 75         size=sizeof(struct sockaddr_in);
 76 
 77         /*accept client & create new fd*/
 78         if((newfd=accept(sockfd,(struct sockaddr*)&client_addr,&size))<0)//接受传入请求
 79         {
 80             fprintf(stderr,"Accept error %s\n",strerror(errno));
 81             exit(-1);
 82         }
 83         cout<<"Server got connect from "<<inet_ntoa(client_addr.sin_addr)<<endl;
 84 
 85         /*recv data from client*/
 86         if((recv(newfd,reqbuf,sizeof(reqbuf),0))<0)//获取数据
 87         {
 88             fprintf(stderr,"Recv error %s\n",strerror(errno));
 89             exit(-1);
 90         }
 91         cout<<reqbuf<<endl;
 92         
 93         /*send data to client*/
 94         char *sendbuf=(char*)"hello,this is server.";
 95         if((send(newfd,sendbuf,strlen(sendbuf),0))!=strlen(sendbuf))//发送数据
 96         {
 97             fprintf(stderr,"Send error %s\n",strerror(errno));
 98             exit(-1);
 99         }
100 
101         /*close new fd*/
102         closesocket(newfd);//终止通信并释放套接字
103     // }
104 
105     /*close server fd*/
106     closesocket(sockfd);//释放套接字
107     WSACleanup();
108 
109     cout<<"exit"<<endl;
110 
111     return 0;
112 }
View Code

udpclient.cpp

  1 #include<iostream>
  2 #include<WinSock2.h>
  3 #include<sstream>
  4 
  5 #pragma comment(lib, "ws2_32")
  6 
  7 using namespace std;
  8 
  9 // #define SERVERIP "192.168.2.169"
 10 // #define SERVERPORT 4140
 11 
 12 void getarg(int argc,char* argv[],const char** SERVERIP,int* SERVERPORT)
 13 {
 14     for(int i=0;i<argc;i++)
 15     {
 16         istringstream iss(argv[i]);
 17         string str;
 18         iss>>str;
 19         if(str=="ip")
 20         {
 21             *SERVERIP=argv[i+1];
 22         }
 23         else if(str=="port")
 24         {
 25             istringstream sts(argv[i+1]);
 26             string s_port;
 27             sts>>s_port;
 28             *SERVERPORT=stoi(s_port);
 29         }
 30         else
 31         {
 32             
 33         }
 34         
 35     }
 36 }
 37 
 38 int main(int argc,char* argv[])
 39 {
 40     const char* SERVERIP="192.168.41.32";
 41     int SERVERPORT=4140;
 42 
 43     getarg(argc,argv,&SERVERIP,&SERVERPORT);
 44 
 45     SOCKET sockfd;
 46     struct sockaddr_in server_addr;
 47     struct sockaddr_in client_addr;
 48     WSADATA ws;
 49     char* sendbuf = (char*)"hello,this is client";
 50     char recvbuf[MAX_PATH];
 51 
 52     //init windows socket
 53     if (WSAStartup(MAKEWORD(2, 2), &ws) != 0)
 54     {
 55         cout << "Init Windows Socket Failed::" << GetLastError() << endl;
 56         return -1;
 57     }
 58 
 59     //create socket
 60     if ((sockfd = socket(AF_INET, SOCK_DGRAM,0)) == INVALID_SOCKET)//创建套接字描述符
 61     {
 62         cout << "Create Socket Failed::" << GetLastError() << endl;
 63         return -1;
 64     }
 65 
 66     ZeroMemory(&server_addr, sizeof(server_addr));
 67     server_addr.sin_family = AF_INET;
 68     server_addr.sin_port = htons(SERVERPORT);
 69     server_addr.sin_addr.S_un.S_addr = inet_addr(SERVERIP);
 70 
 71     // if ((connect(sockfd, (struct sockaddr*) & server_addr, sizeof(struct sockaddr))) < 0)
 72     // {
 73     //     cout << "Connect Error::" << GetLastError() << endl;
 74     //     return -1;
 75     // }
 76 
 77     if ((sendto(sockfd, sendbuf, strlen(sendbuf), 0,(struct sockaddr*)&server_addr,sizeof(struct sockaddr_in))) != strlen(sendbuf))//发送数据
 78     {
 79         cout << "Send Error::" << GetLastError() << endl;
 80         return -1;
 81     }
 82 
 83     ZeroMemory(&client_addr,sizeof(client_addr));
 84     ZeroMemory(recvbuf, sizeof(recvbuf));
 85     while (true)
 86     {
 87         int len=sizeof(struct sockaddr_in);
 88         int num = recvfrom(sockfd, recvbuf, sizeof(recvbuf), 0,(struct sockaddr*)&client_addr,&len);//获取数据
 89         if (num < 0)
 90         {
 91             cout << "Recv Error::" << GetLastError() << endl;
 92             return -1;
 93         }
 94         else
 95         {
 96             cout << recvbuf << endl;
 97             break;
 98         }
 99     }
100 
101     closesocket(sockfd);//终止通信并释放套接字
102     WSACleanup();
103 
104     system("pause");
105     
106     return 0;
107 }
View Code

udpserver.cpp

 1 #include<iostream>
 2 #include<winsock2.h>
 3 // #include<string.h>
 4 // #include<unistd.h>
 5 
 6 #pragma comment(lib, "ws2_32")
 7 
 8 using namespace std;
 9 
10 #define BUFSIZE 512
11 #define PORT 4140
12 #define MAXLISTEN 128
13 
14 /*error report*/
15 static void bail(const char *on_what){
16     fputs(strerror(errno), stderr);
17     fputs(": ", stderr);
18     fputs(on_what, stderr);
19     fputc('\n', stderr);  
20     exit(1);
21 }
22 
23 int main()
24 {
25     int sockfd;//server fd
26     // int port;
27     int newfd;//connect fd
28     WSADATA ws;
29     struct sockaddr_in server_addr;
30     struct sockaddr_in client_addr;
31 
32     char reqbuf[BUFSIZE];
33 
34     // signal(SIGINT,my_handler);
35 
36     //init windows socket
37     if(WSAStartup(MAKEWORD(2,2),&ws)!=0)
38     {
39         cout << "Init Windows Socket Failed::" << GetLastError() << endl;
40         exit(-1);
41     }
42 
43     /*create server socket*/
44     if((sockfd=socket(AF_INET,SOCK_DGRAM,0))<0)//创建套接字描述符
45     {
46         fprintf(stderr,"Socket error %s\n",strerror(errno));
47         exit(-1);
48     }
49 
50     /*ready for server addr*/
51     ZeroMemory(&server_addr,sizeof(server_addr));
52     server_addr.sin_family=AF_INET;
53     server_addr.sin_addr.S_un.S_addr=htonl(INADDR_ANY);
54     server_addr.sin_port=htons(PORT);
55 
56     /*bind socket addr*/
57     if((bind(sockfd,(struct sockaddr*)&server_addr,sizeof(server_addr)))==INVALID_SOCKET)//将ip和port绑定到套接字上
58     {
59         fprintf(stderr,"Bind error %s\n",strerror(errno));
60         exit(-1);
61     }
62 
63     /*server main while*/
64     // while(true)
65     // {
66         int size;
67         size=sizeof(struct sockaddr_in);
68         memset(&client_addr,0,sizeof(client_addr));
69 
70         /*recv data from client*/
71         if((recvfrom(sockfd,reqbuf,sizeof(reqbuf),0,(struct sockaddr*)&client_addr,&size))<0)//获取数据
72         {
73             fprintf(stderr,"Recvfrom error %s\n",strerror(errno));
74             exit(-1);
75         }
76         cout<<reqbuf<<endl;
77         
78         /*send data to client*/
79         char *sendbuf=(char*)"hello,this is server.";
80         if((sendto(sockfd,sendbuf,strlen(sendbuf),0,(struct sockaddr*)&client_addr,size))!=strlen(sendbuf))//发送数据
81         {
82             fprintf(stderr,"Sendto error %s\n",strerror(errno));
83             exit(-1);
84         }
85 
86     // }
87 
88     /*close server fd*/
89     closesocket(sockfd);//终止通信并释放套接字
90     WSACleanup();
91 
92     cout<<"exit"<<endl;
93 
94     return 0;
95 }
View Code

参考:

基于Linux平台实现的流式套接字客户端服务器端代码

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!