Understanding of socket and TCP/IP

Source: Internet
Author: User
TCP/IP

To understand socket, you must first familiarize yourself with the TCP/IP protocol family. TCP/IP (Transmission Control Protocol/Internet Protocol) is the Transport Control Protocol/inter-network protocol, defines how hosts connect to the Internet and how data is transmitted between them,

Literally, TCP/IP is the combination of TCP/IP and ipprotocol, but in fact TCP/IP refers to the entire TCP/IP protocol family of the Internet. Different from the seven layers of the ISO model, the TCP/IP protocol reference model classifies all TCP/IP series protocols into four abstraction layers.

Application Layer: TFTP, HTTP, SNMP, FTP, SMTP, DNS, telnet, etc.

Transport Layer: TCP, UDP

Network Layer: IP, ICMP, OSPF, VPN, IGMP

Data link layer: slip, cslip, PPP, MTU

Each abstraction layer is built on the services provided by the lower layer and provides services for the higher layer. It looks like this

It is estimated that anyone interested in this article will have some knowledge about this. I am also a bit knowledgeable, so I will not explain it in detail. If you are interested, you can search for information on the Internet.

Wikipedia

Baidu encyclopedia

In TCP/IP, two Internet hosts are connected through two routers and corresponding layers. Applications on various hosts read from each other through some data channels.

 

Socket

We know that if two processes need to communicate, the most basic premise is that they can uniquely identify a process. In local process communication, we can use PID to uniquely identify a process, however, the PID is only unique locally, and the two processes in the network have a high chance of PID conflict. At this time, we need to set up another path. We know that the IP address on the IP layer can uniquely identify the host, the TCP layer protocol and port number can uniquely identify a process on the host, so that we can use the IP address + protocol + port number to uniquely identify a process on the network.

After the processes in the network can be uniquely identified, they can use socket for communication. What is socket? We often translate a socket into a socket, which is an abstraction layer between the application layer and the transport layer, it abstracts the complex operations on the TCP/IP layer into several simple interfaces for the application layer to call and Implement Process Communication in the network.

Socket originated from Unix. In the philosophy that everything in UNIX is a file, socket is an implementation of the "open-read/write-Close" mode, the server and the client maintain a "File". After a connection is established and opened, they can write content to their own files for the other party to read or read, and close the file at the end of communication.

Socket Communication Process

The socket is implemented in the "open-read/write-Close" mode. The interaction process of a socket that uses TCP communication is like this.

The server creates a socket based on the address type (IPv4, IPv6), socket type, and protocol.

The server binds the IP address and port number to the socket.

The server socket listens to port number requests and is ready to receive connections from the client at any time. At this time, the server socket is not opened.

Create a socket on the client

The client opens the socket and tries to connect to the server socket based on the server IP address and port number.

The server socket receives the socket request from the client and opens it passively. It starts to receive the request from the client until the client returns the connection information. At this time, the socket entersBlockingStatus, the so-called blocking is the accept () method until the client returns the connection information before it returns, began to receive the next client understanding request

The client is connected successfully and sends the connection status information to the server.

The server's accept method is returned and the connection is successful.

The client writes information to the socket.

Server read information

Close Client

Disable the server

Three-way handshake

In TCP/IP, TCP establishes a reliable connection through three-way handshake.

First handshake: the client tries to connect to the server and sends the SYN Packet (synchronous sequence numberSynchronize sequence numbers), SYN = J. The client enters the syn_send status and waits for confirmation from the server.

The second handshake: the server receives the SYN packet from the client and confirms (ACK = J + 1). It also sends a SYN Packet (SYN = K) to the client, that is, the SYN + ACK packet, the server enters the syn_recv status.

Third handshake: The third handshake: the client receives the SYN + ACK packet from the server and sends the ACK (ACK = k + 1) Confirmation packet to the server, the client and server enter the established status and complete the three-way handshake.

The server socket and client socket are actually three handshakes that are well-known.

 Socket programming API

As mentioned above, socket is implemented in the "open-read/write-Close" mode. Let's take the TCP protocol as an example to briefly understand which APIs are provided by socket for use, look at the socket API in Unix. Other languages are similar (PHP and even their names are almost the same). Here I will briefly explain the functions and parameters of the method, if you are interested, you can refer to the link in the blog reference or search online.

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

Allocates the description of a socket and its resources based on the specified address family, data type, and protocol.

Domain: protocol family. common protocols include af_inet, af_inet6, af_local, and af_route. af_inet indicates that the IPv4 address is used.

Type: socket type. Common socket types include sock_stream, sock_dgram, sock_raw, sock_packet, and sock_seqpacket.

Protocol: protocol. Common protocols include ipproto_tcp, ipptoto_udp, ipproto_sctp, and ipproto_tipc.

int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);

Assigns a specific address in an address family to socket

Sockfd: Socket description, that is, socket reference

ADDR: Protocol address to bind to sockfd

Addrlen: Address Length

Generally, a server is bound to a well-known address (such as an IP address and port number) when it is started to provide services, and customers can use it to connect servers; clients do not need to specify it, A system automatically assigns a port number and its own IP address combination. This is why the server usually calls BIND () before listen, but the client does not call it. Instead, the system randomly generates one at connect.

int listen(int sockfd, int backlog);

Listen to socket

Sockfd: Description of the socket to be listened on

Backlog: Maximum number of connections that can be queued by the corresponding socket

int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen);

Connect to a socket

Sockfd: Socket description of the Client

ADDR: server socket address

Addrlen: length of the socket address

int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);

After the TCP server listens to the client request, it calls the accept () function to receive the request.

Sockfd: Socket description of the server

ADDR: socket address of the client

Addrlen: length of the socket address

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

Read socket content

FD: Socket description

Buf: Buffer Zone

Count: Buffer Length

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

Writing content to a socket is actually sending content

FD: Socket description

Buf: Buffer Zone

Count: Buffer Length

int close(int fd);

When the socket flag is disabled, the reference count of the corresponding socket description is-1. When the reference count is 0, the tcp client is triggered to send a request to the server to terminate the connection.

Understanding of socket and TCP/IP

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.