Socket in Linux Network Programming (2): General Procedures and basic socket functions of C/S programs

Source: Internet
Author: User

1. TCP-based Network Program

It is the general process of a TCP-based client/server program:


After the server calls socket (), BIND (), and listen () to complete initialization, it calls accept () to block and wait, and is in the listening port State. After the client calls socket () to initialize, call connect () to issue SYN segments and block wait for the server to respond, the server responds to a SYN-ACK segment, the client returns from connect () Upon receipt, and responds to an ACK segment at the same time, the server returns the result from accept () After receiving the message.


Data transmission process:

After a connection is established, the TCP protocol provides full-duplex communication services. However, generally, the client/server program initiates a request actively, and the server passively processes the request in a one-Answer manner. Therefore, the server immediately calls read () after returning data from accept (). The read socket is like a read pipeline. If no data arrives, the client blocks the wait. In this case, the client calls write () the server sends a request to the server. After receiving the request, the server returns it from read () and processes the request from the client. During this period, the client calls read () to block waiting for the server's response, and the server calls write () send the processing result back to the client and call read () again to block the next request. After receiving the result, the client returns the result from read () and sends the next request.


If the client has no more requests, call close () to close the connection. Just like the pipeline closed by the write end, the server returns 0 for read, in this way, the server knows that the client has closed the connection and also calls close () to close the connection. Note: After either party calls close (), both transmission directions of the connection are closed and data cannot be sent. If one party calls Shutdown (), the connection is in the semi-closed state and can still receive data from the other party.


When learning the socket API, pay attention to how the application interacts with the TCP protocol layer:

* What action does the TCP protocol layer perform when an application calls a socket function? For example, if connect () is called, a SYN segment is sent.

* How does an application know the status changes at the TCP protocol layer? For example, if a blocked socket function returns, it indicates that the TCP protocol has received some segments, such as read () if 0 is returned, the fin segment is received.


In addition, TCP has a total of 11 statuses, but no closing status appears. when both parties close the connection at the same time, this status will appear, replacing the fin_wait2 status.


Ii. Basic socket Functions

1. Socket Functions

Include header file <sys/socket. h>
Function: Creates a socket for communication.
Prototype: int socket (INT domain, int type, int Protocol );
Parameters
Domain: Specify the protocol family, af_inet, af_inet6, af_unix, and so on.
Type: Specifies the socket type, streaming socket sock_stream, datagram socket sock_dgram, original socket sock_raw
Protocol: protocol type, ipproto_tcp, etc.; generally, the protocol type is determined by the first two parameters, set to 0.
Return Value: a non-negative integer is returned successfully. It is similar to a file descriptor. We call it a socket. -1 returned for failure


2. Bind Functions

Include header file <sys/socket. h>
Function: bind a local address to a socket
Prototype: int BIND (INT sockfd, const struct sockaddr * ADDR, socklen_t addrlen );
Parameters
Sockfd: Socket returned by the socket function
ADDR: Address to bind
Addrlen: Address Length
Return Value: 0 is returned for success, and-1 is returned for failure.


3. Listen Function

Include header file <sys/socket. h>
Function: Used to listen for incoming connections.
Prototype: int listen (INT sockfd, int backlog );
Parameters
Sockfd: Socket returned by the socket function
Backlog: specifies the maximum number of connections that the kernel queues for this socket.
Return Value: 0 is returned for success, and-1 is returned for failure.

Generally, the listen function should be called after the socket and bind functions are called and before the function accept is called.
For a given listening set interface, the kernel needs to maintain two queues:
1. The client has sent the request to the server, and the server is waiting for the TCP three-way handshake process to be completed.
2. Queue with Connection completed

As shown in:



4. Accept Function

Include header file <sys/socket. h>
Function: returns the first connection from the completed connection queue. If the completed connection queue is empty, the connection is blocked.
Prototype: int accept (INT sockfd, struct sockaddr * ADDR, socklen_t * addrlen );
Parameters
Sockfd: server socket
ADDR: the socket address of the peer is returned.
Addrlen: returns the socket address length of the peer.
Return Value: a non-negative integer is returned if the return value is successful. If the return value fails, the return value is-1.


5. Connect Functions

Include header file <sys/socket. h>
Function: Creates a socket connected to the specified ADDR.
Prototype: int connect (INT sockfd, const struct sockaddr * ADDR, socklen_t addrlen );
Parameters
Sockfd: Socket not connected
ADDR: the socket address to connect
Addrlen: length of the second ADDR Parameter
Return Value: 0 is returned for success, and-1 is returned for failure.


Refer:

Linux C Programming one-stop learning

Chapter 1 TCP/IP details

UNP

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.