Chapter 4 Basic Socket Programming
· 4.1 Basic socket function
Header file:
Main functions:
INT Socket (int Domain, int type, int protocol); // Create a socket descriptor
[domain = AF_UNIX, AF_INET, AF_ISO; TYPE = SOCK_STREAM, SOCK_DGRAM, SOCK_RAW;]
INT Connect (Int SockFD, Struct SockAddr * Servaddr, Int Addrlen); / / Send a connection request to the server
INT BIND (int SockFD, struct sockaddr * myaddr, int addrlen); / / register a fixed port to the system
INT Listen (int setfd, int backlog); // Specify port of passive listening socket
INT Accept (int SockFD, Struct SockAddr * Addr, Int Addrlen); // Receive a connection set from the full connection queue
[If the full connection queue is smashed by the air queue, or return -1 directly, Accept is successfully returned (inherited listening socket properties) connection socket descriptor]
INT Close (int SockFD); // Turn off the socket descriptor
INT Shutdown (int suckfd, int howto); // Close the socket descriptor read and write channel
[Howto = shut_rd, shut_wr, shut_rdwr]
Network Frequency Sequence Conversion Function:
Header file:
Unsigned long int htonl; // Host to NetWork Long Font Switch
Unsigned long int htons (unsigned long int hostlong); // Host to NetWork Short Conversion
Unsigned long int ntohl (unsigned long int hostlong); // network to Host Long word conversion
Unsigned long int ntoHS (unsigned long int hostlong); // network to host short word conversion
IP address conversion function
Header file:
Main functions:
INT INET_ATON (const char * cp, struct in_addr * inf); // Indicates the string to the IP address switch Struct in_addr
Unsigned long int inet_addr (const char * cp); // Indicates the string to the IP address to 32 bits
Char * inet_ntoa (STRUCT IN_ADDR IN); // Indicates the Struct In_ADDR to the IP address rotation string
Sample code:
Listen_fd = socket (AF_INET, SOCK_STREAM, 0); // Create Internet Protocol Cluster, Flow Type Socket
IF (listen_fd == -1) error_proc (); Bzero (& serv_addr, sizeof (serv_addr)); // Initializing server address structure
Serv_addr.sin_family = AF_INET; // Using Internet Protocol Clusters
Serv_addr.sin_port = htons; // Set the port number and convert to network word order
/ * If it is the client execution
* Ret = inet_aton ("127.0.
0.1 "
& serv_addr.sin_addr); / / Set IP address for the SocketAddr address structure
* Then bind (); specify the port, then connect (); server-side request to establish a connection, start "three handshake" protocol
* If the server is executed * /
Serv_addr.sinaddr.s_addr = HTONL (INADDR_Any); // Allow any network device interface to connect and process
RET = BIND (Listen_FD, (Struct SockAddr *) & Serv_addr, SIZEOF (Serv_addr)); // Sack-in Binding Specified Port
IF (RET <0) error_proc ();
Listen (listen_fd, listening queue length); // is converted to listening to the connector, set the listening queue length
// Accept a new connection from the full connection queue, return to the connection socket descriptor
CONN_FD = Accept (Listen_FD, (Struct SockAddr *) & Serv_addr, SizeOf (Serv_addr));
· Concurrent server mode
/ *
0 server end sleeve initialization,
1 The parent process of the server side is listening to the address of the client, listens to the request connection from the client.
2 When the Accept is connected to the socket y, the Fork child process dedicated to the client's data processing,
3 child process closes the listening word X is not used, handle the client request, close Y, exit,
4 and the parent process closes the connection socket descriptor y is not used, continue to listen to the new client connection (transfer process 2).
* /
Do {
/ * Process 2; 3; 4; Realization Reference Code * /
// Receive a connection socket descriptor from the fully listening queue
CONN_FD = Accept (Listen_FD, (Struct SockAddr *) & cli_addr, sizeof (cli_addr));
IF (conn_fd <0)
Error_Proc (); // Error handling
Switch (Ret = for ()) {
Case -1:
Error_Proc (); // Error handling
Case 0:
Close (listen_fd); // Close the listening socket descriptor
Serv_for (conn_fd); / / Provide services for the client
exit (0);
DEFAULT:
Close (conn_fd); // Close the connection socket descriptor
}
WHILE (Continue);
Sample code:
Listen_fd = socket (AF_INET, SOCK_STREAM, 0); // Create Internet Protocol Cluster, Flow Type Socket
IF (listen_fd == -1) error_proc ();
Bzero (& serv_addr, sizeof (serv_addr)); // Initializing server address structure
Serv_addr.sin_family = AF_INET; // Using Internet Protocol Clusters
Serv_addr.sin_port = HTONS (port number); // Set the port number, and convert to network box / * If it is the client execution
* Ret = inet_aton ("127.0.
0.1 "
& serv_addr.sin_addr); / / Set IP address for the SocketAddr address structure
* Then bind (); specify the port, then connect (); server-side request to establish a connection, start "three handshake" protocol
* If the server is executed * /
Serv_addr.sinaddr.s_addr = HTONL (INADDR_Any); // Allow any network device interface to connect and process
RET = BIND (Listen_FD, (Struct SockAddr *) & Serv_addr, SIZEOF (Serv_addr)); // Sack-in Binding Specified Port
IF (RET <0) error_proc ();
Listen (listen_fd, listening queue length); // is converted to listening to the connector, set the listening queue length
// Accept a new connection from the full connection queue, return to the connection socket descriptor
CONN_FD = Accept (Listen_FD, (Struct SockAddr *) & Serv_addr, SizeOf (Serv_addr));
· Concurrent server mode
/ *
0 server end sleeve initialization,
1 The parent process of the server side is listening to the address of the client, listens to the request connection from the client.
2 When the Accept is connected to the socket y, the Fork child process dedicated to the client's data processing,
3 child process closes the listening word X is not used, handle the client request, close Y, exit,
4 and the parent process closes the connection socket descriptor y is not used, continue to listen to the new client connection (transfer process 2).
* /
Do {
/ * Process 2; 3; 4; Realization Reference Code * /
// Receive a connection socket descriptor from the fully listening queue
CONN_FD = Accept (Listen_FD, (Struct SockAddr *) & cli_addr, sizeof (cli_addr));
IF (conn_fd <0)
Error_Proc (); // Error handling
Switch (Ret = for ()) {
Case -1:
Error_Proc (); // Error handling
Case 0:
Close (listen_fd); // Close the listening socket descriptor
Serv_for (conn_fd); / / Provide services for the client
exit (0);
DEFAULT:
Close (conn_fd); // Close the connection socket descriptor
}
WHILE (Continue);