1、/服务器端#include #include #pragma comment(lib, “ws2_32.lib“)#define iPort 5050#include void main() printf(“n-n“);printf(“ Server waiting O(_)On“);printf(“-n“);WSADATA wsaData; if (WSAStartup(MAKEWORD(1, 1), return; if(LOBYTE(wsaData.wVersion)!=1|HIBYTE(wsaData.wVersion)!=1)WSACleanup();return;SOCKET sL
2、isten,sAccept;sListen=socket(AF_INET, SOCK_DGRAM, 0);SOCKADDR_IN addrSrv;addrSrv.sin_addr.S_un.S_addr=htonl(INADDR_ANY);addrSrv.sin_family=AF_INET;addrSrv.sin_port=htons(iPort);bind(sListen,(SOCKADDR*)SOCKADDR_IN addrClient;int len=sizeof(SOCKADDR);char recvBuf100;char sendbuf100;char tempbuf200;whi
3、le(1)recvfrom(sListen,recvBuf,100,0,(SOCKADDR*)if(=recvBuf0)sendto(sListen,“,strlen(“)+1,0,(SOCKADDR*)printf(“chat end!n“);break;cout#include #pragma comment(lib, “ws2_32.lib“)#define iPort 5050#include using namespace std;void main() cout“please input your datas( for ending)“endl;WSADATA wsaData; i
4、f (WSAStartup(MAKEWORD(1, 1), return; if(LOBYTE(wsaData.wVersion)!=1|HIBYTE(wsaData.wVersion)!=1)WSACleanup();return;SOCKET sockClient=socket(AF_INET,SOCK_DGRAM,0);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(5050);char sen
5、dbuf100;char recvbuf100;int len=sizeof(SOCKADDR);while(1)cout“I say:“;gets(sendbuf);sendto(sockClient,sendbuf,strlen(sendbuf)+1,0,(SOCKADDR*)coutendl;recvfrom(sockClient,recvbuf,100,0,(SOCKADDR*)if(=recvbuf0)printf(“chat end!n“);break;coutinet_ntoa(addrSrv.sin_addr)“ say:“recvbufendl;closesocket(sockClient);WSACleanup();