import json
import socket
import threading
import logging
import time
from unicodedata import decimal
#主函数
def main():
udp_receive()
def udp_receive():
# 1.创建socket套接字 收
udp_socket_receive = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # AF_INET表示使用ipv4,默认不变,SOCK_DGRAM表示使用UDP通信协议
# 2.绑定端口port 收
local_addr_receive = ("127.0.0.1", 45678) # 默认本机任何ip
udp_socket_receive.bind(local_addr_receive) # 绑定端口
# 1.创建socket套接字 发
udp_socket_send = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # AF_INET表示使用ipv4,默认不变,SOCK_DGRAM表示使用UDP通信协议
# 2.绑定端口port 收
local_addr_send = ("127.0.0.1", 45671) # 默认本机任何ip
udp_socket_send.bind(local_addr_send) # 绑定端口
print('Let\'s go useudp ')
while True:
try:
recv_data = udp_socket_receive.recvfrom(1024) # 定义单次最大接收字节数
print(recv_data)
send_data = input("请输入您想要发送的数据:")
udp_socket_send.sendto(send_data.encode("utf-8"), ("127.0.0.1", 45679))
except socket.timeout:
print
"time out"
connection.close()
print
"a client exit..."
if __name__ == '__main__':
main()
(1)经过测试,可以与C#正常发送数据。