如果 python [TCP] 中未接收到数据且数据长度未知,如何打破 while 循环

How to break the while loop if data is not receiving and data length is unknow in python [TCP]

我是 python 和套接字编程的新手。我正在尝试向客户端发送数据,但数据长度未知。在客户端接收到所有数据后,程序不会因为 while 循环而终止(见下面的代码)。我也使用命令(如果不是消息:break)但它也不起作用。 第二个是丢包问题。当我不给的时候。在发送方休眠 () 然后接收方丢失了一些数据包(在发送方发送所有数据包的接收方未收到所有数据包)。 如果没有 sys.exit 命令,我如何从 while 循环中退出? 和....如何在不使用 time.sleep 函数的情况下处理第二个问题。 如果有人能帮助我,我将不胜感激。 谢谢

[接收节点]

import socket
import os,sys


def frame_reception_function ():
    while True:
        PORT = 123
        s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
        s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
        s.bind(('',PORT))
        s.listen()
        conn,address=s.accept()
        message=conn.recv(4096).decode()
        print (message)
        conn.close()
frame_reception_function()

[发件人节点]

import os,sys
import socket
import time


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']]



def sending_Neighobr_ip_list():    
    #nn1=n1
    message='Neighbor_list_sending'
    #print (len(Data_transfer_listt))

    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')
        PORT = 123
        print ("just after socket")
        for k in range (len(MyNeighborSet_ip)):
            s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
            s.connect((MyNeighborSet_ip[k],PORT))
            s.sendall (T_message_bytes) 
            s.close()
            time.sleep(0.01)


sending_Neighobr_ip_list()

不断打开和关闭套接字会占用大量资源,我将把它们从 while 循环中移除。另外,我不记得 recv 是否是阻塞函数。但这应该可以帮助您正确地跳出 while 循环并解决您的第一个问题。

接收者:

def frame_reception_function ():
    data = []
    PORT = 123
    s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
    s.bind(('0.0.0.0',PORT))
    s.listen(1)
    conn,address=s.accept()  # accept an incoming connection using accept() method which will block until a new client connects
    while True:
        datachunk=conn.recv(1024) # reads data chunk from the socket in batches of 4086 bytes using method recv() until it returns an empty string
        if not datachunk:
            break  # no more data coming in, so break out of the while loop
        data.append(datachunk)  # add chunk to your already collected data

    conn.close()
    print(data)
    return

frame_reception_function()

发件人:

import os,sys
import socket
import time

MyNeighborSet_ip= [<THE IP ADDRESS OF YOUR RECEIVER>]

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']]

def sending_Neighobr_ip_list():
    #nn1=n1
    message='Neighbor_list_sending'
    #print (len(Data_transfer_listt))
    PORT = 123

    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)

            print("sending message")
            s.sendall (T_message_bytes)

        s.close()

sending_Neighobr_ip_list()

对于 THE IP ADDRESS OF YOUR RECEIVER,我使用了我的本地 IP 地址 (192.168.x.x)。

这是接收端的输出:

['Neighbor_list_sending;192.168.1.1;192.168.1.2', 'Neighbor_list_sending;192.168.1.2;192.168.1.3', 'Neighbor_list_sending;192.168.1.2;192.168.1.4', 'Neighbor_list_sending;192.168.1.4;192.168.1.5', 'Neighbor_list_sending;192.168.1.4;192.168.1.6']

问题出在发送方,您每次迭代都关闭套接字。这使得接收者也关闭了它的连接。所以发件人成功发送了第一条消息,然后在第二个连接上出错,导致接收者已关闭并且不在寻找连接。相反,切换 for 循环顺序。先建立连接,然后发送消息,然后关闭连接。