Concept of socket:
Socket is a socket used to describe the address and port. It is a communication chain handle.
ApplicationProgramSend a request or response through a socket like network.
What is the customer/Server mode:
In TCP/IP network applications, the main mode of interaction between two processes is the customer/Server mode, that is, when the client sends a request to the server, the server receives the request and provides the corresponding service. The customer/Server mode is based on the following two points:
1:The reason for establishing a network is that the hardware and software resources, computing power, and information in the network are not equal and need to be shared, so that hosts with many resources can provide services, the non-peering Effect of customer request services with fewer resources
2:Inter-network process communication is completely asynchronous. The Inter-communication process does not have a parent-child relationship, but does not share the memory buffer.
Therefore, a mechanism is required to establish a connection between processes that want to communicate with each other and synchronize the data exchange between them. This mechanism is based on the Client/Server mode of TCP/IP.
(This part is taken from the socket programming principle)
What to do:
Client: Establish a socket, identify the target server through the port number and address, connect to the server using connect, send messages, wait for processing, and then call closesocket to close the socket
Server: Create a socket, declare its own port number and address, bind it to the socket, use listen to open the listener, and then constantly use accept to check whether a connection exists. If yes, capture the socket, obtain the message content through Recv. After the communication is complete, call closesocket to close the socket from the corresponding accept. If you no longer need to wait for any client connection, use closesocket to close your socket.
CodeExample
Client: Create a clientnet class to indicate a customer.
# Pragma once/* clinetnet. h: used to indicate a customer's Network Operations */# include <stdio. h> # include <windows. h> # pragma comment (Lib, "ws2_32.lib") Class cclientnet {public: // specifies the server int connect (INT port, const char * address) connected ); // send message int sendmsg (const char * MSG, int Len); // close void close (); Private: Socket m_sock ;};
/* Clientnet. CPP */# include "clientnet. H "int cclientnet: connect (INT port, const char * address) {int rlt = 0; // used to record error information and output int ierrmsg; // start winsockwsadata wsadata; ierrmsg = wsastartup (makeword (1,1), & wsadata); If (ierrmsg! = No_error) // error {printf ("failed with wsastartup error: % d \ n", ierrmsg); rlt = 1; return rlt ;} // create socketm_sock = socket (af_inet, sock_stream, ipproto_tcp); If (m_sock = invalid_socket) // failed to create socket {printf ("socket failed with error: % d \ n ", wsagetlasterror (); rlt = 2; return rlt;} // target server data sockaddr_in sockaddrserver; sockaddrserver. sin_family = af_inet; sockaddrserver. sin_port = port; sockaddrserver. sin_addr.s_addr = inet_addr (Address); // connection, sock connects to the target server ierrmsg = connect (m_sock, (sockaddr *) & sockaddrserver, sizeof (sockaddrserver )); if (ierrmsg <0) {printf ("Connect failed with error: % d \ n", ierrmsg); rlt = 3; return rlt;} int cclientnet :: sendmsg (const char * MSG, int Len) {int rlt = 0; int ierrmsg = 0; // send a message, specify sock to send the message ierrmsg = Send (m_sock, MSG, Len, 0); If (ierrmsg <0) // failed to send {printf ("Send MSG failed with error: % d \ n", ierrmsg); rlt = 1; return rlt ;} return rlt;} void cclientnet: Close () {closesocket (m_sock );}
# Include "clientnet. H "Void main () {cclientnet client; // connect to the server printf (" connect \ n ") with the port number of 8888 (local); client. connect (8888, "127.0.0.1"); // send the message printf ("Send MSG \ n"); client. sendmsg ("Hello \ 0", sizeof ("Hello \ 0"); // close socketprintf ("close \ n"); client. close (); System ("pause ");}
Server code:
# Pragma once/* servernet. h indicates network operations on the server */# include <stdio. h> # include <Winsock. h> # pragma comment (Lib, "ws2_32.lib") Class cservernet {public: // initialize the server. If 0 is returned, int Init (const char * address, int port) is successful ); // update the data void run (); Private: Socket m_sock ;};
/* Servernet. CPP */# include "servernet. H "int cservernet: Init (const char * address, int port) {int rlt = 0; // used to record error information and output int ierrormsg; // initialize winsockwsadata wsadata; ierrormsg = wsastartup (makeword (1,1), & wsadata); If (ierrormsg! = No_error) {// printf ("wsastartup failed with error: % d \ n", ierrormsg); rlt = 1; return rlt ;} // create server socketm_sock = socket (af_inet, sock_stream, ipproto_tcp); If (m_sock = invalid_socket) {// create socket exception printf ("socket failed with error: % d \ n ", wsagetlasterror (); rlt = 2; return rlt;} // declare sockaddr_in serveraddr; serveraddr. sin_family = af_inet; serveraddr. sin_port = port; serveraddr. sin_addr.s_addr = Inet_addr (Address); // bind ierrormsg = BIND (m_sock, (sockaddr *) & serveraddr, sizeof (serveraddr); If (ierrormsg <0) {// failed to bind printf ("bind failed with error: % d \ n", ierrormsg); rlt = 3; return rlt ;}return rlt ;} void cservernet :: run () {// publicly connect listen (m_sock, 5); sockaddr_in tcpaddr; int Len = sizeof (sockaddr); socket newsocket; char Buf [1024]; int rval; do {// receive information newsocket = accept (m_sock, (sockaddr *) & tcpaddr, & Len); If (n Ewsocket = invalid_socket) {// non-available socket} else {// new socket connection printf ("new socket CONNECT: % d \ n", newsocket ); // message processing do {printf ("Process \ n"); // receives data from memset (BUF, 0, sizeof (BUF); rval = Recv (newsocket, Buf,, 0); If (rval = socket_error) // This should be an exception. When the client directly exits the game without calling closesocket, printf ("Recv socket error \ n"); If (rval = 0) // If Recv returns 0, it indicates that printf ("ending connection") is exited normally "); else // display the received data printf ("Recv % s \ n", Buf);} while (Rval! = 0); // close socketclosesocket (newsocket);} while (1); // close its own socketclosesocket (m_sock );}
# Include "servernet. H "int main (INT argc, char ** argv) {cservernet servernet; int irlt = servernet. init ("127.0.0.1", 8888); If (irlt = 0) {printf ("init OK... \ n "); servernet. run ();} elseprintf ("servernet init failed with error: % d \ n", irlt); System ("pause ");}
# Pragma once/* clinetnet: used to represent a customer's Network Operations */# include <stdio. h> # include <windows. h> # pragma comment (Lib, "ws2_32.lib") Class cclientnet {public: // specifies the server int connect (INT port, const char * address) connected ); // send message int sendmsg (const char * MSG, int Len); // close void close (); Private: Socket m_sock ;};