当前位置:网站首页>Deep understanding of UDP, TCP

Deep understanding of UDP, TCP

2022-07-07 09:46:00 Don't ask me y

I have learned these two concepts several times
Every time, it seems that I don't understand
I listened to it several times today
I made a simple summary
If the description is not clear, please correct

1.UDP
A、UDP No client and server side , The connection must be initiated by the client , Because only the server port number and IP The address is fixed , Be sure to use IP Address and port number , namely bind() Method ; And use send() send data , When the other party receives data recvfrom() receive , When disconnecting, either party can disconnect , Because the server has recorded the client's IP Address and port number ;
B、UDP Is an unreliable connection protocol , Send a lot of , It is allowed to use UDP Protocol for transmission , Like audio , Video and other documents , The result of losing some data has no effect on the viewer .
 Insert picture description here

2.TCP
)TCP It's connection-oriented , reliable , Byte transfer protocol .
A、 Connection oriented , There are clients and servers , When connecting for the first time , There must be three handshake agreements , When disconnected , It's a four wave agreement ;connect,read,write,close.
B、 Monitor the network status at all times , Use the sliding window to control the transmission of data packets , When the network state becomes worse , The number of transmitted bytes is the original 1/2, When the network state recovers , If the current data transmission volume > The original 1/2, The transmission speed will be doubled ; If the current data transmission volume < The original 1/2, Then every transmission , Add a byte .
 Insert picture description here
Simple communication based on client and server :
Server side :
 Insert picture description here
client :
 Insert picture description here
Output results :
 Insert picture description here
principle :
The client determines the service port number , establish socket service , Define a socke, For reception accept Method returns a message from the server , use bufferedreader , use getinputstream Method to input information from the client , use printwriter,getoutputstream Method to send a message from the server to the client . take pw For message system.out.println Write output stream ,flush Method to the client , The client with bufferedreader,getinputstream Method reception . Close input stream , Close output stream , Close socket .

原网站

版权声明
本文为[Don't ask me y]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202130624178638.html