1. Network transmission mode -UDP
This is the 11th day of my participation in Gwen Challenge
1.1 the UDP concept
User Datagram Protocol (UDP) is a connectionless and unreliable network transport Protocol
Udp network transport protocol is like transportation
1.2 characteristics of UDP
Because UDP does not need to establish a connection before sending data all have the following characteristics
- There is no connection
- Low resource overhead
- Fast transmission speed
- The maximum value of each UDP packet is 64K
1.3 Advantages and disadvantages of UDP
Advantages:
- Fast transmission speed
- No connection required, low resource overhead
Disadvantages:
- Data transmission is unreliable and data packets are easily lost
- There is no flow control, when the other party does not receive data in time, the sender keeps sending data will cause the buffer to be full, the computer will be stuck, all the recipients need to receive data in time.
1.4UDP Application Scenarios
When the network communication quality is not high and the network communication speed is required, UDP can be used
- Qq audio and video transmission, wechat audio and video transmission
- Co-screen software used in class
- Sending a broadcast message
summary
UDP is a simple datagram – oriented transport layer protocol. UDP does not provide reliability; it simply sends datagrams from applications to the IP layer, but there is no guarantee that they will reach their destination. UDP does not need to establish a connection between the client and the server before transmitting data packets, and there is no timeout retransmission mechanism, so the transmission speed is very fast
2. Introduction to socket
2.1 How do processes on different computers communicate with each other
First through the IP address to find the corresponding computer in the network, and then through the transmission protocol and port number to determine the process (running the software), so how the data transmission needs to use socket to complete, that is, communication between processes using socket
Note:
A process is a running program or piece of software called a process (more on that in multitasking).
Interprocess communication refers to the sharing of data between running programs
2.2 What is a Socket
Socket (socket for short) is a tool for interprocess communication. It can transfer data from one party to another and complete the communication between processes on different computers. It is like a data porter.
2.3 Socket Usage Scenarios
Literally, any application or software related to the network uses sockets
3, UDP network procedures – send and receive data
3.1 UDP network program flow
The process of creating a UDP-based network application is very simple. The steps are as follows:
- Create a client socket
- Send/receive data
- Close the socket
3.2 create a socket
This can be done by using the socket module’s socket function in Python:
import socket
socket.socket(AddressFamily, Type)
Copy the code
Description:
Socket creates a socket with two arguments:
Address Family: indicates the type of the IP Address. AF_INET indicates ipv4 and AF_INET6 indicates ipv6. Type: Socket Type, which can be either SOCK_STREAM (stream socket, mainly for TCP) or SOCK_DGRAM (datagram socket, mainly for UDP)
Create a UDP socket
import socket
Create a UDP socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
#... Here is the function of using sockets (omitted)...
# Close the socket when not in use
s.close()
Copy the code
instructions
The socket usage process is very similar to the file usage process creating a socket using the socket to receive/send data closing the socket
3.3 Sending Data
The code is as follows:
# -*- coding: utf-8 -*-
import socket
# 1. Create a UDP socket
udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
Prepare the receiver's address. '127.0.0.1' indicates destination IP address. 8080 indicates destination port
dest_addr = ('127.0.0.1'.8080) The IP address is a string and the port number is a number
# 3. Get data from the keyboard
send_data = input("Please enter the data to send :")
# 4. Send data to the specified program on the specified computer
udp_socket.sendto(send_data.encode('gbk'), dest_addr)
# 5. Close sockets
udp_socket.close()
Copy the code
Running Network Debugger on Windows:
3.4 Sending and receiving data
import socket
# 1. Create a UDP socket
udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# 2. Prepare the address of the recipient
dest_addr = ('127.0.0.1'.8080)
# 3. Get data from the keyboard
send_data = input("Please enter the data to send :")
# 4. Send data to the specified computer
udp_socket.sendto(send_data.encode('gbk'), dest_addr)
# 5. Waiting to receive the data sent by the other party
recv_data = udp_socket.recvfrom(1024) # 1024 indicates the maximum number of bytes received at this time
# 6. Display the data sent by the other party
Recv_data is a tuple
The first element is the data sent by the other party
The second element is the IP address and port of the other party
print(recv_data[0].decode('gbk'))
print(recv_data[1])
# 7. Close sockets
udp_socket.close()
Copy the code
4. Udp binding port number
4.1 UDP Network Program-port number Problem
- Variable port number
Re-run the script several times, and then in Network Debug Assistant, you see the following:
Description:
- The reason the number in the red circle is different each time the network program is rerun is that this number identifies the network program, and when rerun, if you have not decided which one to use, the system will assign it randomly by default
- Keep in mind that the network program is uniquely identified as it is running, so if other network programs on computers want to send data to this program, they need to send data to the program identified by this number (port number)
4.2 UDP Binding Information
<1>. Binding information
In general, there are many network programs running on a computer. In order not to occupy the same port number with other network programs, udp port numbers are usually not bound in programming
But if you want to make a server-side application, you need to bind it. Why is that?
If the alarm number changes every day, the world will be confused, so the general service program, often need a fixed port number, this is called port number binding
<2>. Binding example
#coding=utf-8
from socket import *
# 1. Create a socket
udp_socket = socket(AF_INET, SOCK_DGRAM)
# 2. Bind local information. If a network application is not bound, it will be randomly assigned
local_addr = ('127.0.0.1'.9090) IP address and port number: IP address indicates any IP address of the local host
udp_socket.bind(local_addr)
# 3. Waiting to receive the data sent by the other party
recv_data = udp_socket.recvfrom(1024) # 1024 indicates the maximum number of bytes received at this time
# 4. Display the received data
print(recv_data[0].decode('gbk'))
# 5. Close sockets
udp_socket.close()
Copy the code
Running results:
< 3 >. Summary
- A UDP network program that is not bound with a port number. In this case, the operating system randomly assigns a port number. If the port number is bound, the port number remains unchanged each time the program is started.
conclusion
The article is long, give a big thumbs up to those who see it! Due to the author’s limited level, the article will inevitably have mistakes, welcome friends feedback correction.
If you find this article helpful, please like, comment, and bookmark it
Your support is my biggest motivation!!