socket, tcp / ip protocol, udp protocol

Reprinted: https://www.cnblogs.com/xiaowenshu/p/9916755.html

 

socket commonly referred to as "socket" is used to describe IP address and port, the communication link is a handle, the application issues a request to the network through the normally "sockets" network request or response.

socket originated in Unix, and one of the basic philosophy of Unix / Linux is the "everything is a file" to open the file [] [] [Close] to read and write mode operation. socket is a realization of the model, socket is a special file, some of them operating socket function is carried out (open, read / write IO, closed).

socket and file difference:

Files are all on the same computer, transferring data between two processes.

socket may be implemented in transmission of data between different computers, i.e. network traffic. For example, qq, open a web page, these are the socket to achieve communication.

Network communication but also that it comes tcp / ip udp protocol and protocol, socket which has a good package upd and tcp / ip protocols directly on it.

Simply under the tcp / ip protocol is doing, the network just came out, chaos, and that we all have to transfer data to comply with a rule, we all follow this, then appeared tcp / ip protocol. Maybe you heard of 3-way handshake, four times off, that is the process of a tcp / ip connection. Add a computer to computer communication and b, the process is such that

a: in it, I can not even you do
b: in, you even it
a: Well, I'm going to send you the data

# This is the 3-way handshake, which established a good channel, two computers can be passable again.

So what is it four times off

a: I want you off the
b: Well, you disconnect it
b: close the channel
a: closing the channel

Why off twice, because the data transfer ends to each other, we dug two paths, one path is used to transmit data to a b, b to the other one is a data transfer, it is turned off twice, each closing respective channels . The two roads it, there is a sub-called full-duplex, that is, both sides can send data to each other, if only one side can send data, it is called simplex.

FIG following, you can see the process of establishing the data connection and transmission process, and a disconnection process.

 

And the udp protocol is relatively simple, less complicated disconnection and connection, does not require 3-way handshake, the client does not need to determine whether the server can receive, tcp / ip must be established after a good connection to send data, and it is not udp connection, knowing ip and port number is directly sent, it is faster than tcp / ip, but unsafe.

upd like to write as possible on the road, there is no other party did not receive. The tcp / ip like to call, you must have turned to speak.

The following is udp server-side code:

The following is a client-side code

Run the server-side code run client sends data to the server side, server-side back to the data and do a simple chat applet following results

 

Here is the code tcp / ip protocol, server-side code:

 

# The following is the client-side code connection service

 

You might want to learn this dim it, in fact, these web frameworks underlying implementation is so, for example django, flask which will be a socket, we can also develop their own a web framework. Of course, now only a service to a client, with the multi-threaded or multi-process multiple clients that can be served.

The server runs the results

The following multi-threaded, every time a client came even started a thread to service, so that you can service multiple clients, start a thread by threading module to a request to start a thread for his service, as follows :

 

Guess you like

Origin www.cnblogs.com/renjiaqi/p/11371318.html