今天学了一下socket编程的内容,看了很多资料和代码,然后自己做了一下实验,一台windows客户端和linux主机之间进行通信,这两者的socket编程部分稍微有一些差别,不过大体上还是一样的,windows要做的准备工作要多一些。
linux服务器端代码:监听端口为6666
View Code
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <errno.h> 5 #include <sys/types.h> 6 #include <sys/socket.h> 7 #include <netinet/in.h> 8 using namespace std; 9 #define MAXLINE 4096 10 int main(int argc,char * argv[]) 11 { 12 int listenfd,connfd; 13 struct sockaddr_in servaddr; 14 char buff[4096]; 15 int n; 16 if((listenfd = socket(AF_INET,SOCK_STREAM,0)) == -1) 17 { 18 printf("create socket error: %s(errno :%d)\n",strerror(errno),errno); 19 exit(0); 20 } 21 memset(&servaddr,0,sizeof(servaddr)); 22 servaddr.sin_family = AF_INET; 23 servaddr.sin_addr.s_addr = htonl(INADDR_ANY); 24 servaddr.sin_port = htons(6666); 25 if(bind(listenfd,(struct sockaddr*)&servaddr, sizeof(servaddr)) == -1) 26 { 27 printf("bind socket error: %s(errno: %d)\n",strerror(errno),errno); 28 exit(0); 29 } 30 if(listen(listenfd,10) == -1) 31 { 32 printf("listen socket error : %s(errno: %d)\n",strerror(errno),errno); 33 exit(0); 34 } 35 printf("===========waiting for client's request========\n"); 36 while(1) 37 { 38 struct sockaddr_in nowaddr; 39 int size = sizeof(sockaddr_in); 40 if((connfd = accept(listenfd, (struct sockaddr*)&nowaddr, (socklen_t*)&size)) == -1) 41 { 42 printf("accept socket error: %s(errno: %d)",strerror(errno),errno); 43 continue; 44 } 45 n = recv(connfd,buff,MAXLINE,0); 46 buff[n] = '\0'; 47 printf("recv msg from client: %s\n",buff); 48 close(connfd); 49 } 50 close(listenfd); 51 return 0; 52 }
windows客户端部分
View Code
1 #include <winsock2.h> 2 #include <windows.h> 3 #include <iostream> 4 #include <stdio.h> 5 #include <errno.h> 6 #include <string.h> 7 #pragma comment(lib,"ws2_32.lib")//注意链接库文件 8 #define MAXLINE 4096 9 int main(int argc,char * argv[]) 10 { 11 int n; 12 SOCKET sockfd; 13 char recvline[MAXLINE],sendline[4096]; 14 SOCKADDR_IN servaddr; 15 WORD wVersionRequested; //typedef unsigned short WORD; 2字节 16 WSADATA wsaData; 17 int err; 18 wsaData.wVersion =MAKEWORD(1,1); 19 err=WSAStartup(wVersionRequested,&wsaData); //return 0 if successful 20 if(err!=0) 21 { 22 printf("Call WSAStart ERROR!"); 23 exit(1); 24 } 25 if(argc != 2) 26 { 27 printf("usage: ./client <ipaddress>\n"); 28 exit(0); 29 } 30 if((sockfd = socket(AF_INET,SOCK_STREAM,0))< 0) 31 { 32 printf("create socket error: %s(errno: %d)\n",strerror(errno),errno); 33 exit(0); 34 } 35 memset(&servaddr, 0, sizeof(servaddr)); 36 servaddr.sin_family = AF_INET; 37 servaddr.sin_port = htons(6666); 38 servaddr.sin_addr.S_un.S_addr=inet_addr(argv[1]); 39 40 if( connect(sockfd, (struct sockaddr*)&servaddr, sizeof(SOCKADDR_IN))< 0) 41 { 42 printf("connect error: %s(errno: %d)\n",strerror(errno),errno); 43 exit(0); 44 } 45 printf("send msg to server: \n"); 46 sprintf(sendline,"hello,it's the client msg"); 47 if(send(sockfd, sendline, strlen(sendline), 0) < 0) 48 { 49 printf("send msg error: %s(errno: %d)\n",strerror(errno),errno); 50 exit(0); 51 } 52 closesocket(sockfd); 53 exit(0); 54 }