我的TCP服务器仅发送b'',而不发送腌制的数据

时间:2019-01-29 08:41:46

标签: python python-3.x sockets tcpclient tcpserver

我有一个TCP服务器和一个Kivy应用程序客户端,初始化后,我将发送服务器代码90010并获得所有数据,但是当我移到另一个需要再次接收数据的屏幕时,我只会收到b'',但是在在其他屏幕上,我只需要发送数据即可,并且运行良好,老实说,我实在无法理解为什么它发送空字节字符串的任何帮助都值得赞赏

我知道代码已经达到if服务器代码== 919点,因此它应该发送数据,但是什么也没收到!,我还在每个屏幕的初始化上都加上了s.connect,这可能会引起问题但是我完全不确定

此SERVERCODE的TCP代码位于clientthread类中,它使用与具有GetLength()的客户端相同的方法来获取服务器代码

class clientthread(thread)

    def run(self)
        while true:
            SERVERCODE = self.getlength
            if SERVERCODE == b''
                break
            else:
                while true:

                    elif SERVERCODE == 90010:
                        global Todays_refgroup
                        print(Todays_refgroup)
                        print(statuslist)
                        TF_Pickles = pickle.dumps(Todays_refgroup)
                        INIT_Length = self.Pack_data(TF_Pickles)
                        TF_Status = pickle.dumps(statuslist)
                        INIT_Status = self.Pack_data(TF_Status)
                        conn.sendall(INIT_Length)
                        conn.sendall(TF_Pickles)
                        conn.sendall(INIT_Status)
                        conn.sendall(TF_Status)
                        break

                   elif SERVERCODE == 919:
                        pickle_status = pickle.dumps(statuslist)
                        len_statpick = self.Pack_data(pickle_status)
                        conn.sendall(len_statpick)
                        conn.sendall(pickle_status)
                        break

TCP_IP = '127.0.0.1'
TCP_PORT = 8079
BUFFER_SIZE = 1024

tcpServer = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcpServer.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
tcpServer.bind((TCP_IP, TCP_PORT))
threads = []

while True:
    tcpServer.listen(4)
    print("                                  : Waiting for connections from TCP clients...")
(conn, (ip, port)) = tcpServer.accept()
newthread = ClientThread(ip, port)
newthread.start()
threads.append(newthread)

客户代码

    servercode = StructPack(919)
    self.s.sendall(servercode)
    buf = GetLength(self.s)
    stats = GetSpecificAmount(self.s, buf)
    print(stats)

def GetLength(socket):
    buf = b''
    while len(buf) < 4:
        # print('waiting...')
        tbuf = socket.recv(4 - len(buf))
        if tbuf == '':
            raise RuntimeError("Lost connection with peer")
        buf += tube

    SERVERCODE = struct.unpack('!I', buf)[0]
    return SERVERCODE

def GetSpecificAmount(socket, Amount_2Recieve):
    data = []
    len_recvd = 0
    while len_recvd < Amount_2Recieve:
        buf = socket.recv(Amount_2Recieve - len_recvd)
        if buf == '':
            raise RuntimeError("Lost connection with peer")
        data.append(buf)
        len_recvd += len(buf)
        # print(len_recvd)
   df = pickle.loads(b''.join(data))
   return df

0 个答案:

没有答案