Initial understanding of sockets

Source: Internet
Author: User
Tags ack bind socket port number
recently studied the socket, found themselves still have a lot of do not understand the place, simply sank down to the heart, from the most basic start to learn, began to look, now on their own learning to do a little summary, in order to share with you, if there is a fallacy, please correct me. Original articles, reproduced please specify the source: HTTP://BLOG.CSDN.NET/JESSONLV TCP/IP

Before learning the socket, review the TCP/IP protocol.

TCP/IP (transmission Control protocol/internet Protocol) is the transmission Protocol/Inter-network protocol that defines the standard of how hosts connect to the Internet and how data is transferred between them, literally tcp/ IP is the collectively known TCP and IP protocol, but in fact the TCP/IP protocol refers to the entire TCP/IP protocol family. Unlike the seven tiers of the ISO model, the TCP/IP protocol Reference Model classifies all TCP/IP series protocols into four abstraction tiers:

Application layer: TFTP, HTTP, SNMP, SMTP, DNS, Telnet, etc.
Transport layer: TCP and UDP
Network layer: IP ICMP OSPF EIGRP IGMP
Link layer: SLIP cslip PPP MTU

Look at the words:


In the TCP/IP protocol, two Internet hosts are connected through two routers and corresponding layers. Applications on each host perform read operations with each other through a number of data channels:


SocketHow to uniquely identify a process
Using Triples: IP address, protocol, port number. In fact, this is the solution provided by the TCP/IP protocol, the IP address of the network layer can uniquely identify the host in the network. The protocol + port of the transport layer uniquely identifies the application in the host.
After the process can be uniquely identified, socket communication is possible, and the socket is an implementation of a UNIX-based "open--write/read--close" pattern. It abstracts the complex operations of the TCP/IP layer into a few simple interfaces to provide layer calls to implement the process of communication in the network. Read the following: Note: Socket originated from UNIX, in Unix everything is the idea of file philosophy, socket is an "open-read/write-off" mode implementation, the server and the client each maintain a "file", after the establishment of the connection opened, You can write to your own files for each other to read or read the contents of the other, close the file when the communication ends.the communication flow of the socketThe socket is an implementation of "open-read/write-off" mode, which uses the TCP protocol communication socket as an example, and its interaction flow is probably



Server creates sockets based on address type (IPV4,IPV6), socket type, protocol


Server binds IP address and port number to socket


Server socket Listener Port number request, ready to receive the client's connection, this time the server socket is not opened


Client creates socket


Client opens socket, attempts to connect to server socket based on server IP address and port number


The server socket receives a client socket request, passively opens, and begins receiving client requests until the client returns the connection information. When the socket enters the blocking state, the so-called blocking is the Accept () method until the client returns the connection information, and begins receiving the next client understanding request


Client connection successful, sending connection status information to server


Server Accept method returned, connection successful


The client writes information to the socket


Server Read information


Client shutdown


Server-side shutdown

famous three-time handshakeIn the TCP/IP protocol, the TCP protocol establishes a reliable connection through a three-time handshake:
First handshake: The client tries to connect to the server, sends a SYN packet to the server (synchronization sequence number synchronize Sequence Numbers), SYN=J, the client enters Syn_send state waits for the server to confirm


Second handshake: The server receives the client SYN packet and confirms (ACK=J+1), and sends a SYN packet (SYN=K) to the client, which is the Syn+ack packet, when the server enters the SYN_RECV state


Third handshake: The third handshake: the client receives the server's Syn+ack packet, sends the acknowledgment packet ack (ACK=K+1) to the server, the packet is sent, the client and server enter the established state, complete three handshake


Fixed eye, the server socket and the client socket to establish a connection part is actually the famous three times handshake:


Socket Programming APIMain reference Java API:

int socket (int domain, int type, int protocol);
Assigns a descriptor for a socket and the resources it uses according to the specified address family, data type, and protocol.
Domain: Protocol family, commonly used are af_inet, Af_inet6, af_local, Af_route where af_inet represents the use of IPv4 address
Type:socket types, commonly used socket types are, Sock_stream, Sock_dgram, Sock_raw, Sock_packet, Sock_seqpacket, etc.
Protocol: protocol. Commonly used protocols are, IPPROTO_TCP, IPPTOTO_UDP, IPPROTO_SCTP, IPPROTO_TIPC, etc.
int bind (int sockfd, const struct SOCKADDR *addr, socklen_t Addrlen);
Assign a specific address in the address family to the socket
Sockfd:socket description Word, which is the socket reference
Addr: The protocol address to bind to the SOCKFD
Addrlen: Length of Address

Usually when the server is started to bind a well-known address (such as IP address + port number) to provide services, the client can be used to connect the server, and the client does not specify, there is a system automatically assigned a port number and its own IP address combination. This is why the server usually calls bind () before listen, and the client does not invoke it, but instead generates one randomly from the system at Connect ().
int listen (int sockfd, int backlog);
Monitor socket SOCKFD: The socket descriptor to listen on
Backlog: The maximum number of connections that the corresponding socket can queue
int connect (int sockfd, const struct SOCKADDR *addr, socklen_t Addrlen);
Connect a socket
SOCKFD: Socket description Word for Client
Addr: The server's socket address
Length of the Addrlen:socket address
int accept (int sockfd, struct sockaddr *addr, socklen_t *addrlen);
After the TCP server has heard the client request, call the Accept () function to take the receive request
SOCKFD: Socket descriptor for server
Addr: Client's socket address
Length of the Addrlen:socket address

ssize_t Read (int fd, void *buf, size_t count);
Read Socket contents
Fd:socket Description Word
BUF: Buffer
Count: Buffer length

ssize_t Write (int fd, const void *buf, size_t count);
Writing to the socket is actually sending the content
Fd:socket Description Word
BUF: Buffer
Count: Buffer length

int close (int fd);
The socket is marked as close so that the reference count of the corresponding socket descriptor-1, when the reference count is 0, triggers the TCP client to send a terminating connection request to the server.


Related Article

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.