网络编程socket套接字及其使用(五)
今天我们继续就网络编程进行深入的探讨,我们知道TCP/IP协议是面向连接的socket编程,也就是通信是基于连接,必须先建立连接,我们才可以进行通信。具体流程如下:
服务器端流程:
- 创建套接字(socket)
- 将套接字绑定到一个本地地址和端口上(bind)
- 将套接字设为监听模式,准备接受客户请求(listen)
- 等待客户请求到来:当请求到来后,接受连接请求,返回一个新的对于此次连接的套接字(accept)
- 用返回的套接字和客户端进行通信(send/recv)
- 返回等待另一个客户请求。
- 关闭套接字。
- 代码如下:
1 #include<Winsock2.h> 2 #include<stdio.h> 3 4 void main() 5 { 6 //load socket libariy 7 WORD wVersionRequested; 8 //this struct use to stored Windows Sockets value after call WSAStartup() function, 9 //include the data from Winscok.dll 10 WSADATA wsaData; 11 int err; 12 wVersionRequested=MAKEWORD(1,1); 13 err = WSAStartup(wVersionRequested,&wsaData); 14 if(err!=0) 15 { 16 return; 17 } 18 if(LOBYTE(wsaData.wVersion)!=1 || (HIBYTE(wsaData.wVersion)!=1)) 19 { 20 WSACleanup(); 21 return; 22 } 23 //socket listen 24 SOCKET sockSrv=socket(AF_INET,SOCK_STREAM,0); 25 //server address information 26 SOCKADDR_IN addrSrv; 27 addrSrv.sin_addr.S_un.S_addr=htonl(INADDR_ANY);//Default IP address 28 addrSrv.sin_family=AF_INET; 29 addrSrv.sin_port=htons(6000); //nPort 30 //bind socket 31 bind(sockSrv,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR)); 32 //listen 33 listen(sockSrv,5); 34 35 //client address information 36 SOCKADDR_IN addrClient; 37 int len=sizeof(SOCKADDR); 38 while(1) 39 { 40 //wait for client connect request 41 SOCKET sockConn=accept(sockSrv,(SOCKADDR*)&addrClient,&len); 42 char sendBuf[100]; 43 //printf client's address information 44 sprintf(sendBuf,"Welcome %s to server.",inet_ntoa(addrClient.sin_addr)); 45 //send message 46 send(sockConn,sendBuf,strlen(sendBuf)+1,0); 47 //receive message from client 48 char recvBuf[100]; 49 recv(sockConn,recvBuf,100,0); 50 printf("%s\n",recvBuf); 51 //close socket 52 closesocket(sockConn); 53 } 54 }
- 注:需在项目添加 ws2_32.lib 库模块(Project Setting--- Link -> Object/library modules).这里值得注意的是,程序的开头是先加载套接字的库,之后才开始进行按照1--7step执行的,所以确切的说第一步应该是套接字库的加载。
客户端流程:
- 创建套接字(socket)
- 向服务器发出连接请求(connect)
- 和服务器进行通信(send/recv)
- 关闭套接字
- 代码如下:
1 #include<Winsock2.h> 2 #include<stdio.h> 3 4 void main() 5 { 6 //load socket 7 WORD wVersionRequested; 8 //this struct use to stored Windows Sockets value after call WSAStartup() function, 9 //include the data from Winscok.dll 10 WSADATA wsaData; 11 int err; 12 wVersionRequested=MAKEWORD(1,1); 13 err = WSAStartup(wVersionRequested,&wsaData); 14 if(err!=0) 15 { 16 return; 17 } 18 if(LOBYTE(wsaData.wVersion)!=1 || (HIBYTE(wsaData.wVersion)!=1)) 19 { 20 WSACleanup(); 21 return; 22 } 23 //socket create 24 SOCKET sockClient=socket(AF_INET,SOCK_STREAM,0); 25 //server address information 26 SOCKADDR_IN addrSrv; 27 addrSrv.sin_addr.S_un.S_addr=inet_addr("127.0.0.1"); 28 addrSrv.sin_family=AF_INET; 29 addrSrv.sin_port=htons(6000); 30 //connect request 31 connect(sockClient,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR)); 32 //receve data 33 char recvBuf[100]; 34 recv(sockClient,recvBuf,100,0); 35 printf("%s\n",recvBuf); 36 //send data 37 send(sockClient,"this is a message from client.",strlen("this is a message from client.")+1,0); 38 //close socket 39 closesocket(sockClient); 40 WSACleanup(); 41 }
End.
谢谢.