如果没有收到数据并且数据长度未知,如何在python [TCP]

时间:2018-09-04 01:01:48

标签: python sockets while-loop tcpclient

我是python和套接字编程的新手。我正在尝试将数据发送到客户端,并且数据长度未知。在客户端接收到所有数据后,由于while循环,程序未终止(请参见下面的代码)。我还使用了命令(如果没有消息:break),但它也无法正常工作。 第二个是丢包问题。当我不给时间的时候。发送方的sleep()然后接收方错过了一些数据包(发送方发送所有数据包的接收方未接收到所有数据包)。 没有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()

1 个答案:

答案 0 :(得分:0)

保持打开和关闭套接字的资源非常消耗,我将把它们从while循环中删除。另外,我不记得s是否是一个阻塞函数。但这应该可以帮助您正确地退出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()

对于recv,我使用了本地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循环顺序。首先建立连接,然后发送消息,然后关闭连接。