Theoretical Basis of set Interface Programming: normal startup

Source: Internet
Author: User

Start the serverProgram
Zhaoxj $ make tcpserv01
Gcc-I ../lib-g-O2-d_reentrant-wall-c-o tcpserv01.o tcpserv01.c
Gcc-I ../lib-g-O2-d_reentrant-wall-O tcpserv01 tcpserv01.o ../libunp. A-lpthread

Zhaoxj $./tcpserv01 &
[1] 13424

Zhaoxj $ netstat-antp
(Not all processes can be detected. process information of all non-users is not displayed. To view all information, you must switch to the root user) activate Internet connections (servers and established connections)
PROTO Recv-Q send-Q local address foreign address State PID/program name
TCP 0 0 0.0.0.0: 80 0.0.0.0: * listen-
TCP 0 0 0.0.0.0: 9877 0.0.0.0: * Listen 13424/tcpserv01
TCP 0 0 127.0.0.1: 631 0.0.0.0: * listen

Check the TCP connection listening set interface and find that three interfaces are in the listen status. The local port 9877 is the server application we started.

Start the Client Program (open another Terminal)
Zhaoxj $ make tcpcli01
Gcc-I ../lib-g-O2-d_reentrant-wall-c-o tcpcli01.o tcpcli01.c
Gcc-I ../lib-g-O2-d_reentrant-wall-O tcpcli01 tcpcli01.o ../libunp. A-lpthread

Zhaoxj $./tcpcli01 127.0.0.1

The connection is established through three handshakes. Next steps:
1. The client calls the str_cli function, which blocks fgets calls. Because we haven't typed a line of text
2. When the accept in the server returns, the server calls fork and the child process calls str_echo. Str_echo calls Readline and Readline calls read,
Read is blocked while waiting for the client to send a line of text
3. On the other hand, the server parent process calls accept again and blocks it, waiting for the next client to connect

Zhaoxj $ netstat-antp
(Not all processes can be detected. process information of all non-users is not displayed. To view all information, you must switch to the root user) activate Internet connections (servers and established connections)
PROTO Recv-Q send-Q local address foreign address State PID/program name
TCP 0 0 0.0.0.0: 80 0.0.0.0: * listen-
TCP 0 0 0.0.0.0: 9877 0.0.0.0: * Listen 13424/tcpserv01
TCP 0 0 127.0.0.1: 631 0.0.0.0: * listen-
TCP 0 0 127.0.0.1: 9214 127.0.0.1: 9877 established 13525/tcpcli01
TCP 0 0 127.0.0.1: 9877 127.0.0.1: 9214 established 13526/tcpserv01
Tcp6 0 0: 1: 631: * listen-

The local port number of the second established is 9877, and the interface of the corresponding server sub-process can be determined. The local port number of the first established is 9214, you can determine the interface set for the corresponding customer process.

Zhaoxj $./tcpcli01 127.0.0.1
Hello, world
Hello, world
Good bye
Good bye
^ D <CTR-D> end the customer terminal

run the netstat command immediately. The following result is displayed:
zhaoxj $ netstat-antcp | grep 9877
TCP 0 0.0.0.0: 9877 0.0.0.0: * Listen 13424/tcpserv01
TCP 0 127.0.0.1: 9214 127.0.0.1: 9877 time_wait-
TCP 0 0.0.0.0: 9877 0.0.0.0: * Listen 13424/tcpserv01
TCP 0 127.0.0.1: 9214 127.0.0.1: 9877 time_wait-

the currently connected client (local port 9214) enters the time_wait status, and the listening server is still waiting for another client to connect.
steps to terminate the client and server normally:
1. When we type the EOF character, fgets returns a null pointer and the str_cli function returns.
2. When str_cli returns to the main function of the customer, main terminates through exit.
3. When a process is terminated, some operations are performed to disable all opened descriptive words. The description words opened by the customer are disabled by the kernel. This causes the customer to send a fin to the server over TCP, and the server TCP responds with Ack. This is the first half of the TCP connection termination operation. At this point, the server interface is in the close_wait state, and the customer interface is in the fin_wait_2 state.
4. when the server TCP accepts fin, the server sub-process is blocked in Readline call, so Readline returns 0. this causes the str_echo function to return the main function of the server sub-process
5. the server sub-process terminates by calling exit.
6. All the descriptions opened by the server sub-process are disabled. The last two sub-sections of the TCP connection termination sequence are triggered by the sub-process to close the connected interfaces: one is the fin from the server to the customer, and the other is the ACK from the customer to the server. Now, the connection is completely terminated, and the customer interface enters the time_wait status
7. another part of process termination is that when the child process of the server terminates, A sigchld signal is sent to the parent process. The default behavior of this signal is ignored.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.