Version of Windows Sockets:
Header file: WINSOCK.h
Library files: wsock32.lib
Dynamic Trap: WINSOCK.DLL
Header file: WINSOCK2.h
Library files: ws2_32.lib
Dynamic Trap: Ws2_32.dll
Service side:
Wsadatawsd;//wsadata variable socketsserver;//server socket socketsclient;//Client socket sockaddr_inaddrserv;;/ /server address charbuf[buf_size];//receive data buffer intretval;//return value//Initialize socket dynamic Library if (WSAStartup (Makeword (2,2), &WSD)! = 0) {printf (" WSAStartup failed!! "); AfxMessageBox ("WSAStartup Failed"); return 1;} Create Socket sserver = socket (af_inet,sock_stream,ipproto_tcp), if (Invalid_socket = = sserver) {printf ("Socket failed!\n"); AfxMessageBox ("Socket failed!"); WSACleanup ();//release socket return-1;} Next bind socket//addrserv.sin_family = Af_inet;addrserv.sin_port = Htons (4999); addrServ.sin_addr.s_addr = inaddr_any;// Bind socket RetVal = Bind (Sserver, (LPSOCKADDR) &addrserv,sizeof (sockaddr_in)); if (socket_error = = RetVal) {AfxMessageBox ("Bind failed!"); Closesocket (sserver); Close socket WSACleanup ();//Release socket resource return-1;} Start monitoring RetVal = Listen (sserver,1), if (socket_error = = RetVal) {AfxMessageBox ("Listen failed!"); Closesocket (sserver); WSACleanup (); return-1;} Set the server to listen, call the Accept () function to wait for the client to connect. The function is blocked until a client request is accepted, a new socket is created when the function returns, and the address of the client is returned. If the call//failure is off, the Sockaddr_in addrclient;int addrclientlen = sizeof (addrclient); sclient = Accept (Sserver, (sockaddr far*) &addrclient, &addrclientlen), if (Invalid_socket = = sclient) {AfxMessageBox ("accept failed!"); Closesocket (sserver); WSACleanup (); return-1;} Accepts and displays data ZeroMemory (buf,buf_size), RetVal = recv (sclient,buf,buf_size,0), if (socket_error = = RetVal) {AfxMessageBox (" Recv Failed "); closesocket (sserver); closesocket (sclient); WSACleanup (); return-1;} CString Str;str. Format ("%s", buf);//afxmessagebox (BUF); M_recv2.setwindowtext (str); closesocket (sserver);//Close Socket closesocket ( sclient);//Close Socket WSACleanup ();//release socket resource;
Client:
Wsadatawsd;//wsadata variable socketshost;//server socket sockaddr_inservaddr;//Server address charbuf[buf_szie];//receive data buffer intretval;// return value//Initialize socket dynamic Library if (WSAStartup (Makeword (2,2), &WSD)! = 0) {printf ("WSAStartup failed!\n"); return-1;} Create Socket Shost = socket (af_inet, Sock_stream, ipproto_tcp), if (Invalid_socket = = shost) {printf ("Socket failed!\n"); WSACleanup ();//Release socket resource return-1;} Set the server address servaddr.sin_family =af_inet;servaddr.sin_addr.s_addr = inet_addr ("127.0.0.1"); Servaddr.sin_port = Htons (( short) 4999); Intnservaddlen = sizeof (SERVADDR);//Connection Server Retval=connect (Shost, (lpsockaddr) &servaddr, sizeof ( SERVADDR)), if (socket_error = = RetVal) {printf ("Connect failed!\n"); closesocket (shost);//Close Socket WSACleanup ();// Release socket resource return-1;} Send data to the server ZeroMemory (buf, Buf_szie); strcpy (buf, "mytcp"); retVal = Send (Shost, buf, strlen (BUF), 0); if (Socket_error = = RetVal) {printf ("Send failed!\n"); closesocket (shost);//Close Socket WSACleanup ();//Release socket resource return-1;} Exit closesocket (Shost);//Close Socket WSACleanup ();//Release socket resource return 0;
Getting Started with Windows networking programming