Common functions of UNIX Network Programming

Source: Internet
Author: User

Basic tcp client-serverProgramSuch:

1. Socket Functions

Prototype: # include <sys/socket. h>

Int socket (INT family, int type, int Protocol );Returned value: non-negative descriptor -- success,-1 -- error;

Purpose: Specify the expected communication protocol interface (TCP, UDP, or Unix domain byte Protocol)

Family indicates the protocol cluster

Family Explanation
Af_inet IPv4 protocol
Af_inet6 IPv6 protocol
Af_local UNIX and protocols
Af_route Router interface
Af_key Key set Interface

Type

Type Explanation
Sock_stream Byte stream set Interface
Sock_dgram Data Packet Interface
Sock_raw Original set Interface

2. Connect Function

Prototype: # include <sys/socket. h>

Int connect (INT sockfd, const struct sockaddr * servaddr, socklen_t addrlen );Returned value: 0 --- success,-1 ---- error;

Purpose: The client uses the connect function to establish a connection with the TCP server;

Parameter: sockfd, set the interface description;

Servaddr is a pointer to the interface address structure. The address structure must contain the Server IP address and port number;

Addrlen, set the size of the interface address structure;

The following situations are returned when a connect error occurs:

    • If the tcp client does not receive a SYN subscriber response, etimedout is returned.
    • If the response to the customer's SYN is RST, it indicates that there is no process waiting to connect to the server host on the port we specified. This is called a hardware error. When the client receives the rst, an error econnrefused is returned immediately;
    • If the SYN sent by a client causes an ICMP error with an inaccessible destination on the intermediate router, this is called a soft error. The client continues to send SYN at intervals, if no response is received at the specified time, ehostunreach is returned;
3. Bind function prototype: # include <sys/socket. h> int BIND (INT sockfd, const struct sockaddr * myaddr, socklen_t addrlen ); Returned value: 0 --- success,-1 ---- error; Role: assign a local Protocol address to the set interface; parameter: sockfd, set interface description; Myaddr, a pointer specific to the Protocol's address structure. For TCP, you can specify an IP address, a port number, addrlen, and the length of the second parameter's address structure. 4. Listen function prototype: # include <sys/socket. h> int listen (INT sockfd, int backlog ); Returned value: 0 --- success,-1 --- error; Role: convert an unconnected interface to a passive interface, indicating that the kernel should accept connection requests from this interface; the second parameter specifies the maximum number of connections in the next interface queue; parameter: sockfd, set interface description word; backlog, set interface queue maximum number of connections; Understanding of the second parameter backlog:
    • The connection queue is not completed. An entry is opened for each SYN shard that has been sent by the client and reached the server. The server is waiting to complete the TCP three-way handshake process. All these interfaces are in syn_rcvd;
    • Completed queue: opens an entry for each customer who has completed the TCP three-way handshake process. All these interfaces are in the established status;
Link: 5. Accept function prototype: # include <sys/socket. h> int accept (INT sockfd, struct sockaddr * cliaddr, socklen_t * addrlen ); Returned value: non-negative descriptor --- success,-1 ---- error; function: A server call is provided. The following COMPLETED connection is returned from the completed connection queue header. If the completed connection queue is empty, the process is sleep. Parameter: sockfd, set the interface description; Cliaddr: the client process Protocol address; Addrlen, the second parameter, the length of the client process Protocol address; 6. Close function prototype: # include <unistd. h> int close (INT sockfd); function: puts the set of interfaces on the "closed" Mark and immediately returns to the process;

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.