References: 1. windows socket programming 2. Unlocking the veil of socket programming
The general process of socket TCP programming in Windows is as follows: Server Side 1. initialize the Windows Socket library. 2. Create a socket. 3. Bind the socket. 4. Listen. 5. Accept. 6. Send data. client 1. initialize the Windows Socket library. 2. Create a socket. 3. Connection. 4. Send data.
In the following example, the server accepts client connections and creates a thread to receive and output data. The client connects to the server and sends data to the server. That is, the client sends data to the server. The server creates a thread for each client connection to receive and output data. When the connection ends, the corresponding thread ends.
Server. cpp
# Include <iostream> # include <windows. h> # include <process. h> using namespace STD; # pragma comment (Lib, "ws2_32.lib") # define port_no 6000 # define backlog 10 unsigned _ stdcall clientthrd (void * lpparam) {socket sockclient = (socket) lpparam; int ret = 0; char recvbuffer [max_path] = {0}; // receives data ------ while (1) {memset (recvbuffer, 0, sizeof (recvbuffer); ret = Recv (sockclient, recvbuffer, sizeof (recvbuffer), 0); If (Ret = 0) {cout <"connection has been gracefully closed. "<Endl; break;} else if (ret = socket_error) {cout <" connection has been closed ungracefully. "<Endl; break;} cout <" receive data from client: "<recvbuffer <Endl;} closesocket (sockclient); Return 0 ;} int main (INT argc, char * argv []) {wsadata; int ret; Word wversionrequested = makeword (2, 2); socket socksrv, sockconn; sockaddr_in Addrsrv, addrclient; handle hthread = NULL; // initialize Windows Socket ------ ret = wsastartup (wversionrequested, & wsadata); If (Ret! = 0) {cout <"wsastartup () failed:" <wsagetlasterror () <Endl; Return-1 ;}// create socket ------ socksrv = socket (af_inet, sock_stream, 0); If (invalid_socket = socksrv) {cout <"socket () failed:" <wsagetlasterror () <Endl; wsacleanup (); return-1;} addrsrv. sin_addr.s_un.s_addr = htonl (inaddr_any); addrsrv. sin_family = af_inet; addrsrv. sin_port = htons (port_no); // bind socket ------ ret = BIND (socksrv, (sockaddr *) & addrsrv, sizeof (sockaddr); If (socket_error = RET) {cout <"BIND () failed:" <wsagetlasterror () <Endl; closesocket (socksrv); wsacleanup (); Return-1 ;} // listen ------ ret = listen (socksrv, backlog); If (socket_error = RET) {cout <"Listen () failed:" <wsagetlasterror () <Endl; closesocket (socksrv); wsacleanup (); Return-1 ;}cout <"server started ...... "<Endl; // accept ------ int Len = sizeof (sockaddr); While (1) {sockconn = accept (socksrv, (sockaddr *) & addrclient, & Len ); if (invalid_socket = sockconn) {cout <"accept () failed:" <wsagetlasterror () <Endl; break;} hthread = (handle) _ beginthreadex (null, 0, clientthrd, (void *) sockconn, 0, null); If (hthread = 0) {cout <"_ beginthreadex () failed. "<Endl; break;} closehandle (hthread) ;}// clear resources ------ closesocket (socksrv); wsacleanup (); cout <" exit... "<Endl; return 0 ;}
Client. cpp
# Include <iostream> # include <windows. h> using namespace STD; # pragma comment (Lib, "ws2_32.lib") # define port_no 6000 # define srv_ip_addr "127.0.0.1" int main (INT argc, char * argv []) {int ret; wsadata; Word wversionrequested = makeword (2, 2); socket sockclient; sockaddr_in addrsrv; // initialize Windows Socket ------ ret = wsastartup (wversionrequested, & wsadata ); if (Ret! = 0) {cout <"wsastartup () failed:" <wsagetlasterror () <Endl; Return-1 ;}// create socket ------ sockclient = socket (af_inet, sock_stream, 0); If (invalid_socket = sockclient) {cout <"socket () failed:" <wsagetlasterror () <Endl; wsacleanup (); return-1;} addrsrv. sin_addr.s_un.s_addr = inet_addr (srv_ip_addr); addrsrv. sin_family = af_inet; addrsrv. sin_port = htons (port_no); // connection ------ ret = connect (sockclient, (sockaddr *) & addrsrv, sizeof (sockaddr); If (socket_error = RET) {cout <"Connect () failed:" <wsagetlasterror () <Endl; closesocket (sockclient); wsacleanup (); Return-1 ;} else {cout <"Connect () successfully. "<Endl;} // send data ------ char sendbuf [max_path] = {0}; while (1) {cin. getline (sendbuf, sizeof (sendbuf); If (strcmp (sendbuf, "exit") = 0) {break;} ret = Send (sockclient, sendbuf, strlen (sendbuf) + 1, 0); If (socket_error = RET) {cout <"Send () failed:" <wsagetlasterror () <Endl; closesocket (sockclient ); wsacleanup (); Return-1 ;}// clear resources ------- closesocket (sockclient); wsacleanup (); cout <"exit... "<Endl; return 0 ;}