源代码
import socket
def main():
udp_socket=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
udp_socket.sendto(b"udp-test",("192.168.174.1",8080))
udp_socket.close()
if __name__=="__main__":
main()
运行代码
接收端效果
如需要重复发送,代码如下
import socket
def main():
udp_socket=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
while True:
data_str=input("请输入您要发送的数据")
udp_socket.sendto(data_str.encode("utf-8"),("192.168.174.1",8080))
udp_socket.close()
if __name__=="__main__":
main()