Common Information, structure, formatting, and functions of Linux Network Programming

Source: Internet
Author: User
Common Information about Linux network programming, including structure, formatting, and functions-general Linux technology-Linux programming and kernel information. For details, see the following. 1. struct

Struct sockaddr {
Unsigned short sa_family;/* address family, AF_xxx */
Char sa_data [14];/* 14-byte Protocol address */
};

Struct sockaddr_in {
Short int sin_family;/* address family */
Unsigned short int sin_port;/* Port Number */
Struct in_addr sin_addr;/* IP Address */
Unsigned char sin_zero [8];/* fill in 0 to keep the same size as struct sockaddr */
};

2. format functions

Sin_zero (it is used to fill the sockaddr_in structure with the same length as struct sockaddr)

Htons () -- "Host to Network Short"; htonl () -- "Host to Network Long"
Ntohs () -- "Network to Host Short"; ntohl () -- "Network to Host Long"

3. network functions

Int socket (int domain, int type, int protocol );
The domain parameter specifies the socket type, generally AF_INET. The type can be SOCK_STREAM or SOCK_DGRAM, indicating TCP connections and UDP connections respectively. protocol is usually assigned "0 ". The Socket () call returns an integer socket descriptor, which you can use later.

Int bind (int sockfd, struct sockaddr * my_addr, int addrlen );
Sockfd is a socket descriptor. my_addr is a sockaddr pointer that points to information such as the local IP address and port number. addrlen is often set to sizeof (struct sockaddr ). My_addr.sin_port = 0;/* The system randomly selects an unused Port Number */my_addr.sin_addr.s_addr = INADDR_ANY;/* fill in the local IP Address */

Int connect (int sockfd, struct sockaddr * serv_addr, int addrlen );
Sockfd is the sockt descriptor of the target server; serv_addr is a pointer containing the IP address and port number of the target server. -1 is returned when an error occurs, and errno contains the corresponding error code. For client program design, you do not need to call bind (), because in this case, you only need to know the IP address of the target machine, and you do not need to care about which port the customer uses to establish a connection with the server, the kernel automatically selects an unused port for the client to use.

Int listen (int sockfd, int backlog );
Sockfd is the Socket descriptor returned by the socket System Call. backlog specifies the maximum number of requests allowed in the Request queue. incoming connection requests will wait for accept () in the queue (refer to below ). Backlog limits the number of requests waiting for service in the queue. The default value is 20 for most systems. When the listen encounters an error,-1 is returned, and errno is set to the corresponding error code.

Accept (int sockfd, void * addr, int * addrlen );
Sockfd is the socket descriptor to be monitored. addr is usually a pointer to the sockaddr_in variable, this variable is used to store the information of the host requesting the service (a host sends this request from a port); addrten is usually a point value of sizeof (struct sockaddr_in). When an error occurs, a-1 value is returned and the corresponding errno value is set.

4. Server order
Socket (); bind (); listen (); accept ()

5. Data Flow Functions
Int send (int sockfd, const void * msg, int len, int flags );

Sockfd is the socket descriptor you want to use to transmit data, and msg is a pointer to the data to be sent.

Len is the length of data in bytes. Flags is usually set to 0 (for the usage of this parameter, refer to the man Manual ).

Char * msg = "Beej was here! "; Int len, bytes_sent ;......
Len = strlen (msg); bytes_sent = send (sockfd, msg, len, 0 );......

The Send () function returns the number of actually sent bytes, which may be less than the data you want to Send. Therefore, the return value of send () needs to be measured. When the return value of send () does not match len, this situation should be processed.

The recv () function is prototype:
Int recv (int sockfd, void * buf, int len, unsigned int flags );

Sockfd is the socket descriptor for receiving data, buf is the buffer for storing received data, and len is the buffer length. Flags is also set to 0. Recv () returns the number of actually received bytes, or if an error occurs,-1 is returned and the corresponding errno value is set.

Sendto () and recvfrom () -- Data Transmission Using Datagram
In the connectionless datagram socket mode, because the local socket does not establish a connection with the remote machine, the destination address should be specified when sending data. The prototype of the sendto () function is:

Int sendto (int sockfd, const void * msg, int len, unsigned int flags, const struct sockaddr * to, int tolen );
This function has two more parameters than the send () function. to indicates the IP address and port number of the host, while tolen is often assigned sizeof (struct sockaddr ). The Sendto function also returns the actual length of data bytes or-1 in case of a sending error.

The original Recvfrom () function is:
Int recvfrom (int sockfd, void * buf, int len, unsigned int flags, struct sockaddr * from, int * fromlen );

From is a variable of the struct sockaddr type, which saves the IP address and port number of the source machine. Fromlen is often set to sizeof (struct sockaddr ). When recvfrom () is returned, fromlen contains the number of data bytes actually stored in from. The Recvfrom () function returns the number of bytes received or-1 if an error occurs, and the corresponding errno is set.

It should be noted that when you call the connect () function for the datagram socket, you can also use send () and recv () for data transmission, however, this socket is still a datagram socket and uses the UDP Service at the transport layer. However, when sending or receiving data reports, the kernel automatically adds the object and source address information.

Close () and shutdown () -- end Data Transmission
After all data operations are completed, you can call the close () function to release the socket and stop any data operations on the socket: close (sockfd );

You can also call the shutdown () function to close the socket. This function allows you to stop data transmission in a certain direction, while data transmission in one direction continues. For example, you can close the write operation of a socket and allow the socket to continue to accept data until all data is read.

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.