我想创建一个服务器,它从智能手机接收消息并基于它们,将消息发送到一个特定的IP和端口。但是当我定义两个IP(一个用于服务器而另一个用于发送消息)时,会出现错误,即我不能使用套接字超过一次。我不是程序员,所以请帮我纠正我的代码。
import socket
import time
host = '192.168.100.41' #Define a server to receive messages
port = 5000
s = socket.socket()
s.bind((host,port))
s.listen(1)
c, addr = s.accept()
print ("Connection from: ")
while True:
Buffer = c.recv(1024)
if not data:
break
address = ( '192.168.100.60', 5555) #Defind Destination address to send a message (must match destination IP and port)
client_socket = socket(AF_INET, SOCK_DGRAM) #Set Up the Socket
client_socket.settimeout(1) #only wait 1 second for a resonse
def ON(evt):
data = "1ON" #Set data
client_socket.sendto(data, address) #send command
try:
rec_data, addr = client_socket.recvfrom(2048) #Read response
print (rec_data) #Print the response
except:
pass