IP address at the network layer uniquely identifies hosts on the network, and Protocol + Port at the transport layer uniquely identifies application processes (processes) on hosts. In this way, the process of the network can be identified by IP address + protocol + port.

  • The so-calledprocessThe whole process is called a process (a process can be considered as an instance of program execution). Processes are independent entities of system resource allocation, and each process has its own address space.
  • The so-calledInterprocess communicationThis refers to data sharing between running programs

1. What is a socket

Socket (socket for short) is a way of interprocess communication. One of the main differences between socket and other interprocess communication is that it can realize interprocess communication between different hosts. Most of our various services on the network are based on socket to complete communication

2. Create a socket in Python

This can be done by using the socket module’s socket function in Python:

import socket
socket.socket(AddressFamily, Type)
Copy the code

Socket. socket creates a socket with two arguments:

  • Address Family: Can be AF_INET (used for inter-process communication on the Internet) or AF_UNIX (used for inter-process communication on the same machine). In practice, AF_INET is commonly used
  • Type: Socket Type, which can be SOCK_STREAM (stream socket, used mainly for TCP) or SOCK_DGRAM (datagram socket, used mainly for UDP) (there are many other parameters such as SOCK_RAW, but they are not commonly used)
0.Close and socket socket use is actually very simple, with the file operation is basically the same,3Step:1.Create a socket2.Use sockets to receive/send data3.Close the socket1.Create a TCP socketimport socket
Create a TCP sockets = socket.socket(socket.AF_INET, socket.SOCK_STREAM) ..... Code area...# Close the socket when not in use
s.close()

2.Create a UDP socketimport 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

3. Use sockets for network communication

3.1 Using sockets for UDP communication and sending data to the server

Udp_socket = socket(AF_INET, SOCK_DGRAM) # 2. Dest_addr = ('192.168.1.1', 8080) # addr = ('192.168.1.1', 8080) IP is a string and port is the number # 3. Send_data = input(" Please enter the data to send :") # 4. Send data to the specified program on the specified computer using the sendto() method udp_socket.sendto(send_data.encode(' utF-8 '), dest_addr) # 5. Close the socket udp_socket.close()Copy the code

1. Run The Network Debugging Assistant (Baidu) in Windows. Configure the following port numbers and communication protocols to enable the process to communicate

2. Run the preceding program to send Hello and UDP to the process whose IP address is 192.168.1.1:8080

You can find udp communication data sent in the program, received data in the network debugging assistant, if you try several times to execute the program, send different data to 192.168.1.1:8080, found that although received the data, but the following can be seen that each program uses different ports, the specific reasons are analyzed later.

2. Communicate with socket to receive and send data to “server”

from socket import *
# 1. Create a UDP socket
udp_socket = socket(AF_INET, SOCK_DGRAM)

# 2. Prepare the address of the recipient
dest_addr = ('192.168.1.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('utf-8'), 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

Send data on the program side, and then wait to receive a message from the Network Mode Assistant

 

4. Ports

1. The client port number is randomly assigned.

  • The reason why the number in the circle above is different every time you rerun the network program is that this number identifies the network program, and when you rerun it, if you haven’t decided which one to use, it will be randomly assigned by default
  • Keep in mind that this program uniquely identifies the network program as it runs, 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 (or port)
  • 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. However, if you want to make it a server-side application, it needs to be bound, because the fixed process accessing the server must be unique.

2. Bind port numbers

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 = (' '.9527) 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

The udp test results are as follows: assign a unique port to the server, and the client communicates with it

  • Udp network program, can not bind, the operating system will randomly assigned a port, if you run the program again the port may change
  • A UDP network application can also bind information (IP address, port number), and if the binding is successful, the operating system uses this port number to distinguish between received network data from the process

Unified statement: About the original blog content, there may be some content reference from the Internet, if there is an original link will be quoted; If can not find the original link, in this statement if there is infringement please contact to delete ha. About reprint blog, if have original link will declare; If can not find the original link, in this statement if there is infringement please contact to delete ha.