Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>A network stream does not end until it is closed, and <em>by default</em> protobuf (as defined by Google) consumes to the end of the stream. It is waiting for more data, or for your stream to conclusively end. If you are sending multiple messages, or just want to keep the stream open, then replace <code>Serialize</code> and <code>Deserialize</code> with <code>SerializeWithLengthPrefix</code> and <code>DeserializeWithLengthPrefix</code>. This will add extra information to let it get an individual message without closing the stream. It is important that both ends of the pipe know that they are using this variant.</p> <hr> <p>Full example (this is using core .NET, but should translate without issue now):</p> <pre><code>using System; using System.IO; using System.Net.Sockets; using System.Threading; using ProtoBuf; [ProtoContract] public class Player { [ProtoMember(1)] public int flag; [ProtoMember(2)] public Int16 id; [ProtoMember(3, DataFormat = DataFormat.Group)] public MyVector3 CharPos; [ProtoMember(7)] public bool spawned; } public struct MyVector3 { public readonly float X, Y, Z; public MyVector3(float x, float y, float z) { X = x; Y = y; Z = z; } public override string ToString() { return string.Format("({0},{1},{2})", X, Y, Z); } } static class Program { static ManualResetEvent evt = new ManualResetEvent(false); static void Main(string[] args) { var player = new Player() {CharPos = new MyVector3(1, 2, 3), flag=123, id=456, spawned=true}; ThreadPool.QueueUserWorkItem(x =&gt; { Console.WriteLine("client: waiting for server"); evt.WaitOne(); Console.WriteLine("client: opening connection"); using (var client = new TcpClient("localhost", 15000)) using (var ns = client.GetStream()) { serialize(ns, player); ns.Flush(); Console.WriteLine("client: wrote player"); Console.WriteLine("client: waiting for response"); while (ns.ReadByte() &gt;= 0) { Console.WriteLine("client: receiving..."); } Console.WriteLine("client: connection closed by server"); ns.Close(); } }); TcpListener serverSocket = new TcpListener(15000); TcpClient clientSocket; serverSocket.Start(); Console.WriteLine("server: accepting connections"); evt.Set(); while (true) { Console.WriteLine("server: waiting for client..."); clientSocket = serverSocket.AcceptTcpClient(); Console.WriteLine("server: got client"); using (NetworkStream networkStream = clientSocket.GetStream()) { var fromNetwork = deserialize(networkStream); Console.WriteLine("server: got player"); Console.WriteLine("&gt; flag: {0}", fromNetwork.flag); Console.WriteLine("&gt; id: {0}", fromNetwork.id); Console.WriteLine("&gt; spawned: {0}", fromNetwork.spawned); Console.WriteLine("&gt; pos: {0}", fromNetwork.CharPos); } } } public static void serialize(Stream dest, Player player) { if (player == null) throw new ArgumentNullException(); Serializer.SerializeWithLengthPrefix(dest, player, PrefixStyle.Base128); } public static Player deserialize(Stream inc) { Player obj = Serializer.DeserializeWithLengthPrefix&lt;Player&gt;(inc, PrefixStyle.Base128); return obj; } } </code></pre> <p>This gives:</p> <pre><code>client: waiting for server server: accepting connections server: waiting for client... client: opening connection server: got client client: wrote player client: waiting for response server: got player &gt; flag: 123 &gt; id: 456 &gt; spawned: True &gt; pos: (1,2,3) client: connection closed by server server: waiting for client... </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload