1. Server code
#include <Windows.h>//ws2_32.lib corresponds to Ws2_32.dll, providing support for network-related APIs#pragmaComment (lib, "Ws2_32.lib")int_tmain (intARGC, _tchar*argv[]) { /*typedef struct WSADATA {WORD wversion; WORD whighversion; #ifdef _win64 unsigned short imaxsockets; unsigned short IMAXUDPDG; char FAR * LPVENDORINFO; Char szdescription[wsadescription_len+1]; Char szsystemstatus[wsasys_status_len+1]; #else Char szdescription[wsadescription_len+1]; Char szsystemstatus[wsasys_status_len+1]; unsigned short imaxsockets; unsigned short IMAXUDPDG; char FAR * LPVENDORINFO; #endif} wsadata, FAR * lpwsadata; */Wsadata Wsadata; if(WSAStartup (Makeword (2,2), &wsadata)! =0) { /*WORD Makeword (Byte BLow,//Specifies the low-byte order of the new variable; byte bhigh//Specifies the high-byte order of the new variable;); typedef unsigned short WORD; WORD Temvalue = Makeword (0x12, 0x34); Temvalue = 0x3412*/ /*WSAStartup: Return 0 for Success WSAStartup should be paired with WSACleanup to use the WSAStartup function is to initialize the Winsock DLL This function must be an application or DLL The first Windows Sockets function that is called. It allows the application or DLL to indicate the version number of the Windows Sockets API and to get the details of a specific Windows Sockets implementation. An application or DLL can call further Windows Sockets API functions only after a successful WSAStartup () call. */ /*wsadata.wversion = 0x0202 wsadata.whighversion = 0x0202 wsadata.szdescription = 0x0046f2c4 "WinS Ock 2.0 "wsadata.szsystemstatus = 0x0046f3c5" Running "*/ return-1; } if(Lobyte (wsadata.wversion)! =2|| Hibyte (wsadata.wversion)! =2) { /*unsigned short value0 = 0x1234; unsigned short temvalue0 = Lobyte (value0),//temvalue0 = 0x0034 unsigned short temvalue1 = Hibyte (VALUE0);//temvalue 1 = 0x0012 LoWord () get a 32bit number of low 16bit hiword () get a 32bit number of high 16bit lobyte () get a 16bit number lowest (rightmost) that byte Hibyte () gets a 16bit number highest (leftmost) that byte*/WSACleanup (); /*WSACleanup is to unbind the socket library and release the system resources occupied by the socket library. The WSACleanup operation successfully returned a value of 0, otherwise the return value is socket_error in a multithreaded environment, WSACleanup aborts the operation of Windows Sockets on all threads. */ return-1; } //typedef uint_ptr SOCKET; //typedef _w64 unsigned int uint_ptr, *puint_ptr;Socket SOCKFD = socket (af_inet, Sock_stream,0); /*socket (): The first parameter indicates the protocol family, mainly for the second parameter af_inet,af_inet6,af_local, such as specifying the socket type, mainly Sock_stream,sock_dgram,sock_raw, etc. The third parameter is typically 0 for a Windows environment, a small nonnegative integer value is returned, similar to a file descriptor, when the function needs to be called before calling the WSAStartup function to complete initialization of the Winsock service successfully. When failed, return Invalid_socket*/ //Af_inet (also known as pf_inet) is the socket type of the IPV4 network protocol, AF_INET6 is IPV6, and Af_unix is the UNIX system local communication//#define Invalid_socket (SOCKET) (~0) if(Invalid_socket = =sockfd) {WSACleanup (); return 0; } //typedef struct SOCKADDR_IN sockaddr_in; /*struct SOCKADDR_IN {short sin_family; U_short Sin_port; struct IN_ADDR sin_addr; Char Sin_zero[8]; }; typedef struct IN_ADDR {union {struct {UCHAR s_b1,s_b2,s_b3,s_b4;} S_un_b; struct {USHORT s_w1,s_w2;} S_un_w; ULONG s_addr; } S_un; #define S_ADDR S_un. S_ADDR//Can is used for most TCP & IP code #define S_HOST s_un. S_UN_B.S_B2//Host on IMP #define S_NET S_un. S_UN_B.S_B1//Network #define S_IMP S_un. S_UN_W.S_W2//IMP #define S_IMPNO S_un. S_UN_B.S_B4//Imp # #define S_LH S_un. S_UN_B.S_B3//Logical host} in_addr, *pin_addr, far *lpin_addr; */sockaddr_in seraddr= {0}; //#define AF_INET 2//internetwork:udp, TCP, etc.seraddr.sin_family =af_inet; //u_short PASCAL far htons (__in u_short hostshort); //htons Converting unsigned short shapes of a host to network byte order//byte order is the big end in front or small end in front is dependent on the CPU and not depending on the operating system, the native is the small terminal mode//network byte order: is the big endian byte order. Specifies that the network byte order is used for communication between different systems. //unsigned short nvalue = htons (9000); //Nvalue = 10275 9000 for 0x2328 10275 for 0x2823Seraddr.sin_port = htons (9000); /*htonl ()--"host to Network Long" Ntohl ()--"Network to Host Long" htons ()--"host to Network Short" ntohs ()- -"Network to Host short"*/ //the function of inet_addr () is to convert a dot-decimal IP to a long integer (u_long type)//seraddr.sin_addr. S_un. s_addr = 0x0100007f 1 0 0 127Seraddr.sin_addr. S_un. S_ADDR = inet_addr ("127.0.0.1"); /*struct SOCKADDR is a universal socket address, while struct sockaddr_in is the address form of a socket in the Internet environment, both of which are 16 bytes in length. The two are parallel structures, and pointers to sockaddr_in structures can also point to sockaddr. In general, it is necessary to cast the SOCKADDR_IN structure into the SOCKADDR structure and re-pass it into the system call function. typedef struct SOCKADDR {#if (_win32_winnt < 0x0600) U_short sa_family; #else address_family sa_family; Address family. #endif//(_win32_winnt < 0x0600) CHAR sa_data[14]; Up to bytes of direct address. } sockaddr, *psockaddr, far *lpsockaddr; */ if(Socket_error = = Bind (SOCKFD, (sockaddr*) (&SERADDR),sizeof(SOCKADDR))) { /*bind: Assign a local protocol address to a socket the first parameter is a socket the second parameter is a pointer to the protocol-specific address structure The third parameter is the length of the address structure */closesocket (SOCKFD); WSACleanup (); return-1; } if(Listen (SOCKFD,Ten) !=0) { /*Create a socket interface with listen () and set up a fallback log for the connection requested, and then accept the connection by accepting (). Listen () applies only to socket interfaces that support connections, such as the Sock_stream type. The socket interface is in a "change" mode, and the request for incoming connection requests is confirmed and queued for acceptance. This function is especially useful for servers with multiple connection requests at the same time, and if the queue is full when a connection request arrives, the customer will receive a wsaeconnrefused error. If no error occurs, listen () returns 0. Otherwise, return-1*/closesocket (SOCKFD); WSACleanup (); return-1; } sockaddr_in addrclient; intLen =sizeof(SOCKADDR); SOCKET Clientsocket= Accept (SOCKFD, (sockaddr*) &addrclient, &Len); if(Invalid_socket = =clientsocket) {closesocket (SOCKFD); WSACleanup (); return-1; } if(Send (Clientsocket,"1234",4,0) <=0) {closesocket (SOCKFD); Closesocket (Clientsocket); WSACleanup (); return-1; } Charbuff[1024x768] = {0}; if(Recv (Clientsocket, Buff,4,0) <=0) {closesocket (SOCKFD); Closesocket (Clientsocket); WSACleanup (); return-1; } printf ("%s\n", Buff); System ("Pause"); //releases the socket interface descriptor, and closesocket () returns 0 if no error occurs. Otherwise, return socket_error errorclosesocket (SOCKFD); Closesocket (Clientsocket); //Note: All open sockets need to be closedWSACleanup (); return 0;}
2. Client code
#include <windows.h>#pragmaComment (lib, "Ws2_32.lib")int_tmain (intARGC, _tchar*argv[]) {Wsadata wsadata; if(WSAStartup (Makeword (2,2), &wsadata)! =0) { return-1; } if(Lobyte (wsadata.wversion)! =2|| Hibyte (wsadata.wversion)! =2) {wsacleanup (); return-1; } SOCKET sockclient= Socket (Af_inet, Sock_stream,0); if(Invalid_socket = =sockclient) {WSACleanup (); return-1; } sockaddr_in sockaddr= {0}; Sockaddr.sin_family=af_inet; Sockaddr.sin_port= Htons (9000); Sockaddr.sin_addr. S_un. S_addr= Inet_addr ("127.0.0.1"); //Connect successfully returns 0, failure returns 1, and the cause of the error is stored in errno. if(Socket_error = = Connect (sockclient, (sockaddr*) &sockaddr,sizeofsockaddr)) { //Connect will wait for a while while the connection is in progress, failing to return if no connection is establishedclosesocket (sockclient); WSACleanup (); return-1; } Charbuff[1024x768] = {0}; if(Recv (sockclient, Buff,4,0) <=0) {closesocket (sockclient); WSACleanup (); return-1; } printf ("%s\n", Buff); if(Send (Sockclient,"1234",4,0) <=0) {closesocket (sockclient); WSACleanup (); return-1; } System ("Pause"); Closesocket (sockclient); WSACleanup (); return 0;}
The simplest example of a TCP connection