1、SOCKET例子1.简单服务器/#include /#pragma comment(lib,WS2_32.lib)WSADATA wsd;static UINT port=%1;UINT Listen(LPVOID pParam)SOCKET sServer,sClient;char buf1024;int retVal;if(WSAStartup(MAKEWORD(2,2),&wsd)!=0) return -1;/失败sServer=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);if(INVALID_SOCKET=sServer)WSACleanup();
2、return -1;/创建套接字失败SOCKADDR_IN addrServ;addrServ.sin_family=AF_INET;addrServ.sin_port=htons(short)pParam);addrServ.sin_addr.s_addr=INADDR_ANY;retVal=bind(sServer,(LPSOCKADDR)&addrServ,sizeof(SOCKADDR_IN);if(SOCKET_ERROR=retVal)closesocket(sServer);WSACleanup();return -1;/绑定套接字失败retVal=listen(sServer,
3、1);if(SOCKET_ERROR=retVal)closesocket(sServer);WSACleanup();return -1;/开始监听失败sockaddr_in addrClient;int addrClientlen=sizeof(addrClient);sClient=accept(sServer,(sockaddr FAR*)&addrClient,&addrClientlen);if(INVALID_SOCKET=sClient)closesocket(sServer);WSACleanup();return -1;/开始接受客户端连接失败ZeroMemory(buf,
4、sizeof(buf);retVal=recv(sClient,buf,sizeof(buf),0);if(SOCKET_ERROR=retVal)closesocket(sServer);closesocket(sClient);WSACleanup();return -1;/接收数据失败CString %2(buf);closesocket(sServer);closesocket(sClient);WSACleanup(); return 0;CWinThread *pThread=AfxBeginThread(Listen,&port);2.简单客户端/#include /#pragm
5、a comment(lib,WS2_32.lib)WSADATA wsd;SOCKET sHost;SOCKADDR_IN servAddr;char buf1024;int retVal;if(WSAStartup(MAKEWORD(2,2),&wsd)!=0)return -1;/失败sHost=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);if(INVALID_SOCKET=sHost)WSACleanup();return -1;/创建套接字失败servAddr.sin_family=AF_INET;servAddr.sin_addr.s_addr=i
6、net_addr(%3);servAddr.sin_port=htons(short)%2);int nServAddlen=sizeof(servAddr);retVal=connect(sHost,(LPSOCKADDR)&servAddr,sizeof(servAddr);if(SOCKET_ERROR=retVal) closesocket(sHost);WSACleanup();return -1;/连接服务器失败ZeroMemory(buf,sizeof(buf);strcpy(buf,%3);retVal=send(sHost,buf,sizeof(buf),0);if(SOCK
7、ET_ERROR=retVal)closesocket(sHost);WSACleanup();return -1;/向服务器发送数据失败closesocket(sHost);WSACleanup();3.获得本机IP/#include /#pragma comment(lib,WS2_32.lib)WSADATA wsd;if(WSAStartup(MAKEWORD(2,2),&wsd)!=0)return -1;/失败char szHostname100,szHostaddress200;if(gethostname(szHostname,sizeof(szHostname)!=SOCKE
8、T_ERROR)HOSTENT *pHostEnt=gethostbyname(szHostname);if(pHostEnt!=NULL)sprintf(szHostaddress,%d.%d.%d.%d,( pHostEnt-h_addr_list00&0x00ff ),( pHostEnt-h_addr_list01&0x00ff ),( pHostEnt-h_addr_list02&0x00ff ),( pHostEnt-h_addr_list03&0x00ff );elsereturn;CString %1(szHostaddress);4.端对端通信/#include /#prag
9、ma comment(lib,WS2_32.lib)WSADATA wsd;SOCKET s;char buf1024;if(WSAStartup(MAKEWORD(2,2),&wsd)!=0)return -1;/失败s=socket(AF_INET,SOCK_DGRAM,0);if(s=INVALID_SOCKET)WSACleanup();return -1;/创建套接字失败SOCKADDR_IN servAddr;servAddr.sin_family=AF_INET;servAddr.sin_addr.s_addr=inet_addr(%1);servAddr.sin_port=ht
10、ons(INADDR_ANY);if(bind(s,(SOCKADDR*)&servAddr,sizeof(SOCKADDR_IN)=SOCKET_ERROR)closesocket(s);WSACleanup();return -1;/绑定套接字失败int nServAddrlen=sizeof(servAddr);ZeroMemory(buf,sizeof(buf);if(recvfrom(s,buf,sizeof(buf),0,(SOCKADDR*)&servAddr,&nServAddrlen)=SOCKET_ERROR)closesocket(s);WSACleanup();retu
11、rn -1;/接收数据失败CString %2(buf);ZeroMemory(buf,sizeof(buf);strcpy(buf,%3);SOCKADDR_IN clientAddr;clientAddr.sin_family=AF_INET;clientAddr.sin_addr.s_addr=inet_addr(%4);clientAddr.sin_port=htons(short)%5);int nClientlen=sizeof(clientAddr);if(sendto(s,buf,sizeof(buf),0,(SOCKADDR*)&clientAddr,nClientlen)=
12、SOCKET_ERROR)closesocket(s);WSACleanup();return -1;/向服务器发送数据失败closesocket(s);WSACleanup();5.点对点通信/#include /#pragma comment(lib,WS2_32.lib)WSADATA wsd;SOCKADDR_IN addrServ,addrServ2;SOCKET sServer,sClient,sHost;int retVal;sockaddr_in addrClient;char buf1024;static UINT port=%2;BOOL listenerRun=TRUE;
13、UINT Listen(LPVOID pParam)addrServ.sin_family=AF_INET;addrServ.sin_port=htons(UINT)pParam);addrServ.sin_addr.s_addr=INADDR_ANY;retVal=bind(sServer,(LPSOCKADDR)&addrServ,sizeof(SOCKADDR_IN);if(SOCKET_ERROR=retVal)closesocket(sServer);WSACleanup();return -1;/绑定套接字失败retVal=listen(sServer,1);if(SOCKET_E
14、RROR=retVal)closesocket(sServer);WSACleanup();return -1;/开始监听失败int addrClientlen=sizeof(addrClient);sClient=accept(sServer,(sockaddr FAR*)&addrClient,&addClientlen);if(INVALID_SOCKET=sClient)closesocket(sServer);WSACleanup();return -1;/接收客户端请求失败while(listenerRun)ZeroMemory(buf,sizeof(buf);retVal=rec
15、v(sClient,buf,sizeof(buf);if(SOCKET_ERROR=retVal)closesocket(sServer);closesocket(sClient);WSACleanup();return -1;/接收客户端数据失败CString %4(buf);if(WSAStartup(MAKEWORD(2,2),&wsd)!=0)return -1;/失败sServer=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);if(INVALID_SOCKET=sServer)WSACleanup();return -1;/创建套接字失败CWinT
16、hread *pThread=AfxBeginThread(Listen,&port);sHost=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);if(INVALID_SOCKET=sHost)WSACleanup();return -1;/创建套接字失败servAddr2.sin_family=AF_INET;servAddr2.sin_addr.s_addr=inet_addr(%1);servAddr.sin_port=htons(short)%3);int nServerAddrlen=sizeof(servAddr2);retVal=connect(
17、sHost,(LPSOCKADDR)&servAddr2,sizeof(servAddr2);if(SOCKET_ERROR=retVal)closesocket(sHost);WSACleanup();return -1;/连接失败zeroMemory(buf,sizeof(buf);strcpy(buf,%5);retVal=send(sHost,buf,sizeof(buf),0);if(SOCKET_ERROR=retVal)closesocket(sHost);WSACleanup();return -1;/向发送数据失败listenerRun=FALSE;DWORD dwExitCode;:GetExitCodeThread(pThread-m_hThread,&dwExitCode);pThread=null;closesocket(sServer);closesocket(sClient);closesocket(sHost);WSACleanup();
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1