Socket:
The socket interface is between the application and hardware. The understanding of socket can be simplified as follows: it is a soft connection that encapsulates the data stream from the machine to the machine. Through this soft connection, and with the help of the sending and receiving programs at both ends of the line, information exchange and intercommunication are achieved between machines on the network. Applications (server programs and client programs) separated at both ends of the soft line can be used to develop program applications with TCP/IP network functions by calling the socket interface.
As a client, you must know the room number (IP address) of the room where the service program is located, and the location (port) of the backdoor ). In this way, you can enter the room correctly.
Data transmission:
After the soft line is established, there must be data transmission rules. This is what the server and client discuss. What encapsulation is used for data, what rules (sending and receiving protocols) are followed during transmission, and whether compression and encryption/Decryption technologies are used for transmission. Encapsulating data allows program designers to operate information flowing on the network like local files. Different Data encapsulation methods determine the data access methods on the client and server. The encoding type is limited to scalar (basic type) and string object. When copying objects, we need to consider the serialization of objects to bytes and the restoration of byte streams to objects.
Multi-Programming
As a routine service, it must be considered to serve multiple users at the same time. This problem can be solved through the thread technology, and it is quite simple. The basic idea is to generate a single listening socket in the server and call accept () cyclically to wait for a new soft-line connection. Each time an accept () is returned, it will bring back a socket related to a specific customer, and arrange the operation of the socket to an independent thread, so that its service is targeted at a specific customer. Considering that the synchronization problem of multi-thread access to shared resources may cause thread insecurity if improperly handled, thread locks must be set for the critical section.
TCP-based receiving and receiving protocols
To use socket programming, you must specify the sending and receiving protocols between the client and the server. The following are the sending and receiving protocols defined for this instance:
Client: sending header 9999, sending username (string), sending password (string)
Server: receives header 9999, receives user names and passwords, and verifies logon permissions.
Valid user: Send header 9999, send Welcome Message (string)
Illegal user: Send header-1, send warning information (UTF)
Client: receives header 9999 and obtains the welcome information (string)