Linux Socket Socket Programming 20160704

Source: Internet
Author: User
Tags ack file transfer protocol

Before we introduce sockets, let's look at the protocol TCP and UDP for the transport layer:

The difference between TCP protocol and UDP protocol

First we understand that the TCP protocol and the UCP protocol and the TCP/IP protocol, many people are confused, has always been the TCP/IP protocol and the UDP protocol

Difference, I think it is not clear from the nature of network communication!

The TCP/IP protocol is a protocol cluster. It includes a lot of protocols. UDP is just one of them. The TCP/IP protocol is named because the TCP,IP protocol is

Two very important agreements, they are named after him.

The TCP/IP protocol set includes the application layer, the transport layer, the network layer, and the network access layer.

Among the application layers are:

Hypertext Transfer Protocol (HTTP): The basic protocol for the World Wide Web.

File transfer (TFTP Simple File Transfer Protocol):

Telnet, which provides remote access to other host features, which allows users to log on

Internet host, and execute commands on this host.

Network Management (SNMP Simple Network Management Protocol), which provides methods for monitoring network devices, as well as configuration management, statistical information collection, performance management and security control

Management and so on.

Domain Name System (DNS), which is used to convert a domain name and its public broadcast network nodes to an IP address on the Internet.

Second, the network layer includes:

Internet Protocol (IP)

Internet Control Information Protocol (ICMP)

Address Resolution Protocol (ARP)

Reverse Address Resolution Protocol (RARP)

Finally, the network access layer: The network access layer is also known as the host-to-network layer (host-to-network). Network access layer features include IP address and physical address hardware

and encapsulate the IP into frames. Based on the network interface of different hardware types, the network access layer defines the connection to the physical media.

Of course, I do not say well enough here, TCP/IP protocol is a science, each branch is a very complex process, but I believe that every learning software

The development of the students are necessary to understand carefully.

Let me focus on the differences between the TCP protocol and the UDP protocol.

TCP (transmission Control Protocol, transmission Protocol) is a connection-oriented protocol, which means that before sending and receiving data, it must be built

Reliable connections. A TCP connection has to go through three "dialogs" to build up, and the process is very complex, with a simple description of the three times

Simple process: Host A sends a connection request packet to Host B: "I want to send you data, OK?" "This is the first conversation; Host B to host a

Send consent to connect and require synchronization (sync is two hosts one in the send, one in the receiving, coordinated work) packet: "Can you, when did you send

? "This is the second conversation; Host a then sends a packet to confirm that Host B's requirements are synchronized:" I'll send it now, you go on! " "This is the third time to

Words Three times the purpose of the "conversation" is to synchronize the sending and receiving of packets, and after three "conversations", host a formally sends the data to Host B.

The detailed point says is: (article partial reprint http://zhangjiangxing-gmail-com.iteye.com, mainly is this person explained very in place, indeed very

Easy to make people understand! )

TCP three-time handshake process

1 Host A to host B by sending a data segment containing a flag bit of the serial number to Host B, to host B to request a connection, through this data segment,

Host A tells Host B two things: I want to communicate with you, you can use which serial number as the starting data segment to respond to me.

2 when Host B receives a request from host A, it responds to host A with a data segment with a ACK (ACK) and a synchronous sequence number (SYN) flag bit, and also tells the host a two

Thing:

I have received your request, you can transfer the data, you need to use which pins XLR serial number as the starting data segment to respond to my

3 When host a receives this data segment, it sends a confirmation reply confirming that the data segment of Host B has been received: "I have received a reply and I am now going to start transmitting the actual number

According to the

So 3 times the handshake is complete, and host A and Host B can transfer the data.

3-Time handshake features

No data for the application tier

SYN This flag bit will only be set when the TCP build connection is 1

SYN flag bit is set 0 after handshake completes

TCP establishes a connection for 3 handshakes, and disconnects 4 times

1 when host a completes the data transfer, the control bit fin is set to 1 and a request to stop the TCP connection is made

2 Host B responds to the FIN after it receives it, confirming that the TCP connection to the side is closed and the ACK is set to 1

3 by the B-end and then forward the reverse direction of the closing request, the Fin 1

4 Host a confirms the request of Host B, the ACK is set to 1, and the closing of the double direction closes.

It can be seen from the three handshake and four disconnects of TCP that TCP uses the connection-oriented communication method, which greatly improves the reliability of data communication, and makes the transmitting end

And the receiving end of the data before the formal transmission of the interaction, for the formal transmission of data to lay a reliable foundation

noun explanation

ACK one of the control bits of the TCP header to confirm the data. Confirm that the destination is issued, and use it to tell the sender that the data segment before the serial number

All received. For example, the confirmation number is x, that is, the first X-1 data segment received, only when ack=1, the confirmation number is valid, when ack=0, the confirmation number is invalid, then

will require data to be re-transmitted to ensure the integrity of the data.

SYN Synchronous serial number, TCP establishes this position 1 when establishing a connection

The FIN sender completes the send task bit, and when TCP completes the data transfer needs to disconnect, the side that proposes the disconnection will have this position 1

The Baotou structure of TCP:

Source Port 16-bit

Destination Port 16-bit

Serial Number 32 bits

Response Number 32 bit

TCP Header Length 4 bits

Reserved 6-bit

Control Code 6 Bits

Window size 16 bits

Offset 16 bits

Checksum 16-bit

Option 32-bit (optional)

This gives us the minimum length of the TCP header, which is 20 bytes.

UDP (User data Protocol, Subscriber Datagram Protocol)

(1) UDP is a non-connected protocol that transmits data before the source and terminal do not establish a connection, and when it wants to transmit it simply crawls from the application

Data and throw it to the web as quickly as possible. On the sending side, UDP transmits data only at the speed of the application-generated data, the ability of the computer

and transmission bandwidth; At the receiving end, UDP places each message segment in the queue, and the application reads a message segment from the queue each time.

(2) since the transmission of data does not establish a connection, so there is no need to maintain the connection status, including sending and receiving status, so a server can be simultaneously to multiple customers

The same message is transmitted by the machine.

(3) The header of the UDP packet is very short, with only 8 bytes, and the extra overhead of 20 byte packets relative to TCP is small.

(4) Throughput is not regulated by congestion control algorithms, only the rate of data generated by the application software, the transmission bandwidth, the source side and the performance limit of the terminal host.

(5) UDP uses the best effort to deliver, that is, does not guarantee reliable delivery, so the host does not need to maintain a complex link state table (there are many parameters).

(6) UDP is message-oriented. The sender's UDP message to the application is delivered down to the IP layer after the header is added. Neither split nor divided.

And, instead of preserving the boundaries of these messages, the application needs to select the appropriate message size.

We often use the "ping" command to test the TCP/IP communication between the two hosts is normal, in fact, the principle of "ping" command is sent to the other host

UDP packets, and then the other host to confirm the receipt of the packet, if the packet arrives in a timely manner feedback back, then the network is a pass.

Header structure of UDP:

Source Port 16-bit

Destination Port 16-bit

Length 16 bit

Checksum 16-bit

Summary of the differences between TCP and UDP:

1. Based on connection and no connection;

2. Requirements for system resources (more TCP, less UDP);

3.UDP program structure is relatively simple;

4. Flow mode and datagram mode;

5.TCP guarantees data correctness, UDP may drop packets, TCP guarantees data order, UDP is not guaranteed. TCP fails to retransmit, UDP does not

Next, let's look at the socket programming that works in the application layer:

Sockets are a way of communicating with other programs using the standard UNIX file descriptor (descriptor). Sockets can be thought of as communication connection endpoints for two programs that are located between different hosts. On the one hand, the information that the program will transmit is written to the socket, and on the other hand the data in the socket is read to obtain the transmitted information.

Figure 1 Socket communication

Figure 1 shows the use of sockets for communication. Assuming that there are two hosts A and B, process C exists in host A, process d exists in Host B, and when process C needs to send data to process D, the data is first written to the socket, and process D can obtain the information sent by process C by reading the socket.

In the network, different computers are differentiated by IP address, that is, to send data from host A to Host B, as long as the IP address of Host B can determine the destination of the data to send. However, in host A and b it is not possible to have only process C and process D two processes. After Host B receives the data sent by host A, how can I determine if the data is being sent to process d? Therefore, some identification information is also needed to describe the process that the network communication data is destined for. The TCP/IP protocol proposes a protocol port concept that identifies the process of communication.

When a process is bound to a port, the operating system will send the data received to that port to the process. Like file descriptors, each port has an identifier for an integer type called a port number, which is used to differentiate between different ports. Different protocols can use the same port number for data transfer. For example, TCP uses a port number of 344, and UDP can also use a 344 port number for data transfer.

The port number is a 16-bit unsigned integer with a value range of 0~65535. Ports below 256 are used as the system's reserved port number, primarily for system process communication, and the port number that is not in this range is called the free port number, which can be used freely by the process.

Mainly look at the client:

retval = Connect (sockfd, (struct sockaddr*) &servaddr,sizeof (SERVADDR));

(struct sockaddr*) &servaddr is the address of the server that represents an address connected to a server

Sockets

ret = getaddrinfo (Psrvname, NULL, &hostinfo, &paddrinfo);

The Getaddrinfo function can handle both name-to-address and service-to-port conversions, and returns a list of SOCKADDR structures instead of one. These sockaddr structures can then be used directly by the set of interface functions

int getaddrinfo (const char *hostname, const char *service, const struct addrinfo *hints, struct addrinfo **result);

Parameter description

Hostname: A host name or address string (a dotted decimal string of IPv4 or a 16-string of IPV6)

Service: The server name can be a decimal port number or a defined service name, such as FTP, HTTP, etc.

Hints: can be a null pointer or a pointer to a addrinfo struct in which the caller fills in a hint about the type of information expected to return. For example, the specified service can support both TCP and UDP, so the caller can set the Ai_socktype member in the hints structure to sock_dgram so that only the information returned is applicable to the datagram socket interface.

Result: This function returns a pointer to the ADDRINFO structure's linked list through the result pointer parameter.

Return value: 0--successful, non-0--error

The following 6 parameters are usually required before the Getaddrinfo function: nodename, servname, hints Ai_flags, ai_family, Ai_socktype, Ai_protocol

Usually the server side before calling Getaddrinfo, ai_flags settings ai_passive, for bind, host name nodename is usually set to NULL, return to the wildcard address [::].

If NodeName is a native name and ServName is null, the address list is returned depending on the operating system slightly different

struct sockaddr{

unsigned short sa_family;

Char sa_data[14]

};

Sa_family: Used to specify the address family, and if TCP/IP traffic, the value is pf_inet. Sa_data: The IP address and port number information used to hold the socket

struct SOCKADDR_IN {

short int sin_family;

unsigned short int sin_port;

struct IN_ADDR sin_addr;

unsigned char sin_zero[8];

};

L sin_family: Used to specify the address family.

L Sin_port: The port number of the socket communication.

L SIN_ADDR: The IP address of the communication.

L SIN_ZERO[8]: to fill 0, keep the same size as the struct sockaddr.

Because the SOCKADDR data structure is the same size as the SOCKADDR_IN data structure, pointers to sockaddr_in can be cast to exponentially pointers to the SOCKADDR structure.

int socket (int domain, int type, int protocol);

The socket function is used to create a socket for communication and returns the file descriptor for that socket. The parameter domain specifies the communication domain, which is used to select the communication protocol family parameter type for specifying the types of sockets. Socket types In addition to the previously mentioned flow sockets, datagram sockets, and raw sockets, there are several other types of parameter protocol used to specify the communication protocol used by the socket. Under normal circumstances, for a given protocol family, only a single protocol supports a particular socket type. At this point, just set the protocol parameter to 0

int connect (int sockfd, const struct SOCKADDR *serv_addr, socklen_t Addrlen);

The Connect function uses the socket in the parameter sockfd to connect to the server specified in parameter serv_addr. The parameter addrlen is the amount of memory space that serv_addr points to.

If the type of the parameter SOCKFD is the SOCK_DGRAM,SERV_ADDR parameter, the address is reported to, and only the datagram of that address is received. If the type of SOCKFD is Sock_stream or sock_seqpacket, calling the function will connect to the server address in serv_addr.

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

The Send function is used to send information to the specified socket file descriptor. This function can only be used in socket communication that has already established a connection, that is, only for connection-oriented communication. The parameter S is the socket file descriptor to send the data. The BUF parameter is a pointer to the data to be sent. Len is the length of the data to be sent. The flag parameter can contain the following parameters

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

The RECV function is used to get the sent message from the specified socket. As with the Send function, this function can only be used in socket communications that have already established a connection, that is, only for connection-oriented communication. The parameter S is the socket file descriptor to read the information. The BUF parameter is a pointer to the data buffer to be saved. And Len is the maximum length of the cache.

In addition to the use of sockets can be implemented between different hosts in the network communication, but also to achieve the same host of different processes between the communication, and the establishment of communication is two-way communication. The use of sockets for inter-process communication, referred to here, is achieved by specifying the communication domain as Pf_unix.

AF means address FAMILY, PF represents protocol FAMILY protocol family, but the two macro definitions are the same, so it doesn't matter which one is used. Winsock2.h in # define AF_INET 2, #define Pf_inet af_inet, so af_inet in Windows is exactly the same as pf_inet. In the Unix/linux system, there is a slight difference between the two in different versions. For BSD, it is AF, for POSIX is PF. UNIX Systems support Af_inet,af_unix,af_ns and so on, while Dos,windows only supports Af_inet, which is the Internet zone.

Af_inet (also known as pf_inet) is the socket type of the IPV4 network protocol, AF_INET6 is IPV6, and Af_unix is the UNIX system local communication. The purpose of choosing af_inet is to use IPV4 for communication. Because IPV4 uses a 32-bit address, the calculation is faster and easier to use for LAN communication than the 128-bit IPv6. And Af_inet is more versatile than Af_unix because there is af_inet on Windows and no Af_unix.

2.

Socket is a way of inter-process communication, this time the socket creation, binding, connection parameters are different from the network on the communication between different hosts, such as the use of SOCKETADDR, interprocess communication using Sockaddr_un. And the communication between the hosts is using sockadd_in

CONNECT_FD = socket (Pf_unix, sock_stream, 0);

if (CONNECT_FD < 0)

{

Perror ("Client Create socket failed");

return 1;}

Set Server Sockaddr_un

srv_addr.sun_family = Af_unix;

strcpy (Srv_addr.sun_path, Unix_domain);

Connect to Server

ret = Connect (connect_fd, (struct sockaddr*) &srv_addr, sizeof (SRV_ADDR));

As for the server, refer to:

Linux Socket Socket Programming 20160704

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.