python套接字无法发送多条消息 - 在分配之前引用数据 -

时间:2014-04-30 07:39:57

标签: python sockets tcp

您好我试图向tcp服务器发送多条消息,但在我的客户端中,我收到一个错误,即在分配之前引用了数据。如果我发送一条消息将没有错误,但如果我尝试发送多个消息,则返回错误。

tcp server:

class Connect(object):

    def __init__(self):
        try:
            self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        except:
            print('socket cannot be created')
        server_address = ('169.254.34.240', 10000)
        #print('starting up: ' + server_address)
        self.sock.bind(server_address)
        self.sock.listen(1)

    def listen(self):
        while True:
            connection, client_address = self.sock.accept()
            print('client connected')
            try:
                data = connection.recv(16)
                print(data)
                if data == "STATUS":
                    connection.sendall("vision=ready")
                elif data == "MEASURE":
                    connection.sendall("vision=computing")
                elif data == "GET_RESULT":
                    connection.sendall("x=1.5,y=0.25,z=0.14,a=0.15")
                else:
                    connection.sendall("wrong command")
            finally:
                connection.close()


def main():
    connect = Connect()
    connect.listen()

if __name__=='__main__':
    main() 

正在发送消息的tcp客户端:

class Connect(object):

    def __init__(self): 
        # Create a TCP/IP socket
        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

        # Connect the socket to the port on the server given by the caller
        print('connecting to host')
        self.sock.connect(('169.254.34.240',10000))

    def send(self, command):
        try:
            message = command
            print('sending: ' + message)
            self.sock.sendall(message)
            amount_received = 0
                amount_expected = len(message)
                while amount_received < amount_expected:
                    data = self.sock.recv(16)
                    amount_received += len(data)
                    print('received: ' + data)
        finally:
            self.sock.close()
            return data

def main():
    connect = Connect()
    print connect.send("STATUS")
    print connect.send("MEASURE")

if __name__=='__main__':
    main()

所以任何人都有一个想法,我想我没有正确结束或者其他什么,我认为我在客户端有什么关系?

2 个答案:

答案 0 :(得分:3)

问题是您在每次请求后调用self.sock.close()而不创建新套接字。每次关闭它后,您将需要创建一个新的套接字。

您可以通过按请求创建连接来解决此问题:

class Connect(object):

    def connect(self):
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        print('connecting to host')
        sock.connect(('127.0.0.1',10000))
        return sock

    def send(self, command):
        sock = self.connect()
        recv_data = ""
        data = True

        print('sending: ' + command)
        sock.sendall(command)

        while data:
            data = sock.recv(1024)
            recv_data += data 
            print('received: ' + data)

        sock.close()
        return recv_data


def main():
    connect = Connect()
    print connect.send("STATUS")
    print connect.send("MEASURE")

答案 1 :(得分:0)

提供完整的堆栈跟踪将有助于指向确切的行,问题出在哪里。学习阅读这些堆栈跟踪,它们看起来很无聊,但提供有价值的信息,如源文件和源文件来源。

我怀疑,在您finally return data的{​​{1}}区块内,您的代码已失效。

data将不会分配值,while amount_received < amount_expected甚至不允许循环中的第一轮,或者如果该循环在行self.sock.recv(16)上发生异常。

顺便说一句:您假设,响应的长度与请求的长度相同,但您的服务器不提供这样长度的响应。