《C套接字编程示例》由会员分享,可在线阅读,更多相关《C套接字编程示例(9页珍藏版)》请在金锄头文库上搜索。
1、sockets(套接字)编程有三种,流式套接字(SOCK_STREAM),数据报套接字(SOCK_DGRAM),原始套接字(SOCK_RAW);基于TCP的socket编程是采用的流式套接字。服务器端编程的步骤:1:加载套接字库,创建套接字(WSAStartup()/socket();2:绑定套接字到一个IP地址和一个端口上(bind();3:将套接字设置为监听模式等待连接请求(listen();4:请求到来后,接受连接请求,返回一个新的对应于此次连接的套接字(accept();5:用返回的套接字和客户端进行通信(send()/recv();6:返回,等待另一连接请求;7:关闭套接字,关闭加载
2、的套接字库(closesocket()/WSACleanup()。客户端编程的步骤:1:加载套接字库,创建套接字(WSAStartup()/socket();2:向服务器发出连接请求(connect();3:和服务器端进行通信(send()/recv();4:关闭套接字,关闭加载的套接字库(closesocket()/WSACleanup()。第一式: 加载/释放Winsock库:1.加载方法:WSADATA wsa;/*初始化socket资源*/if (WSAStartup(MAKEWORD(1,1),&wsa) != 0) return; /代表失败2.释放方法:WSACleanup();
3、第二式: 构造SOCKET:1.服务端:构造监听SOCKET,流式SOCKET. SOCKETListen_Sock= socket(AF_INET, SOCK_STREAM, 0)2.客户端:构造通讯SOCKET,流式SOCKET.SOCKETClient_Sock= socket(AF_INET, SOCK_STREAM, 0)第三式:配置监听地址和端口:1.服务端: SOCKADDR_INserverAddrZeroMemory(char *)&serverAddr,sizeof(serverAddr);serverAddr.sin_family =AF_INET;serverAddr.
4、sin_port = htons(1234); /*本地监听端口:1234*/serverAddr.sin_addr.s_addr = htonl(INADDR_ANY); /*有IP*/第四式: 绑定SOCKET:1.服务端:绑定监听SOCKET.bind(Listen_Sock,(struct sockaddr *)&serverAddr,sizeof(serverAddr)第五式: 服务端/客户端连接:1.服务端:等待客户端接入. SOCKETCommand_Sock= accept(Listen_Sock, .)2.客户端:请求与服务端连接.int ret = connect(Clie
5、nt_Sock, .)第六式: 收/发数据:1.服务端:等待客户端接入.char buf1024. 接收数据:recv(Command_Sock,buf, .)或 发送数据:send(Command_Sock,buf, .)2.客户端:请求与服务端连接.char buf1024. 发送数据:send(Client_Sock,buf, .)或 接收数据:recv(Client_Sock,buf, .)第七式: 关闭SOCKET:1.服务端:关闭SOCKET. closesocket(Listen_Sock)closesocket(Command_Sock)2.客户端:关闭SOCKET.close
6、socket(Client_Sock)客户机端#include #include void main() /加载winsocket库WORD wVersionRequested; WSADATA wsaData; int err; wVersionRequested = MAKEWORD( 1, 1 ); err = WSAStartup( wVersionRequested, &wsaData ); if ( err != 0 ) return; if ( LOBYTE( wsaData.wVersion ) != 1 | HIBYTE( wsaData.wVersion ) != 1 )
7、WSACleanup( ); return; /创建socket SOCKET sockClient=socket(AF_INET,SOCK_STREAM,0); /创建socket要连接的(地址:端口) SOCKADDR_IN addrSrv; addrSrv.sin_addr.S_un.S_addr=inet_addr(127.0.0.1); addrSrv.sin_family=AF_INET; addrSrv.sin_port=htons(6000); /请求与服务器建立连接connect(sockClient,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR);
8、/发送数据 send(sockClient,hello,strlen(hello)+1,0); char recvBuf50;/接受数据recv(sockClient,recvBuf,50,0); printf(%sn,recvBuf); /关闭套接字 closesocket(sockClient);/释放Socket库 WSACleanup();服务器端#include #include void main() /加载Winsock库 WORD wVersionRequested; WSADATA wsaData; int err; wVersionRequested = MAKEWORD(
9、 1, 1 ); err = WSAStartup( wVersionRequested, &wsaData ); if ( err != 0 ) return; if ( LOBYTE( wsaData.wVersion ) != 1 | HIBYTE( wsaData.wVersion ) != 1 ) WSACleanup( ); return; /建立流式Socket SOCKET sockSrv=socket(AF_INET,SOCK_STREAM,0); /配置监听端口号 SOCKADDR_IN addrSrv; addrSrv.sin_addr.S_un.S_addr=htonl
10、(INADDR_ANY); addrSrv.sin_family=AF_INET; addrSrv.sin_port=htons(6000); /绑定监听socket(socket和端口绑定在一起)bind(sockSrv,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR); /在Socket上监听 listen(sockSrv,5); /定义一个变量,将存储客户端地址 SOCKADDR_IN addrClient; int len=sizeof(SOCKADDR); while(1) /等待客户端接入 SOCKET sockConn=accept(sockSrv,(SO
11、CKADDR*)&addrClient,&len); char sendBuf50; sprintf(sendBuf,Welcome %s to here!,inet_ntoa(addrClient.sin_addr); /发送数据send(sockConn,sendBuf,strlen(sendBuf)+1,0); char recvBuf50; /接受数据recv(sockConn,recvBuf,50,0); printf(%sn,recvBuf); /关闭套接字closesocket(sockConn); 面向无连接的套接字编程(数据报套接字)客户端:#include Winsock2
12、.h#include stdio.hint main()/加载socket库函数 WORD wVersionRequested; WSADATA wsaData; int err; wVersionRequested = MAKEWORD( 1, 1 ); err = WSAStartup( wVersionRequested, &wsaData ); if ( err != 0 ) return 0; if ( LOBYTE( wsaData.wVersion ) != 1 | HIBYTE( wsaData.wVersion ) != 1 ) WSACleanup( ); return 0
13、; /创建套接字 SOCKET sockClient = socket(AF_INET,SOCK_DGRAM,0); SOCKADDR_IN sockSrv; /创建地址+端口 sockSrv.sin_addr.S_un.S_addr=inet_addr(127.0.0.1); sockSrv.sin_family = AF_INET ; sockSrv.sin_port = htons(6000); /直接发送sendto(sockClient,hello,strlen(hello)+1,0,(SOCKADDR*)&sockSrv,sizeof(SOCKADDR); closesocket(sockClient); WSACleanup(); return 0;服务器:#include Winsock2.h#include stdio.hint main() /加载socket库函数 WORD wVersionRequested; WSADATA wsaData; int err; wVersionRequested = MAKEWORD( 1, 1 ); err = WSAStartup( wVersionRequested, &wsaData ); if ( err != 0 )