我正在尝试开发服务器-客户端类型的应用程序。我想从服务器向客户端发送一条消息(如下所示)。并非所有客户端都直接与服务器(多跳)通信连接。我想在客户端(接收方)收到消息时在发送方(服务器)得到确认。如果假设服务器未收到确认,则服务器将再次向该客户端重新发送相同的消息。 在我的代码中,我尝试将与ack相同的消息发送回(从cleint到服务器)。服务,但我的错误点中断了。
任何机构都可以帮助解决此错误吗? 我如何发送确认。邮件从接收人(接收者)发送到服务器(发件人)
预先感谢
import socket
import os,sys
import time
import concurrent.futures
from multiprocessing import Process
发送方
MyNeighborSet_ip= ['192.168.1.2']
Data_transfer_listt = [['192.168.1.1', '192.168.1.2'], ['192.168.1.2', '192.168.1.3'], ['192.168.1.2', '192.168.1.4'], ['192.168.1.4', '192.168.1.5'], ['192.168.1.4', '192.168.1.6']]
received_list=[]
def sending_Neighobr_ip_list():
message='Neighbor_list_sending'
PORT = 12345
for k in range (len(MyNeighborSet_ip)):
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((MyNeighborSet_ip[k],PORT))
s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
for i in range(len(Data_transfer_listt)):
receiver_ip=Data_transfer_listt[i][0]
receiver_node_list=Data_transfer_listt[i][1]
T_message= message + ";" + receiver_ip + ";" + receiver_node_list
T_message_bytes= bytes(T_message,'utf-8')
print("sending message",T_message_bytes)
s.sendall (T_message_bytes)
time.sleep(0.01)
s.close()
def receive_ack():
PORT = 12345
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
s.bind(('',PORT))
s.listen(1)
conn,address=s.accept()
while True:
message_chunk=conn.recv(1024).decode()
if not message_chunk:
break
received_list.append(message_chunk)
conn.close()
print ("after the recv_message_list",received_list)
return
temp= concurrent.futures.ThreadPoolExecutor (max_workers=2)
temp.submit(sending_Neighobr_ip_list)
temp.submit(receive_ack)
接收器端
data =[]
ip_list=[]
def frame_reception_function ():
PORT = 12345
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
s.bind(('',PORT))
s.listen(1)
conn,address=s.accept()
while True:
datachunk=conn.recv(1024).decode()
#print (conn)
print (datachunk)
if not datachunk:
break
data.append(datachunk)
ip_list.append(address[0])
T_message_bytes= bytes(datachunk,'utf-8')
s.sendall(T_message_bytes)
conn.close()
return
if __name__ == '__main__':
p1 = Process(target=frame_reception_function)
p1.start()
p1.join()