Linux Network Programming Fundamentals Programming --TCP point summary

About the basics of network programming, I will summarize the previous blog there are interested can look at: network programming - basics summary
TCP transport layer protocol (Transmission Control Protocol) is a connection-oriented, reliable, DE, word-based transport layer communication protocol throttled
in the seven-layer model, which belongs to the transport layer, to establish a connection between the host port.

Here Insert Picture Description

TCP programming steps

General Procedure server

1, to create a socket, with a socket function ();
2, to set the socket attribute, a function setsockopt (); * optional
3, binding information of the IP address and port to the socket, a function the bind ();
. 4, open monitoring, by the function the listen ();
. 5, is connected to receive customer served by the function Accept ();
. 6, send and receive data, a function send () and the recv (), are read () and Write ();
. 7, closed network connection;
8, close monitor;

Clients typically step

1, to create a socket, with a socket function ();
2, to set the socket attribute, a function setsockopt (); * optional
3, binding information of the IP address and port to the socket, a function bind (); * Optional
4, set the attribute of the IP address and port of the other side to be connected;
5, the connection server, a function connect ();
. 6, send and receive data, a function send () and the recv (), or a read () and Write ();
7, off a network connection;

The socket function

socket function

int socket(int domin int type int protocol);

Parameters:
①domin
Here Insert Picture Description
②type
SOCK_STREAM: stream sockets] [uniquely corresponds to TCP
SOCK_DGRAM: single datagram socket [] corresponding to UDP
SOCK_RAM: raw socket
③protocol
generally filled 0, the original socket programming is required to fill

Return value
the RETURN of VALUE
ON Success, A File descriptor for error returned.On The new new Socket IS, IS returned -1, errno IS SET appropriately and
returns a file descriptor on success, -1 on error.

bind () function

For a port associated with the socket descriptor on the machine (only for server)

int bind(int sockefd, const strict sockaddr*addr, sockelen_t addrlen);

Parameters:
sockfd: socket descriptor is a get function through the socket fd
addr: the address of the variable structure of struct sockaddr
addrlen = sizeof (struct aockaddr)

struct sockaddr{
sa_family;
char sa_data(14);
}

Return Value: 0 = successful; failed = -1; erno = error number
can be acquired automatically with the IP address of the following assignments and a random port number acquired not occupied:
addr.sin_port = 0 / system is not a random selection port number /
addr.sin_.s_addr = INADR_ANY; / fill local IP address /

Connect function

Establish a TCP connection with the remote server (for the client)

int connect(int sockfd, struct sockaddr *serv_addr, int addrlen);

Sockfd purpose server sockt descriptor
serv_addr object pointer IP address and port number contained.
Returns: 0 = Success; failed = -1, errno = error number.

listen function

After listening whether there is a service request for bind ()

int listen(int sockefd, int backlog);

Parameters:
sockfd: by a socket () function to get fd
backlog: 5 generally filled; while allowing the passage of several client and server connection process, that test, a maximum of 8 the ARM
core server maintains two socket fd a list (number = 2 * backlog + 1)
has been established client list good connection (3-way handshake has been completed with a good fly newfd)
returns:
success: 0; error: 1

accept function

Block waiting for client connection requests

int accept(int sockefd, struct sockaddr *addr,sockelen_t*addrlen);

Parameters:
scokefd: After the previous socket (), fd and through the bind (), listen () provided
addr: the address structure variable of a struct sockaddr
addrlen: address length
Return Value:
Success: Returns established newfd good connection
failure :-1

read (), write () function

ssize_t read(int fd, void *buf, size_t count);
ssize_t write(int fd, const void *buf, size_t count);

read():

  • read function is responsible for reading the content from fd

  • When the reading is successful, read returns the number of bytes actually read

  • If the return value is 0 means the end of the file has been read, is less than 0 indicates that an error

  • If the error is caused by the terminal is described EINTR; if it is represented by a network connection problem ECONNREST

  • write():

  • nbytes byte write function to write the contents of the file descriptor fd buf

  • Returns the number of bytes written on success, -1 on failure and set errno variable

  • In network programming, there are two possibilities when we write to the socket file descriptor:

    • 1, write the return value> 0, indicating a write part or all of the data
    • 2, the return value is <0, then an error has occurred
  • If the error is an error EINTR indicates that an interruption in the time of writing; if EPIPE indicate a problem with the network connection (the other side has closed the connection)

Send function

In the information transmission connected socket (TCP) of
int Send (int sockfd, const void * msg, int len, int the flags);
sockfd is used to transmit data socket descriptor
msg is a pointer to a pointer to the data to be transmitted.
Len is the length in bytes of data.
flags set to 0 under normal circumstances.

recv function

Receiving data at a connected socket (TCP) of
int the recv (int sockfd, void * buf, int len, the flags unsigned int);
sockfd accept data socket descriptor;
buf is the buffer storing the received data;
len is buffered length.
Flags also set to 0.
Returns: the number of bytes actually received, if the connection is aborted, returns 0 ,. When an error occurs, and sets the appropriate return -1 errno value.

sendto function

Transmitting at connectionless (UDP) in the socket pattern data
int the sendto (int sockfd, const void * MSG, int len, unsigned int the flags, const struct the sockaddr * to, int tolen);
to represent IP addresses and ports head to machine number information
tolen = sizeof (struct sockaddr).
Returns: byte length of data actually transmitted or -1 when a transmission error.

Recvfrom function

Receiving data in a connectionless (UDP) of the socket manner
int recvfrom (int sockfd, void * buf, int len, unsigned int the flags, struct the sockaddr * from, int * of fromlen);
from the IP address and port number stored source machine.
fromlen = sizeof (struct sockaddr).
Returns: the number of data bytes from the actual deposit. Returns -1 when an error occurs, and sets the appropriate errno.

close function

Release socket, to stop any data manipulation
close (sockfd);

shutdown function

Close-way connection
int the shutdown (int sockfd, int How);
How can be set to the following values:
1.0 ------- allowed to continue to receive data
-transmission data allowed to continue. 1 -------
* 2 ------- allowed to continue to send and receive data, it is allowed is called Close ()
the shutdown operation returns 0 on success, returned when an error occurs -1 (corresponding juxtaposed errno).

gethostbyname function

Translates domain names and IP addresses
struct hostent * gethostbyname (const char * name);

inet_pton function

The dotted decimal string converted into binary values network byte order, the function of IPv4 and IPv6 addresses can be handled.
int inet_pton (int family, const char * strptr, void * addrptr);
The first parameter may be the AF_INET6 AF_INET or: The second parameter is a pointer to a pointer to the string decimal point: The third parameter is a pointer to the converted binary value pointer network byte order.
Returns: 1- 0- successful expression format is not a valid input failure -1-

inet_ntop function

And inet_pton functions contrary, inet_ntop function is to convert the binary value to network byte order dotted decimal string.
const char * inet_ntop (int family, const void * addrptr, char * strptr, size_t len);
The first parameter may be AF_INET or AF_INET6: The second parameter is a pointer pointing to network byte order binary value; third parameter is a point after the decimal point to convert string pointer; fourth parameter is the size of the target, in order to avoid buffer overflow which function caller.
Returns: the pointer points to the result of - success NULL- failure

Published 28 original articles · won praise 5 · Views 3717

Guess you like

Origin blog.csdn.net/qq_42711899/article/details/104422740