Socket programming foundation
This chapter is based on Berkeley Socket, mainly introduces the calls and programs used by network programming and procedures to use their methods and basic structures. Network programming has two main programming interfaces, one is the Socket programming interface of Berkeley UNIX (BSD UNIX), and the other is the TLI interface of AT & T (for UNIXSYSV).
1, TCP / IP basic knowledge
Here first assumes that the reader has a certain understanding of the ISI seven-layer model of ISO. Let's take a look at the TCP / IP model. ISO's OSI's concept is very clear about the concept of service, interface and protocol, but it has no real user group. The TCP / IP model is not as clarity as the concept of the OSI model, but very practical. The TCP / IP model is divided into four layers, corresponding to the OSI seven-layer model as shown below: Figure 6-1 TCP / IP reference model and OSI model approximate correspondence in TCP / IP model, the Internet layer is based on unconnected interconnection Packet switched network of the network layer. In this layer, the host can send a message to any network, and the packet is independently transmitted to the target. The Internet layer defines the format and protocol of the packet, which is the IP protocol (Internet Protocol). The function of the Internet layer is to send the packet to the destination, the main design problem is that the routing and avoidance of the message. The Internet layer is the transport layer, the main function of the layer, like this layer of the OSI model, mainly to sessions between the source and destination hosts. This layer defines two end-to-end protocols, one is a connection-oriented transmission control protocol TCP, and the other is an unconnected user datagram protocol UDP. There is no session layer and representation in the TCP / IP protocol model. The transfer layer is the application layer, which contains all high-level protocols, such as remote virtual terminal protocol Telnet, file transfer protocol FTP, simple mail transfer protocol SMTP, etc. These high-level protocols are common to the Telnet protocol to allow users to log in to another UNIX machine; FTP protocol is used to transfer files, commonly available with Wu-ftp (Washington University's FTP server-side program, is a free program) The SMTP protocol is used to transmit email, and the common server-side program has a program made by companies such as Netscape, and also has free use Sendmail programs; there are domain name system service DNS protocols, news group transfer protocol NNTP, Hypertext Transfer Protocol for WWW HTTP, etc. The main unit to the network, there is no detailed definition in the TCP / IP model, which is not introduced.
2, Socket General Description
Due to more and more computer manufacturers, especially workstation manufacturers such as Sun, Socket interfaces are widely adopted, so that the Socket interface is widely recognized and has become a factual industry standard. Current Sysv, BSD, OSF will use the socket interface as part of the system. When designing how to support the TCP / IP protocol, there are two ways to join the function. One is to directly join the call to support the TCP / IP protocol, and the other is to join the function of supporting the general network protocol, and use parameters to specify support TCP / IP protocol. Berkeley adopted the latter, which supports multi-protocols, TCP / IP is one of the protocols (PF_INET).
2.1 Socket Descriptor
As mentioned earlier, in UNIX, the process is to operate on the file, usually using the Open call to open a file for access, each process has a file descriptor table, which is stored in the open file descriptor. The file descriptor obtained by the user using Open is actually the index number of the file descriptor in the table, which is a pointer to the file table. The application can operate with the specified file as long as the descriptor is used. Similarly, the Socket interface adds an abstract definition of network communication operations. Like the file operation, each open socket corresponds to an integer. We call it a socket descriptor, which is also an index in the file descriptor in the file descriptor. value. However, the table item in the descriptor table does not point to the file table, but points to a data structure associated with the socket. BSD UNIX adds a socket call. The application can call it to create a Socket descriptor, and the process can only generate a file descriptor with OPEN without generating a Socket descriptor. Socket calls can only complete part of the work of establishing communication. Once a socket is established, the application can use other specific calls to add additional details to complete the process of establishing communication. 2.2 Understanding Socket Using Network Programming From Conceptually Understanding of Socket is Client / Server Mode.
When programming with this mode, the server has a process (or multiple processes) waiting for the customer to connect, the service program waits for the customer's connection information, and after the connection is connected, it can be performed according to the design data exchange method and format. data transmission. The client issues a connection request to the server at the time required.
Here, these calls and their rough functions are mentioned in order to facilitate understanding. After using the socket call, only one Socket descriptor can be used, and the communication cannot be performed, but also other calls, so that the information used in the structure referred to as Socket is completed. When using the TCP protocol, the general server process first uses the socket call to get a descriptor, then connect a name with the socket descriptor using the bind call, and for the Internet address to the socket. After that, the server uses the Listen call to indicate the length of the waiting service request queue. Then you can use the Accept call waiting to wait for the client to initiate a connection (generally blocking waiting connection, the back chapter tells the non-blocking mode), once the client issues a connection, Accept returns the customer's address information and returns a new Socket Description The descriptor has the same characteristics as the original socket, where the server can use this new Socket to read and write. The general server may create a new process after Accept returns to communicate with the customer, and the parent process is waiting to wait for another connection at the Accept call. The client process generally uses the socket call to get a socket descriptor, then initiate a connection to the specified port on the specified server, once the connection is successfully returned, the connection with the server has been established, then the connection can be described by Socket The functions are read and written. Below is the process of using the system call when the client and the server use TCP.
When using TCP's client and server uses system calls to use the unconnected UDP protocol, the server process creates a socket, then call the RecVFrom Receive the client's datagram, and then call Sendto to return to the client's message. The client must also create a socket first, then use Sendto to send a request to the server process, and get the return message using Recvfrom.