Brief introduction to TCP / IP

This is the back-end small class of the monastery. Each article is shared from

[background introduction] [knowledge analysis] [common problems] [solutions] [coding practice] [extended thinking] [more discussion] [References]

Eight aspects of in-depth analysis of back-end knowledge / skills. This article shares:

[brief introduction to TCP / IP]

Hello, I'm Yan Heng, java student of IT academy Shanghai Branch, an honest, pure and kind java programmer

Today, I'd like to share with you the introduction of postman and its simplicity and practicality

1. Background introduction

A collection of rules, standards, or conventions established for the exchange of data in a computer network. Generally speaking, network protocol is the bridge of communication and exchange between networks. Only computers with the same network protocol can communicate and exchange information. This is just like the various languages used in communication between people. Only by using the same language can we communicate normally and smoothly. From a professional point of view, network protocol is a convention that must be observed when computers realize communication in the network, that is, communication protocol. It mainly specifies and formulates standards for information transmission rate, transmission code, code structure, transmission control steps, error control, etc.

2. Knowledge analysis

3. Frequently asked questions

1: What is TCP / IP protocol

2: What is three handshakes

3: What is four handshakes

4. Solutions

1: What is TCP / IP protocol

TCP is a connection oriented protocol to ensure reliable transmission. Through TCP protocol, we get a sequential error free data stream. A connection must be established between the two paired sockets of the sender and the receiver in order to communicate on the basis of TCP protocol. When one socket (usually server socket) waits to establish a connection, the other socket can request a connection. Once the two sockets are connected, they can carry out two-way data transmission, and both sides can carry out sending and receiving operations.

2: What is three handshakes

In TCP / IP protocol, TCP protocol establishes a reliable connection through three handshakes: the first handshake: the client sends a TCP message with syn flag to the server. This is message 1 in the process of three handshakes. Second handshake: the server responds to the client. This is the second message in the three handshakes. This message carries both ack flag and syn flag. Therefore, it represents the response to the client syn message just now; At the same time, SYN is marked to the client to ask whether the client is ready for data communication. The third Handshake: the customer must respond to an ACK message in the service segment again, which is message segment 3.

3: What is four handshakes

In the TCP / IP protocol, the TCP protocol disconnects the connection four times. Because the TCP connection is full duplex, each direction must be closed separately. This principle is that when one party completes its data transmission task, it can send a fin to terminate the connection in this direction. Receiving a fin only means that there is no data flow in this direction. A TCP connection can still send data after receiving a fin. The party that first performs the shutdown will perform the active shutdown and the other party will perform the passive shutdown. First handshake: the TCP client sends a fin to close the data transmission from the client to the server. Second handshake: the server receives the fin and sends back an ACK. The confirmation sequence number is the received sequence number plus 1. Like syn, a fin will occupy a sequence number. The third Handshake: the server closes the connection of the client and sends a fin to the client. The fourth Handshake: the client segment sends back ACK message confirmation and sets the confirmation sequence number to the received sequence number plus 1

5. Coding practice

public classclient {

public static voidmain(String[] args) {

try{

//Create a socket and specify the server address and port

Socket client =newSocket("127.0.0.1",8001);

//The client sends login information to the server

OutputStream os = client. getOutputStream();// Byte output stream

PrintWriter pw =newPrintWriter(os);

pw. Write ("user name: Yan Heng; password: 123");

pw. flush();

client. shutdownOutput();

//Get output stream

InputStream is = client. getInputStream();

InputStreamReader isr =newInputStreamReader(is);

BufferedReader br =newBufferedReader(isr);

String info =null;

while((info = br.readLine())!= null) {

System. out. Println ("server:" + info);

}

//Close other resource exchange

pw. close();

os. close();

is. close();

isr. close();

br. close();

client. close();

}catch(UnkNownHostException e) {

e.printStackTrace();

}catch(IOException e){

e.printStackTrace();

}

}

}

public classServer {

public static voidmain(String[] args) {

try{

//Create a server socket, specify the bound port, and listen

ServerSocket server =newServerSocket(8001);

//Call the accept method to start listening and wait for the client to connect

System. out. Println ("start the service and wait for the client to go online");

Socket socket = server. accept();

//Get an input stream to read the login information sent by the client

InputStream is = socket. getInputStream();// Byte input

InputStreamReader isr =newInputStreamReader(is);// Convert byte stream

BufferedReader br =newBufferedReader(isr);// Add buffer for input stream

String info =null;

while((info = br.readLine()) != null){

System. out. Println ("I am the server, and the client says:" + info ");

}

socket. shutdowninput();

//Get output stream

OutputStream os = socket. getOutputStream();

PrintWriter pw =newPrintWriter(os);// Packaging print stream

pw. Write ("happy National Day ~");

pw. flush();

socket. shutdownOutput();

//Close resource (connection)

pw. checkError();

br. close();

isr. close();

is. close();

server. close();

}catch(IOException e) {

e.printStackTrace();

}

}

}

Video connection

https://pan.baidu.com/s/1qYqiivy

6. Expand thinking

The difference between TCP and UDP

TCP is character oriented, and there is no boundary between data streams; UDP is packet oriented, and there are clear boundaries between packets. For TCP, Send a string of numbers (1, 2, 3, 4, 5) and receive twice (1, 2) and (2, 4, 5), or change to any receiving mode, and the protocol stack only ensures the correct receiving order; UDP sends a packet, the receiver or the reception fails completely, and if it succeeds, the whole packet will be received. TCP is connection oriented, and UDP is connectionless. It is similar to the relationship between making a phone call and generating a telegram. TCP needs to shake hands with IP packets three times to establish a connection, and IP packets need to be disconnected Four handshakes. In addition, the initiator may enter time when the connection is disconnected_ Wait status for several minutes, In this state, the connection (port) cannot be released. TCP is reliable, and the data sent and received are consistent through data verification. UDP is unreliable, and sending a series of digital packets (1, 2, 3) may become invalid when received (1, 0, 0). When making a UDP connection, you need to verify the data yourself. TCP data is in order. The data sent in what order will be received in the same order; UDP is out of order, and it is possible to send (1, 2, 3) in the same order (1, 3, 2) are received in sequence. The application must sort the packets by itself. TCP needs extra work because of establishing connections, releasing connections, IP packet verification and sorting, and the speed is much slower than UDP. TCP is suitable for data transmission, and UDP is suitable for streaming media.

7. References

Reference 1:

Reference 2:

Reference 3:

By: Yan Heng

8. More discussion

That's all for today's sharing. You are welcome to like, forward, leave messages and make bricks~

Skill tree It Academy

"We believe that everyone can become an engineer. From now on, find a senior brother to introduce you, control your learning rhythm, and stop being confused on the way to learning.".

Here is the skill tree In it academy, thousands of senior brothers have found their own learning route here. Learning is transparent and growth is visible. Senior brothers have 1-to-1 free guidance. Come and study with me~

Author: the right path in the world is YH link: https://www.jianshu.com/p/85b86cfd0d3b Source: the copyright of Jianshu Jianshu belongs to the author. Please contact the author for authorization and indicate the source for any form of reprint.

For more information, you can join the IT communication group 565734203 to discuss and communicate with you

Here is the skill tree · it Academy: a gathering place for beginners to switch to the Internet

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>