"Linux" ... Let Linux are more charm!
Socket Programming Introduction
Author: Pedro Paulo Ferreira Bueno and Antonio Pires de Castro Junior
Translator: Xiaoyu
Introduction
Most operating systems now provide a compiled network communication program. The most common example in the TCP / IP range is the web client (browser) and web server, as well as the client and server of FTP and Telnet. In most cases, we use these tools on the Internet without considering how it works. In order to better understand this problem, our development group (GTI, Grupo de Tecnologia EM ICA) decided to write a network application. It is a mini dialogue that can use some basic Socket structures, similar to the program interface, and its mechanism involves network communication functions as much as possible.
We use socket to detect the communication function of the network. Socket is the ultimate socket of two-way communication with another process, and Socket is the basic mechanism of IPC and heterocular networks of Berkeley's IPC and heterocular networks in UNIX. In order to better understand certain structures of this topic, you need to in-depth understanding of your computer system and its network protocol. The content of this article can be used as a guided article of computer programmers.
Socket function
Most web applications can be divided into two parts: clients and server.
Create a socket
#include
#include
You need to specify three main parameters when you create a socket:
Domain domain TYPE type and protocol protocol
INT Socket (int Domain, int type, int protocol);
The domain parameter specifies the domain range of the communication existing. In our example, it is AF_INET, which specifies the ARPA Internet protocol, the type parameter specifies the semantics of the communication, and the stream used in this mini-conversation (stream) socket, type ( SOCK_STREAM, which provides two-way, reliable, byte-based dual connection (reference data 2). Finally, the type of agreement is that we choose a streaming socket type, so you must choose a protocol that provides a connection, such as IP, so we decide that the protocol type is IP, and we can view in / etc / protocols To the IP address, 0. Now we create this:
S = Socket (AF_INET, SOCK_STREAM, 0)
's' is the file descriptor returned by the socket function.
Since our mini-talk program is divided into two parts, it will be explained separately in the following content and compare them different.
Mini dialog server structure
Bind Socket to port waiting access
Like all TCP / IP-based network services, Socket usually combine with ports, just as Telnet occupies port 23, FTP occupies 21 ... In our server, we must do this, bind port is ready to detect Listen to the connection, (this is the basic difference between the client and the server side), binding to specify the protocol port that the Socket defending is waiting for the message.
The problem has appeared, which port we bind on the new service? The system pre-defines a large number of ports, from 1 to 7000 (the ports that occupy some of the ports in / etc / services). We chose the 15,000 port.
BIND function description:
Int Bind (int S, struct sockaddr * addr, int addrlen)
The necessary structure of the Socket starts is a sockaddr_in address; then press the content listed to report the information of Socket to the system.
Socket Type Address.sin_Family = AF_INET / * USE A Internet Domain * /
IP address used
Address.sin_addr.s_addr = INADDR_ADDR = INADDR_ANADD * USE A Specific IP of Host * /
The port used
Address.sin_Port = HTONS (15000); / * Use a specific port number * /
Follow the binding port to socket
Bind (create_socket, (struct socmeddr *) & address, sizeof (address);
Another important content is that Socket is ready to listen to the information from the client, and the Listen function is used to receive communication-oriented access, and the number of access is limited (reference data 3).
Listen (Create_Socket, MaxNumber)
The maximum number of access we set is 3. As the final content, we need to tell the server to receive access using the accept () function. ACCEPT is used in a Socket stream connection.
Accept (Create_Socket, (Struct SockAddr *) & address, & address;
As we see in Listing 2, the (create_socket) parameter is the descriptor of the primary socket, the following parameters are sockeaddr_in and structural size (reference information 3)
Mini dialogue client structure
The client needs to perform the connect () function, maybe this is the main difference with the server. Connect operation is used to connect to the server, if possible, start connecting to the server.
Connect (Create_Socket, (Struct SockAddr *) & address, sizeof (address);
Common structure
The public structure of the client and server-side is listed in Listing 1 and 2. There are also Send and RECV functions and public code.
The Send () function sends the contents in the buffer to the server.
Send (New_Socket, Buffer, Bufsize, 0);
The RECV () function is used to receive the contents in the server and put it in the buffer, which can see this function in the client and server side.
Recv (New_Socket, Buffer, Bufsize, 0);
Conclusion conclusion
The TCP / IP protocol in the operating system is built-in, the application, and the specific content of the TCP / IP protocol interface, the detailed information on the operation system (reference data 4). And we use the UNIX BSD Socket interface because Linux also follows this specification. The program that is listed here is how simple is to develop Socket-based applications under Linux, or no reprogram-based models based on Client / Server models. More content related to IPC (process communication), fork, thread (reference data 5) is easily understood after understanding these contents. The basic steps that let them start work are:
Running the server is very fascinating within the server range, what do you think?
This is the beginning of our server project procedure, is a network management program. The following is the source code:
Client.c server.c
Resources Reference
Operating Systems, Harvey M. Deitel, 1990 Socket Linux Man Page Network Functions in C - Tutorial Internetworking with TCP / IP Vol1 - Doulgas Commer Unix Network Programming, Vol2, Richard Stevens Unix Network Programming, Vol1, Richard Stevens