Socket: stream, TCP connection, TCP reliability Overview

Source: Internet
Author: User

As mentioned above, different types of sockets are associated with different underlying protocol families and different protocols in the same protocol family. What I want to talk about is the content in the TCP/IP protocol family. Now TCP
/The main socket types in the IP protocol family are "stream socket" and "datagrams socket (
Socket )". In reality, stream socket is similar to call communication
Socket is similar to writing and communication. Of course, other protocol families also have the corresponding stream socket and datemedisocket.

I like to compare the similarities and differences between similar things, so I will start with the similarities.

I. Client and Server

No matter whether you call or write a letter, there will always be a call
The recipient and the recipient of the email. The latter responds to the request sender by replying or answering the phone number. Internet communication is similar to this process. Client
The terms client and server represent two different roles: client is the initiator of the communication, and server passively waits for the client to initiate
And respond to it. The key point to be put forward here is to distinguish between the client and the server is who is the request sender and who is the request responder. Therefore, the client and
Roles can be exchanged between servers. Once the original client becomes the request responder, the client becomes the server, and vice versa. Server is not
A high-configuration Advanced Server with oracle and other data. The client is neither a personal computer nor a browser. Remember, the client and server are only the senders of the request.
And an abstract concept of the requestor. Why do we need to distinguish the two? Because a program is used as a client and server to determine the Socket used by the peer to establish communication
API format. What is peer? The peer end of the client is the server, and vice versa. Furthermore, the client needs to know the location of the server, that is, the location of the server.
Server IP and port, but not vice versa. If necessary, the server can obtain the client address through the corresponding API. This is very similar to calling,
The caller can answer the call without knowing the phone number of the caller. If necessary, call the caller to display the phone number. As long as the communication is established successfully, the server and
The client has no difference. (What, free answer? Call fees? Okay, I admit this is one of the differences)

II. in the TCP/IP protocol family, stream Socket uses TCP as its peer protocol, provides a connection-based trusted byte stream service.

 

The following are the questions I encountered when I first read something similar: 1. What is a stream? 2. What is connection-based? What is the connection? 3. What is trustworthy? To what extent can I trust? Is the receiver still able to receive data after a power failure on the sender side?

I believe someone will have questions similar to me.
A stream is a simple and ordered byte sequence. Note that it is ordered. It is easy to understand that the input stream writes new bytes in an ordered manner, while the output stream reads bytes. Because TCP
Socke is stream-based, so we can guess that the corresponding InputStream object and OutputStream object will be maintained in the Socket instance. When we pass
After the Socket OutputStream outputs data, these bytes can finally be read from the other end of the connection through InputStream. TCP transmits a byte stream without the record boundary concept. This is very important and can be summarized as a non-user-visible "group" concept in TCP, which only transmits a byte stream, we cannot accurately predict how many bytes will be returned in a specific read operation. In the simplest example, the data you send three times through OutputStream may be read twice after OutputStream read, and there may be multiple cases in the middle. TCP reliability is involved here, which will be detailed in the following article.
  
2. Like a byte sequence in a network environment called a packet, the "connection" here is also a word used based on a specific context. It is easier to understand the connection from "no connection. Obviously, if communication is required between different hosts, you must connect them in some way, either wired or wireless. So what does connectionless communication mean? The answer is that connection-based and non-connection-based protocols. That is to say, the connection here does not refer to the physical media, but is a transmission method that describes how to transmit data on the physical media. Data may be transmitted online on the same network, whether connected or not. For TCP, the connection is completely "imagined. It is composed of the State remembered by the endpoint (client and server) and does not have a "physical" connection. Although we used a telephone call to compare TCP, there was a physical connection during the call. This
As long as the two parties believe that the TCP connection exists and the IP packet can be sent to each other
The connection always exists. Simply put, if one end thinks that the other end can accept data, it records the sending status of the data and sends the data, unless it knows that the other end can no longer receive the data. That is to say, the so-called
The connection is that the client thinks that I can transmit data to the server, and the server exists. The server thinks that I should wait for the client to transmit the data. While
We believe that the connection exists through three handshakes. In this case, the problem arises. The problem is that the connection is imaginary, so it is not real-time. That is to say, it is not like the connection in the lower right corner after you unplug the network cable.
It indicates that there will be a Red Cross. When one end fails, the other end may still think that the connection exists.

 

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.