#include"stdafx.h"#include<Winsock2.h>#include<iostream>#pragmaComment (lib, "Ws2_32.lib")using namespacestd;voidMain () {interr; WORD versionrequired; Wsadata Wsadata; Versionrequired=makeword (1,1); Err=wsastartup (Versionrequired,&wsadata);//version information for the Protocol library if(!err) {printf ("The client nested word has been opened!\n"); } Else{printf ("the client's nested word open failed!\n"); return;//End} SOCKET Clientsocket=socket (Af_inet,sock_stream,0); Sockaddr_in clientsock_in; Clientsock_in.sin_addr. S_un. S_addr=INET_ADDR ("127.0.0.1"); Clientsock_in.sin_family=af_inet; Clientsock_in.sin_port=htons (8001); //bind (Clientsocket, (sockaddr*) &clientsock_in,strlen (sockaddr));//Note the third parameter//Listen (clientsocket,5);Connect (Clientsocket, (sockaddr*) &clientsock_in,sizeof(SOCKADDR));//Start Connection Charreceivebuf[ -]; Recv (Clientsocket,receivebuf,101,0); printf ("%s\n", RECEIVEBUF); //Send (Clientsocket, "Hello,this is the client", strlen ("Hello,this is Client") +1,0);Send (Clientsocket,"123", strlen ("123")+1,0); Charreceivebuff[ -]; Recv (Clientsocket,receivebuff,101,0); printf ("%s\n", Receivebuff); Closesocket (Clientsocket); WSACleanup ();}
Tcp-client-c++