TcpServer: #include <stdio.h> #include <winsock2.h> //the endpoint of server void main() { //1.initiates windows socket WORD wVersionRequested = MAKEWORD(1,1); WSADATA wsaData; if (WSAStartup(wVersionRequested,&wsaData)!=0) { printf("server initiates windows socket failure/n"); WSACleanup(); return; } printf("server initiates windows socket success,the current socket version is :%d.%d/n",(WORD)(LOBYTE(wsaData.wVersion)),(WORD)(HIBYTE(wsaData.wVersion))); //2.create a windows socket SOCKET sockSvr=socket(AF_INET,SOCK_STREAM,0); if (sockSvr==INVALID_SOCKET) { printf("server create a windows socket failure/n"); WSACleanup(); return; } printf("server create a window socket success/n"); //3.associates a local address with a socket //create a local address SOCKADDR_IN sadressIn; sadressIn.sin_addr.S_un.S_addr = inet_addr("192.168.0.100"); sadressIn.sin_family = AF_INET; sadressIn.sin_port = htons(6000); if (bind(sockSvr,(SOCKADDR*)&sadressIn,sizeof(SOCKADDR))!=0) { printf("server associates a local address with socket failure/n"); WSACleanup(); return; } printf("server associates a local address success/n"); //4.ready listen if (listen(sockSvr,5)!=0) { printf("server read listen failure/n"); WSACleanup(); return; } printf("server ready listen success/n"); //5.accept and process client's request SOCKADDR_IN sadressInClient; int addLen = sizeof(SOCKADDR); char sendBuffer[128]; char recvBuffer[128]; while (1) { printf("server accept and process client's request/n"); SOCKET sockClient = accept(sockSvr,(SOCKADDR*)&sadressInClient,&addLen); memset(sendBuffer,128,0); memset(recvBuffer,128,0); sprintf(sendBuffer,"welcome %s to my blog/n",inet_ntoa(sadressInClient.sin_addr)); send(sockClient,sendBuffer,strlen(sendBuffer)+1,0);//send message to client recv(sockClient,recvBuffer,128,0);//receive message from client printf("come from client's message:%s/n",recvBuffer); closesocket(sockClient); } WSACleanup(); } TcpClient: #include <stdio.h> #include <WINSOCK2.H> void main() { //1.initiates use of Ws2_32.dll by process WORD wVersionRequested = MAKEWORD(1,1); WSADATA wsaData; if (WSAStartup(wVersionRequested,&wsaData)!=0) { printf("initiates failure/n"); WSACleanup(); return; } //2.create windows socket SOCKET socketClient = socket(AF_INET,SOCK_STREAM,0); if (socketClient==INVALID_SOCKET) { printf("create windows socket failure/n"); WSACleanup(); return; } printf("create windows socket success/n"); //3.connect to server SOCKADDR_IN sadressInSvr; sadressInSvr.sin_addr.S_un.S_addr = inet_addr("192.168.0.100"); sadressInSvr.sin_family = AF_INET; sadressInSvr.sin_port = htons(6000); if (connect(socketClient,(SOCKADDR*)&sadressInSvr,sizeof(SOCKADDR))!=0) { printf("connet to server failure/n"); WSACleanup(); return; } //4.process send and receive //recevie message from server char recvBuffer[128]; char sendBuffer[] = "hello,jiang"; recv(socketClient,recvBuffer,128,0); printf("come from server's message:%s/n",recvBuffer); send(socketClient,sendBuffer,strlen(sendBuffer)+1,0); closesocket(socketClient); }