The client process is as follows:
(1) Create socket (socket)
(2) Send a request to the server (connect)
(3) Communication with the server (SEND/RECV)
(4) Close socket
#include <Winsock2.h> #include <stdio.h> #pragma comment (lib, "Ws2_32.lib") int main () { w
ORD wversionrequested;
wsadata Wsadata;
int err;
wversionrequested = Makeword (1,1);
err = WSAStartup (Wversionrequested,&wsadata); if (Lobyte (wsadata.wversion)!=1 | | Hibyte (wsadata.wversion)!= 1) { wsacleanup (); &
nbsp; return 0;
 } //Create socket socket sockclient = socket (af_inet,sock_stream,0);
sockaddr_in addrsrv; addrsrv.sin_addr. S_un.
S_ADDR = inet_addr ("127.0.0.1");
addrsrv.sin_family = af_inet;
addrsrv.sin_port = htons (6000); //A connection request to the server connect (sockclient, (sockaddr*) &addrsrv,sizeof (sockaddR); The first parameter is the socket on which the connection is to be established, the second parameter sets the connection server-side address information, and the third information specifies the server-side address length //receive data char
RECVBUFF[100]; recv (sockclient,recvbuff,100,0); The first parameter represents a socket that has been connected, the second parameter points to a pointer to the buffer, holds the received information, and the third parameter represents the size of the buffer.
The fourth parameter is generally set to 0 printf ("%s\n", Recvbuff); //send information to the server send (sockclient, "This is Lisi", strlen ("It is Lisi") +1,0); The first parameter represents a socket that has been connected, the second parameter points to a buffer, contains the information to be passed, the third parameter is the length of the buffer, and the fourth parameter is typically set to 0 //close socket
Closesocket (sockclient);
wsacleanup ();
return 0; }
The server-side process is as follows:
(1) Create socket (socket)
(2) Binding sockets to a local address and port (BIND)
(3) Set socket to answer mode, prepare to receive customer request (listen)
(4) Waiting for the user to request, to receive the request, return a socket corresponding to this connection (accept)
(5) Communication with the returned socket and client (SNED/RECV)
(6) Return, wait for another client request
(7) Close socket
#include <Winsock2.h> #include <stdio.h> #pragma comment (lib, "Ws2_32.lib") int main () { w
ORD wversionrequested;
WSADATA wsaData;
int err; wversionrequested = Makeword (1,1);//makeword macros can get WSAStartup high and low err = WSAStartup (wversionrequested,&wsadata);//wsastartup is used to load sockets, wversionrequested specifies the version of the Winsock library to be loaded, the high point is the secondary version, Status refers to major version if (err!=0) { return 0; &
nbsp;} if (Lobyte (wsadata.wversion)!=1 | | Hibyte (wsadata.wversion)!= 1) { wsacleanup (); &
nbsp; return 0;  } //create sockets for answering socket socksrv = socket (af_inet,sock_stream,0) //The first parameter specifies the address family, for the TCP/IP protocol, only af_inet (Pf_inet), the second parameter specifies the socket type, SOCK_STREAM produces a streaming socket, Sock_dgram generates a datagram socket sockaddr_in addrsrv; addrsrv.sin_addr. S_un. S_ADDR = htonl (inaddr_any);//sin_addr Specify socket host IP,HTONL converts a u_long-type value from host byte order to TCP/IP network byte order Addrsrv.sin_family = af_inet;//sin_family Represents the address family, for IP address, will always be af_inet addrsrv.sin_port = htons (6000) ;//sin_port specifies the port that will be assigned to the socket, and the Htons function converts the value of a u_short type from host byte order to TCP/IP network byte order //binding socket bind (Socksrv, (sockaddr*) &addrsrv,sizeof (sockaddr));//The first parameter specifies the socket to bind, and the second parameter specifies the local address information for the socket. The third parameter specifies the length of the second parameter //sets the socket to listen mode, prepares to receive the client request listen (socksrv,5); The first argument is a socket descriptor.
The second parameter is the maximum length of the wait queue.
sockaddr_in addrclient;
int len = sizeof (SOCKADDR); while (1) { //waiting for customer request arrival & nbsp; socket sockconn = Accept (Socksrv, (sockaddr*) &addrclient,&len);//The first parameter is a socket descriptor, The socket has been set to the listening state through listen. The second parameter is a pointer to a buffer that is used to receive the connection realThe address //of the body is when the client initiates a connection to the server, the server side receives the connection, saves the IP address information and port information that initiates the connection to the client, and the third parameter returns a pointer to the reshaping
, returns the length of the containing address information char sendbuff[100]; sprintf (Sendbuff, "Welcome%s to http://www.sunxin.org", Inet_ntoa (
ADDRCLIENT.SIN_ADDR));
printf ("%s\n", Sendbuff); //Send data send (Sockconn,sendbuff,strlen ( Sendbuff) +1,0; The first parameter represents a socket that has been connected, the second parameter points to a buffer, contains the information that will be passed, the third parameter is the length of the buffer, and the fourth parameter is typically set to 0
char recvbuff[100]; //receive data recv (sockconn,recvbuff,100,0);//
The first parameter represents a socket that has been connected, the second parameter points to a pointer to the buffer, holds the received information, the third parameter represents the size of the buffer, and the fourth parameter is typically set to 0 //Print received data
printf ("%s\n", Recvbuff); //Close socket &NBSP;CLOsesocket (Sockconn);
  return 0; }
Customer Service End Interface:
Server-side interface: