Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python中编程协议缓冲区以通过套接字发送消息_Python_Sockets_Protocol Buffers - Fatal编程技术网

如何在python中编程协议缓冲区以通过套接字发送消息

如何在python中编程协议缓冲区以通过套接字发送消息,python,sockets,protocol-buffers,Python,Sockets,Protocol Buffers,我正在尝试使用python中的协议缓冲区将消息从一台计算机发送到另一台计算机。我从网上的几个例子中学到了一些东西,并试图从中找出一个最简单的例子。但是,在我的代码中,服务器没有打印出客户端发送的变量的正确值。非常感谢您的帮助 首先,test_msg.proto如下所示: message test_msg { required int32 param1 = 1; required int32 param2 = 2; } 第二,客户端代码(test_client.py) 然后,在启

我正在尝试使用python中的协议缓冲区将消息从一台计算机发送到另一台计算机。我从网上的几个例子中学到了一些东西,并试图从中找出一个最简单的例子。但是,在我的代码中,服务器没有打印出客户端发送的变量的正确值。非常感谢您的帮助

首先,test_msg.proto如下所示:

message test_msg {
    required int32 param1 = 1;
    required int32 param2 = 2;
}
第二,客户端代码(test_client.py)

然后,在启动服务器之后,我执行客户机,它在发送参数10次时打印出以下10行

[client] param1: 100 param2: 5
[client] param1: 100 param2: 5
[client] param1: 100 param2: 5
[client] param1: 100 param2: 5
[client] param1: 100 param2: 5
[client] param1: 100 param2: 5
[client] param1: 100 param2: 5
[client] param1: 100 param2: 5
[client] param1: 100 param2: 5
[client] param1: 100 param2: 5
但是,在服务器端,它会打印出来

Listening
[server] param1: 0 param2: 0
Listening
[server] param1: 0 param2: 0
Listening
[server] param1: 0 param2: 0
Listening
[server] param1: 0 param2: 0
Listening
[server] param1: 0 param2: 0
Listening

因此,未打包的编号param1和param2不正确,它只收到了一半的消息。非常感谢您的帮助。

第一条建议:简化。首先,让我们看看是否可以在没有协议缓冲区的情况下实现这一点:

client.py server.py 现在,我不是套接字方面的专家(这可能是我回答的第一个关于套接字的问题),但是如果运行这个示例,您将获得预期的输出。这告诉我,每次发送都对应一个
.recv
。大概,
sockets
库正在处理长度的细节。当然,在接收端,你很可能想知道长度,这样你就可以通过一个合适的maxlen。如果是这种情况,那么我想你可能需要发送两条消息。第一条消息的长度为4,为整数长度。第二条消息应该包含数据。e、 g:

client2.py 服务器2.py
现在序列化应该很容易了。毕竟,proto缓冲区可以很好地序列化为字符串。

协议缓冲区允许您定义远程过程调用(RPC)。这允许您以声明性、语言无关的方式在.proto文件中定义服务器和客户端之间的接口。一旦定义了服务并编译了protos,就可以使用RPC库抽象掉所有序列化/网络代码。看一看,它是谷歌内部RPC库的开源版本,提供了一种非常干净的方式来定义服务器和客户端。

非常感谢您的帮助!您的代码运行良好。然而,由于我计划使用UDP协议(sockdgram),我想知道发送两个包是否是一种好方法。例如,如果pack1丢失,则TotalEnrecv可能无法获得解包第二条消息的正确长度。再次感谢。
[client] param1: 100 param2: 5
[client] param1: 100 param2: 5
[client] param1: 100 param2: 5
[client] param1: 100 param2: 5
[client] param1: 100 param2: 5
[client] param1: 100 param2: 5
[client] param1: 100 param2: 5
[client] param1: 100 param2: 5
[client] param1: 100 param2: 5
[client] param1: 100 param2: 5
Listening
[server] param1: 0 param2: 0
Listening
[server] param1: 0 param2: 0
Listening
[server] param1: 0 param2: 0
Listening
[server] param1: 0 param2: 0
Listening
[server] param1: 0 param2: 0
Listening
import socket
import sys
import struct

address = ('localhost', 6005)
client_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
client_socket.connect(address)

messages = ["foobar", "barbaz", "bazquxfad", "Jimmy Carter"]

for s in messages:

    totallen = 4 + len(s) 
    pack1 = struct.pack('>I', totallen) # the first part of the message is length
    client_socket.sendall(s)
import socket
import sys
import struct

address = ('localhost', 6005)
server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
server_socket.bind(address)

while True:
    print "Listening"

    # totallen = server_socket.recv(4)
    # totallenRecv = struct.unpack('>I', totallen)[0]
    # messagelen = totallenRecv - 4 
    message = server_socket.recv(1000)

    print message
import socket
import sys
import struct

address = ('localhost', 6005)
client_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
client_socket.connect(address)

messages = ["foobar", "barbaz", "bazquxfad", "Jimmy Carter"]

for s in messages:

    totallen = len(s) 
    pack1 = struct.pack('>I', totallen) # the first part of the message is length
    client_socket.sendall(pack1)
    client_socket.sendall(s)
import socket
import sys
import struct

address = ('localhost', 6005)
server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
server_socket.bind(address)

while True:
    print "Listening"

    totallen = server_socket.recv(4)
    totallenRecv = struct.unpack('>I', totallen)[0]

    message = server_socket.recv(totallenRecv)

    print message