进击 of python
Network Programming --UDP
That seeing here now
This is a network programming last spoke
Mentioned above all about the programming method of TCP
Remember the difference between TCP and UDP transport it?
Simple UDP simply means that it may not transmit data to the pipe
It is up to you how to transmit information by way UDP socket way to do that?
The old rules, messaging py create two files, the server and the client
server = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
Remember this sentence in the TCP how to write it
Here data transmission using a "datagram mode", the UDP protocol is also called Datagram Protocol
(Phone = socket.socket (socket.AF_INET, socket.SOCK_STREAM))
In this talk, the streaming protocol is not a stick pack phenomenon ah
Sorry boil, this is simply not the UDP this phenomenon
We found this socket.socket always write a lot of trouble
So we can write at the beginning importing modules
form socket import *
Though not recommended * to import the module, but because of the way we want to use too much
So, you can import them all to this module
Therefore, the above method can be written as
server = socket(socket.AF_INET,socket.SOCK_DGRAM)
What port binding, binding IP, is not it need?
server.bind(("127.0.0.1", 8080))
That listener or need it? server.listen (5)
I this is UDP, there are links Well? Is not no? So do not need to listen
That or need server.accept () do?
Or is this sentence, accept links only to use, I do not UDP link, so do not
That there is no link, the link is not circulating, ah you do not need
That is not the communication loop should also be ah
In UDP, the received message is called recvfrom () message with the sendto ()
while 1:
data = server.recvfrom(1024)
print(data)
server.close()
That server then finished
Then there is the client
The client does not need to be binding IP? Of course not need!
The client does not need to be established pipeline? Of course not need!
I can not send it empty?
It is impossible for TCP, UDP and see that it
msg = input(">>>")
client.sendto(msg.encode("utrf-8"),("127.0.0.1", 8080))
sendto receiving two arguments, the first is the message you send, the second is in the form of tuples ip and port number you want to send the
That, client and server on the finished
# 客户端
from socket import *
client = socket(AF_INET, SOCK_DGRAM)
while 1:
msg = input(">>>")
client.sendto(msg.encode("utf-8"), ("127.0.0.1", 8080))
client.close()
# 服务端
from socket import *
server = socket(AF_INET, SOCK_DGRAM)
server.bind(("127.0.0.1", 8080))
while 1:
data = server.recvfrom(1024)
print(data)
server.close()
# 客户端
>>>haha
>>>dawdadwad
>>>fdafwa
# 服务端
(b'haha', ('127.0.0.1', 63671))
(b'dawdadwad', ('127.0.0.1', 63671))
(b'fdafwa', ('127.0.0.1', 63671))
Then I found out that my data is received in the form of a tuple
In front of the content transmitted to the client behind the port and ip
I want what I can understand why I would also like his ip and port ah?
Simply because I need to know this data who sent me ah, then I better go back ah
That completed the interactive services and client of the
data,client_addr = server.recvfrom(1024)
server.sendto(data.upper(),client_addr)
Address the issues raised above, when I made empty, but also can run, so do not consider the problem of empty
And, because he is a datagram protocol, when in fact every hair is to put all the information sent me
So there is no possibility of contact between each of the data, there is no stick package
That being so convenient, why not UDP ah
Remember when we said that he's not safe yet?
If the data transmission network has experienced volatility, and that there is no information
But others will not give you hair again
Another is to receive information may not be complete (you try to recvfrom () inside look at the effect of digital becomes 1)
So there are two instability
UDP protocol file transfer mode is not suitable for downloading or remote command
But the inquiry is the thing with UDP protocol
QQ information transmission is UDP protocol
To a query is fast, unreliable big deal for me and then check it again
However it boils, TCP still used a lot of
This part of the thing is actually quite small, most of them are based concept
From the beginning of the five-layer protocol, and then learned that the two agreements
Then build up the application layer and the transport layer by a method bridge socket
Then the information transmission mode TCP protocol on a careful analysis
Cotton developed a generic template
Finally raised about the use of UDP protocol is TCP and the relative advantages and disadvantages for the
The next part is a concurrent programming, interested continue to focus on me!