"Turn" Linux C network programming--TCP set interface programming

Source: Internet
Author: User

Address: http://blog.csdn.net/matrix_laboratory/article/details/136692112. Socket () [CPP]View Plaincopy
    1. <span style="font-size:14px" >int socket (int domain, int type, int protocol);</span>


The socket () opens a network communication port and, if successful, returns a file descriptor like open (), where the application can send and receive data on the network like a read-write file, and returns 1 if the socket () call error occurs. read/write
(1) Domain:
Af_inet:ipv4
Af_inet6:ipv6
Af_unix: Non-network environment
af_unspic:undefined
(2) Type

Sock_stream: Creating a TCP stream socket

Sock_dgram: Creating a UDP datagram socket

Sock_raw: Creating the original socket

(3) Protocol: Specify a specific type of protocol
The parameter protocol is typically set to 0, indicating that the protocol used is determined by the type of socket specified by the parameter domain specified by the protocol family and parameter type.
(4) Return value
Returns a newly created socket after successful execution, and returns a-1 if an error occurs, and the error code is stored in errno.

3. Bind ()

int bind (int sockfd,struct sockaddr *my_addr,socklen_t addrlen)

The function of bind () is to bind a socket file descriptor to an address and a port.

(1) SOCKFD:

SOCKFD is the file descriptor returned by the call to the socket function;

(2) Addrlen

The length of the SOCKADDR structure.

(3) My_addr

A pointer to the SOCKADDR structure that holds the address (that is, port and IP address) information for the local socket. However, due to the problem of system compatibility, this structure is generally not used, and another structure (STRUCTSOCKADDR_IN) is used instead

(4) return

After the function succeeds, it returns 0 and returns 1 when an error occurs, and the error code is stored in errno.

[CPP]View Plaincopy
  1. struct sockaddr_in addr_serv,addr_client; / * Local Address information * /
  2. memset (&serv_addr,0,sizeof (structsockaddr_in));
  3. Addr_serv.sin_family= af_inet; / * Protocol family * /
  4. addr_serv.sin_port= htons (Serv_port); / * Local Port number * /
  5. Addr_serv.sin_addr.s_addr= htonl (Inaddr_any); / * Any local address * /
  6. /* Socket Binding */
  7. if (Bind (SOCK_FD, (STRUCTSOCKADDR *) &addr_serv),sizeof (struct sockaddr_in)) <0)
  8. {
  9. Perror ("bind");
  10. Exit (1);
  11. }

4. Listen ()

int listen (int sockfd,int list_size);

Used to initialize the server to connect to the queue, the server handles client connection requests in sequence, and only one client connection can be processed at the same time. When multiple client connection requests arrive at the same time, the server is not processing at the same time, but the client connection requests that cannot be processed are placed in the wait queue, the length of which is defined by the listen () function.

(1) SOCKFD

SOCKFD is the file descriptor returned by the call to the socket function

(2) List_size

Specifies the maximum length of the connection queue. If the connection queue has reached its maximum, subsequent connection requests are rejected by the server. Most systems are set to 20 and can be modified to 5 or 10, depending on the system's ability to withstand the load or the needs of the application.

(3) return

When the Listen () function runs successfully, the return value is 0, and when the run fails, its return value is-1, and the error code is stored in errno.

5. Accept ()

#include <sys/types.h>

#include <sys/socket.h>

int accept (int sock_fd,struct sockaddr*addr,socklen_t *addrlen)

3 times after the handshake is complete, the server calls accept () and accepts a link request.

If there is no link request before calling this function, the process is blocked, waiting for the link to SOCK_FD: The socket file descriptor that is returned when the socket is established, called by the socket ().

(1) SOCK_FD

is a listener socket created by a function socket that is bound to a local port by a function bind and then converted through the function listen.

(2) Addr

Used to save the address and port of the client .

Note: return parameters

(3) Addrlen

into: the size of the addr buffer.

Out: The size of the structure that the addr points to.

(4) return

The return value of the Accept () function is the new connected client socket file descriptor, which communicates with the client through the new socket file descriptor that is returned by the accept (), rather than by the file descriptor when the socket was established. If the accept () function has an error, accept () returns 1, and the error value can be obtained by errno.

(5) If the socket specified by the parameter sock_fd is set to block mode (the default mode under Linux) and the connection request queue is empty, the accept () will be blocked until a connection request ends, and if the socket specified by the parameter S is set to nonblocking, if the queue is empty, Accept will return immediately -1,errno is set to Eagain.

6. Connect ()

#include <sys/types.h>

#include <sys/socket.h>

int connect (int sock_fd,struct sockaddr *serv_addr,socklen_taddrlen);

TCP: the Connect () function is used by the server to make a connection request, and the server's IP address and port number are specified by the parameter serv_addr.

UDP: The Connect function does not establish a real connection, it simply tells the kernel the destination address (specified by the second parameter) that communicates with the socket, and only the data sent by that destination address is received by that socket. The benefit of calling the Connect function is that you do not have to specify the destination address each time you send and receive data.

(1) serv_addr

is a pointer to the data structure sockaddr, which includes the destination IP address and port number of the server to which the client needs to connect.

(2) Addrlen

Indicates the second parameter size, you can use sizeof (struct sockaddr)

(3) return

After successful execution, return 0, error occurs, return-1, error code is stored in errno.

7. Send ()

#include <sys/types.h>

#include <sys/socket.h>

ssize_t Send (int conn_fd,const void *buf,size_t len, int flags);

The function send is used to send data on a TCP socket, and send can only be used for sockets that are in the connected state.

(1) CONN_FD

is a socket descriptor that has been established for a connection, that is, the set of sockets that is returned after the Accept () function is called.

(2) BUF

Holds the buffer that sends the data.

(3) Len

The length of the send buffer

(4) Flags

For control options, the general setting is 0, or the following values are taken:

    • Msg_oob: Sends out out-of-band data on the specified socket (Out-of-band data), which must support out-of-band (for example: Sock_stream).

    • Msg_dontroute: Sends data through the most direct path, ignoring routing settings for the underlying protocol.

(5) return

The execution successfully returns the number of bytes actually sent, with an error of 1, and the error code is stored in errno.

A successful execution simply means that the data is written to the socket's buffer and does not indicate that the data has been successfully sent over the network to the destination.

8. RECV () Receive data

#include <sys/types.h>

#include <sys/socket.h>

ssize_t recv (int conn_fd,void *buf,size_t len,int flags)

Recv () is used to receive data on a TCP socket. The function recv receives data from the specified socket descriptor and saves it to the specified buf

(1) CONN_FD

is a socket descriptor that has a well-established connection, that is, the socket descriptor returned after calling the Accept () function

(2) BUF

Receive buffers

(3) Len

The size of the receive buffer

(4) Flags

For control options, generally set to 0 or take the following values

    • Msg_oob: Request to receive out-of-band data

    • Msg_peek: View data only and not read

    • Msg_waitall: Returns only when the receive buffer is full.

(5) return

The function execution successfully returns the number of bytes received, an error returns 1, and the error code is stored in errno.

"Turn" Linux C network programming--TCP set interface programming

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.