套接字编程

介绍

此为计算机网络中案例的实践,基于Ubuntu服务器实现

UDP套接字编程

相关代码

UDPClient.py

1
2
3
4
5
6
7
8
9
10
11
12
from socket import * # import socket module

serverName = '127.0.0.1' # server name
serverPort = 12000 # server port

clientSocket = socket(AF_INET, SOCK_DGRAM) # create a socket object, AF_INET is the address family for IPv4, SOCK_DGRAM is the socket type for UDP

message = input('Input lowercase sentence:') # input the message to send to the server
clientSocket.sendto(message.encode(), (serverName, serverPort)) #convert the message from the string type to byte type
modifiedMessage, serverAddress = clientSocket.recvfrom(2048) #receive the message from the server,modifiedMessage is the message received from the server, serverAddress is the address of the server
print(modifiedMessage.decode()) #convert the message from the byte type to string type
clientSocket.close() #close the socket

UDPServer.py

1
2
3
4
5
6
7
8
9
10
11
from socket import * # import socket module
serverPort = 12000 # server port

serverSocket = socket(AF_INET, SOCK_DGRAM) # create a socket object, AF_INET is the address family for IPv4, SOCK_DGRAM is the socket type for UDP
serverSocket.bind(('', serverPort)) # bind the socket to the port
print("The server is ready to receive") # print the message to the user
while True: # loop to receive the message from the client
message, clientAddress = serverSocket.recvfrom(2048) # receive the message from the client, message is the message received from the client, clientAddress is the address of the client
modifiedMessage = message.upper() # convert the message to uppercase
serverSocket.sendto(modifiedMessage, clientAddress) # send the message to the client
serverSocket.close() # close the socket

服务器中配置

可以在家目录下构建一个文件夹存放该项目

1
mkdir -p ~/apps/udp_echo && cd ~/apps/udp_echo

检查是否安装python

1
python3 --version

显示版本号说明安装成功,若没有可按照下面操作进行安装:

1
sudo apt install python3

克隆你的项目到目标文件夹,当然没有的话也可以克隆我的

1
git clone [email protected]:cohesivepuma/Computer_Network.git

开始之前请确保你的服务器中开放了UDP12000端口

以阿里云为例子

登录阿里云控制台 → “云服务器 ECS” → 左侧“网络与安全” → “安全组”。找到你的实例所绑定的安全组并进入配置。

在“入方向(入站)规则”页点击“添加安全组规则”。

  • 协议类型:自定义UDP
  • 端口范围:12000/12000(或填写 12000)
  • 授权对象:根据需要填 0.0.0.0/0(任何来源)或指定网段/IP。
  • 优先级保持默认或按需调整,策略选择“允许”。保存即可。

启动程序

在本地电脑中开启UDPClient.py

1
python3 UDPClient.py

在服务器中开启UDPServer.py

1
python3 UDPServer.py

开启测试即可

TCP套接字编程

小插曲

服务器重新安装系统过后用ssh连接可能出现安全问题

1
2
3
4
5
6
7
8
9
10
11
12
13
14
(base) gujunxiang@gujunxiangdeMacBook-Pro ~ % ssh [email protected]
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that a host key has just been changed.
The fingerprint for the ED25519 key sent by the remote host is
SHA256:zbyGP1aHgYZNxh7Die+ubzIou8j44UMoUlkdp671wOE.
Please contact your system administrator.
Add correct host key in /Users/gujunxiang/.ssh/known_hosts to get rid of this message.
Offending ECDSA key in /Users/gujunxiang/.ssh/known_hosts:9
Host key for 106.14.158.150 has changed and you have requested strict checking.
Host key verification failed.

需要删除旧主机的指纹记录

1
ssh-keygen -R xxx.xxx.xxx.xxx

然后重新连接就可以了