Java 如何多次接收字符串和对象的协议

Java 如何多次接收字符串和对象的协议,java,c#,protocol-buffers,protobuf-net,Java,C#,Protocol Buffers,Protobuf Net,我使用的是服务器和客户端。如果收到字符串“yes”,客户端只应反序列化接收到的protobuf消息 编辑: protobuf的第一条消息接收良好。但是,如果我想一次发送多条消息,它会给我: System.OverflowException:数字溢出。 位于ProtoBuf.ProtoReader.tryreaduint32变量(System.IO.Stream源、System.UInt32和值) 我通读了这个链接,但我不知道在我的情况下我应该怎么做 我正在使用TCP套接字。下面是一个示例代码:

我使用的是服务器和客户端。如果收到字符串“yes”,客户端只应反序列化接收到的protobuf消息

编辑:

protobuf的第一条消息接收良好。但是,如果我想一次发送多条消息,它会给我:

System.OverflowException:数字溢出。 位于ProtoBuf.ProtoReader.tryreaduint32变量(System.IO.Stream源、System.UInt32和值)

我通读了这个链接,但我不知道在我的情况下我应该怎么做

我正在使用TCP套接字。下面是一个示例代码:

客户C#:

TcpClient TcpClient=新的TcpClient(主机、端口);
NetworkStream netStream=tcpClient.GetStream();
StreamReader=新的StreamReader(netStream);
while(true)
{
//一个接一个地阅读多条消息。
字符串消息=reader.ReadLine();
if(message.Equals(“yes”))
{
Command=Serializer.DeserializeWithLengthPrefix(netStream,PrexiStyle.Base128);
BinaryWriter bw=新的BinaryWriter(File.Open(path,FileMode.Create));
Write(command.File);
bw.Flush();
bw.Close();
}
}
服务器Java:

OutputStream outputStream = clientSocket.getOutputStream();
PrintWriter writer = new PrintWriter(outputStream, true);
try{
    // send "yes" and the protobuf messages ten times one after another
    for(int i=0; i<10; i++)
    {
        writer.println("yes");
        command.writeDelimitedTo(outputStream);
    }
}catch(Exception e)
    e.printStackTrace();
}
finally{
    outputStream.close();
    clientSocket.close();
}
OutputStream OutputStream=clientSocket.getOutputStream();
PrintWriter writer=新的PrintWriter(outputStream,true);
试一试{
//一个接一个地发送十次“是”和protobuf消息

对于(inti=0;i尝试在不同的套接字中分离protobuf数据和其他数据。添加了一个while循环,以便能够从服务器读取多条消息

using System.Threading;
new Thread(() => 
{
    Thread.CurrentThread.IsBackground = true; 
    // Protobuf data is read on this socket
    TcpClient tcpClient = new TcpClient(host, port);
    NetworkStream netStream = tcpClient.GetStream();
    StreamReader reader = new StreamReader(netStream);

    BinaryWriter bw = new BinaryWriter(File.Open(path, FileMode.Create));
    bool running = true;
    while(running)
    {
        bw.Write(Serializer.DeserializeWithLengthPrefix<Command>(netStream, PrexiStyle.Base128).File);
    }
    bw.Flush();
    bw.Close();
}).Start();

// Other data is read no this socket
TcpClient tcpClientForOtherStuff = new TcpClient(host, port);
NetworkStream netStream = tcpClientForOtherStuff.GetStream();
StreamReader readerForOtherStuff = new StreamReader(netStream);
string message;
bool running = true;
BinaryWriter bwForOtherStuff = new BinaryWriter(File.Open(path2, FileMode.Create));
bool running = true;
while(running) bwForOtherStuff.Write(readerForOtherStuff.ReadLine());
bwForOtherStuff.Flush();
bwForOtherStuff.Close();
使用系统线程;
新线程(()=>
{
Thread.CurrentThread.IsBackground=true;
//Protobuf数据在此套接字上读取
TcpClient TcpClient=新的TcpClient(主机、端口);
NetworkStream netStream=tcpClient.GetStream();
StreamReader=新的StreamReader(netStream);
BinaryWriter bw=新的BinaryWriter(File.Open(path,FileMode.Create));
bool running=true;
(跑步时)
{
Write(Serializer.DeserializeWithLengthPrefix(netStream,PrexiStyle.Base128.File));
}
bw.Flush();
bw.Close();
}).Start();
//此套接字不读取其他数据
TcpClient tcpClientForOtherStuff=新的TcpClient(主机、端口);
NetworkStream netStream=tcpClientForOtherStuff.GetStream();
StreamReader readerForOtherStuff=新的StreamReader(netStream);
字符串消息;
bool running=true;
BinaryWriter bwForOtherStuff=新的BinaryWriter(File.Open(path2,FileMode.Create));
bool running=true;
(运行)bwForOtherStuff.Write(readerForOtherStuff.ReadLine())时;
bwForOtherStuff.Flush();
bwForOtherStuff.Close();

我还没有测试或编译代码。

尝试在不同的套接字中分离protobuf数据和其他数据。添加了一个while循环,以便能够从服务器读取多条消息

using System.Threading;
new Thread(() => 
{
    Thread.CurrentThread.IsBackground = true; 
    // Protobuf data is read on this socket
    TcpClient tcpClient = new TcpClient(host, port);
    NetworkStream netStream = tcpClient.GetStream();
    StreamReader reader = new StreamReader(netStream);

    BinaryWriter bw = new BinaryWriter(File.Open(path, FileMode.Create));
    bool running = true;
    while(running)
    {
        bw.Write(Serializer.DeserializeWithLengthPrefix<Command>(netStream, PrexiStyle.Base128).File);
    }
    bw.Flush();
    bw.Close();
}).Start();

// Other data is read no this socket
TcpClient tcpClientForOtherStuff = new TcpClient(host, port);
NetworkStream netStream = tcpClientForOtherStuff.GetStream();
StreamReader readerForOtherStuff = new StreamReader(netStream);
string message;
bool running = true;
BinaryWriter bwForOtherStuff = new BinaryWriter(File.Open(path2, FileMode.Create));
bool running = true;
while(running) bwForOtherStuff.Write(readerForOtherStuff.ReadLine());
bwForOtherStuff.Flush();
bwForOtherStuff.Close();
使用系统线程;
新线程(()=>
{
Thread.CurrentThread.IsBackground=true;
//Protobuf数据在此套接字上读取
TcpClient TcpClient=新的TcpClient(主机、端口);
NetworkStream netStream=tcpClient.GetStream();
StreamReader=新的StreamReader(netStream);
BinaryWriter bw=新的BinaryWriter(File.Open(path,FileMode.Create));
bool running=true;
(跑步时)
{
Write(Serializer.DeserializeWithLengthPrefix(netStream,PrexiStyle.Base128.File));
}
bw.Flush();
bw.Close();
}).Start();
//此套接字不读取其他数据
TcpClient tcpClientForOtherStuff=新的TcpClient(主机、端口);
NetworkStream netStream=tcpClientForOtherStuff.GetStream();
StreamReader readerForOtherStuff=新的StreamReader(netStream);
字符串消息;
bool running=true;
BinaryWriter bwForOtherStuff=新的BinaryWriter(File.Open(path2,FileMode.Create));
bool running=true;
(运行)bwForOtherStuff.Write(readerForOtherStuff.ReadLine())时;
bwForOtherStuff.Flush();
bwForOtherStuff.Close();

我还没有测试或编译代码。

看看我的答案,谢谢你的回答!但是,你在第一个回答的问题中说,函数“writeDelimitedTo”确实会写入大小,然后发送消息本身。因此,在C中,我阅读带有“DeserializeWithLengthPrefix”的分隔消息.但是,在我发送protobuf消息之前,我必须发送一个字符串。那么,我如何才能做到这一点呢?我认为protobuf使用上述函数写入和读取大小…但这似乎不是正确的方法。您可以发布其余代码吗?读卡器与netStream相同吗?
是的
是您自己的定界符,您需要这个吗用于protobuf以外的其他通信的ocket?也尝试在两端打印整个缓冲区,并将其与
DeserializeWithLengthPrefix
试图反序列化的内容进行比较。@petersv我编辑了我的问题并添加了更多必要的代码。读者从netStream读取,是的。是的,我需要区分protobuf消息和一条具有简单数据类型的消息。
tcpClient
client
是否相同?请看我的答案,谢谢你的回答!但是,你在第一个回答的问题中说,函数“writeDelimitedTo”确实写入大小,然后发送消息本身。因此,在C中,我用“使用长度前缀反序列化".但是,在我发送protobuf消息之前,我必须发送一个字符串。那么,我如何才能做到这一点呢?我认为protobuf使用上述函数写入和读取大小…但这似乎不是正确的方法。您可以发布其余代码吗?读卡器与netStream相同吗?
是的
是您自己的定界符,您需要这个吗用于protobuf以外的其他通信的ocket?也尝试在两端打印整个缓冲区,并将其与
DeserializeWithLengthPrefix
试图反序列化的内容进行比较。@petersv我编辑了我的问题并添加了更多必要的代码。读者从netStream读取,是的。是的,我需要区分protobuf消息还有一条简单的da信息