我正在尝试开发服务器-客户端应用程序并进行多跳通信(一个客户端将数据发送到邻居客户端。所有客户端均未直接与服务器连接)。我正在发送消息,我需要接收者的确认消息。如果发件人收到确认。来自接收方的消息,然后确定,否则发送方需要重新发送他未收到确认的消息。我正在尝试,但无法收到确认。发件人的邮件。
有人可以帮助我如何使用python中的TCP套接字连接并行地在发送方获得确认消息(并行意味着在发送消息期间他还应该从接收方接收确认消息)? ...
,以及在接收方客户端不是目的地的情况下如何将消息转发到邻居客户端(需要与同步相关的帮助)
第二个问题是,当我向收件人(客户端)发送消息时,我没有正确接收消息。我需要给睡眠时间,然后我会在接收方正确接收所有消息,否则我会收到一条消息,而不是一条单独的消息。
如何在不给发件人睡眠的情况下单独获得所有消息? 我是否需要在TCP层之上构建消息协议?
import os,sys
import socket
import time
import concurrent.futures
服务器(发件人)
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')
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:
print (datachunk)
print (' ')
if not datachunk:
break
data.append(datachunk)
ip_list.append(address[0])
conn.close()
Send_Data_ack()
#conn.close()
print(data)
return
def Send_Data_ack():
mess=data.pop(0)
ips=ip_list.pop(0)
PORT = 12345
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
s.connect((ips,PORT))
s.connect((ips,PORT))
T_message_bytes= bytes(mess,'utf-8')
s.sendall (T_message_bytes)
s.close ()
frame_reception_function()