Socket features and three types of sockets

Source: Internet
Author: User
Tags ack rfc

Reprinted from Http://blog.chinaunix.net/uid-22240661-id-1781638.html

The function of 6.2.2 socket
The English original meaning of the socket is "hole" or "socket", now, as a BSD Unix process communication mechanism, take the subsequent significance. Common in daily life sockets, some signal outlets, some power outlets, some can accept signals (or energy), and some can send signals (or energy). If a socket is placed between the telephone line and the telephone (equivalent to the interface between the two, which is physically present), the socket is very similar to the telephone socket.

There is a striking similarity between the telephony system and the connection-oriented socket mechanism. Take a national telephone network for example. The telephone calls are equivalent to two processes of mutual communication; The area in which the call is located (with a globally unique area code) is equivalent to a network where the area code is its network address, and a unit in the zone is equivalent to a single host, and the internal number assigned to each user by the host is equivalent to the socket number (discussed below).

Any user prior to the call, the first to occupy a telephone, equivalent to the application of a socket number, and to know the other person's phone number, equivalent to each other has a socket. Then dial the call to the other side, which is equivalent to sending a connection request (if the other person is not in the same zone, and dial the other area code, equivalent to the network address). If the other party is present and free (equivalent to another host of communication and can accept the connection request), pick up the phone microphone, the two sides can officially call, the equivalent of successful connection. The process of communication between the two sides is the process of signaling to the telephone and receiving signals from the telephone, which is equivalent to sending data to the socket and accepting data from the socket. After the call is over, one side hangs the telephone, which is equivalent to closing the socket and undoing the connection.

In the telephone system, the average user can only feel the presence of the local telephone and the other phone number, the process of establishing the call, the process of voice transmission and the technical details of the whole telephone system are transparent to it, which is very similar to the socket mechanism. Socket uses the network communication facilities to achieve process communication, but it is not concerned about the details of the communication facilities, as long as the communication facilities to provide sufficient communication capacity, it is satisfied.

So far, we have an intuitive description of the socket. Abstract, the socket essentially provides the endpoint of the process communication. Before a process communicates, both parties must first create an endpoint, otherwise there is no way to establish a connection and communicate with each other. Just like before the phone call, both sides must each have a telephone.

Each socket is described in a semi-relevant description:
{protocol, local address, local port}
A complete socket is described with a related description:
{protocol, local address, local port, remote address, remote port}
Each socket has a local unique socket number, which is assigned by the operating system.

    Most importantly, the socket is designed for the client-server model and provides different socket system calls for client and server programs. A client randomly applies for a socket number (equivalent to a caller who can dial a call on any of the networks); The server has a globally recognized socket, and any customer can send it a connection request and a request for information (equivalent to a called phone with a phone number known to the caller)                           The
    Socket leverages the client-server model to skillfully resolve the problem of establishing a communication connection between processes. It is important that server sockets are globally recognized. Two completely random user processes, because no one side of the socket is fixed, like a phone call but do not know someone else's phone number, it is impossible to call.
        

Three types of 6.2.3   sockets
    sockets have three types: Streaming sockets (SOCK_STREAM), datagram Sockets (SOCK_DGRAM), and raw sockets.
    1. Streaming sockets (Sock_stream)
    Streaming sockets can provide a reliable, connection-oriented flow of traffic. If you send a sequential data through a streaming socket: "1" "2", then the order of the data at remote time is also "1" "2".
    What can a streaming socket do? Have you heard of the Telnet application? Oh, the most commonly used BBS services, as well as the remote landing of the system are connected through the Telnet protocol. Telnet is a streaming connection. Do you want the characters (or kanji) you entered on the Telnet application to arrive in the order you entered the remote application? The answer should be yes. There is also the WWW browser, which uses the HTTP protocol to get web pages through streaming sockets. In fact, if you telnet to a 80 port on a Web site and then type "get Web page pathname" and press the two-carriage return (or two Ctrl-carriage returns), you will be given the Web page that the "Web path name" represents!
    How does a streaming socket guarantee the quality of data transfer at this level of application? It uses the TCP (The transmission control Protocol) protocol (RFC-793 can be referenced to get details of TCP) )。 TCP guarantees that your data transfer is correct and is in order. TCP is the first half of TCP/IP that occurs frequently. IP represents Internet Protocol (Internet Protocol, reference RFC-791) IP only handles network routing.

    2. Datagram Sockets (SOCK_DGRAM)
    Datagram sockets define a connectionless service in which data is transmitted through separate messages, which are unordered and not guaranteed to be reliable or error-free. The original socket allows direct access to low-level protocols such as IP or ICMP, primarily for testing new network protocol implementations.
    Datagram Sockets (Datagram Sockets) How is it called "connectionless"? What should be done with them? Well, here are some facts:
    If you send a datagram, it may not arrive.
    It may arrive in a different order.
    If it arrives, there may be an error in the data it contains.
    Datagram sockets also use IP, but it does not use TCP, it uses User Datagram Protocol UDP (User datagram protocol can refer to RFC 768)
    Why are they "connectionless"? because it (UDP) does not maintain an open connection like a streaming socket, you only need to put the data into a packet, put the remote IP up, and then send the packet out. This process is not required to establish a connection. Examples of UDP applications are: TFTP, BOOTP and so on.
      So, how can you guarantee that a program will work if the packet is lost? In fact, every program that uses UDP has its own protocol for confirming data. For example, the TFTP protocol defines a packet that is sent out to each sender, sending a packet back to the local program after receiving it: "I've got it!" (an "ACK" package). If the sender of the packet does not receive a response within 5 seconds, it will resend the packet until the packet recipient echoes the ACK signal. This knowledge is necessary to write a programmer who uses the UDP protocol.
    Connectionless servers are generally transaction oriented, and one response to a request completes the interaction between client and service programs. The requests for connection-oriented server processing are often complex, not resolved by a single request response, and are often concurrent servers.

The

    sockets work as follows: The server starts first, creates a socket by calling the socket (), and then calls bind () to connect the socket to the local network address, and then call listen () to prepare the socket for listening. and specify the length of its request queue, and then call accept () to receive the connection. After a socket is established, the customer can call connect () and the server to establish a connection. Once a connection is established, the client and the server can send and receive data by calling read () and write (). Finally, after the data transfer is over, both sides call Close () to turn off the socket.
    3. Original socket
    Original socket is mainly used in the development of some protocols, can be compared to the bottom of the operation. It is powerful, but does not have the above two kinds of sockets to use conveniently, the general program also involves not to the original socket.

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.