Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7076761
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:20:16+00:00 2026-05-28T06:20:16+00:00

I want to serialize a ‘Player’ class and send it over my network stream

  • 0

I want to serialize a ‘Player’ class and send it over my network stream to a client.

Player Class

    [ProtoMember(1)]
    public int flag;
    [ProtoMember(2)]
    public Int16 id;
    [ProtoMember(3)]
    public MyVector3 CharPos;
    [ProtoMember(7)]
    public bool spawned;

MyVector3 (due to the fact that protobuf does not support serialization of Vector3)

[ProtoContract]
public class MyVector3
{
    [ProtoMember(4)]
    public float X { get; set; }

    [ProtoMember(5)]
    public float Y { get; set; }

    [ProtoMember(6)]
    public float Z { get; set; }

    public MyVector3()
    {
        this.X = 0.0f;
        this.Y = 0.0f;
        this.Z = 0.0f;
    }

    public MyVector3(float x, float y, float z)
    {
        this.X = x;
        this.Y = y;
        this.Z = z;
    }

    public static implicit operator Vector3(MyVector3 v)
    {
        return new Vector3(v.X, v.Y, v.Z);
    }

    public static implicit operator MyVector3(Vector3 v)
    {
        return new MyVector3(v.X, v.Y, v.Z);
    }
}

Serialize and Deserialize Function

public byte[] serialize(Object obj)
    {
        if (obj == null)
        {
            return null;
        }
        MemoryStream ms = new MemoryStream();
        Serializer.SerializeWithLengthPrefix(ms,obj,PrefixStyle.Base128);
        return ms.ToArray();
    }

    public Player deserialize(NetworkStream inc)
    {
        Player obj = Serializer.DeserializeWithLengthPrefix<Player>(inc,PrefixStyle.Base128);
        return obj;
    }

Test Function (which in fact does not work!)

    static void Main(string[] args)
    {
        TcpListener serverSocket = new TcpListener(8888);
        TcpClient clientSocket = default(TcpClient);

        Serialize ser = new Serialize();

        Player temp = new Player();

        serverSocket.Start();

        while (true)
        {
            clientSocket = serverSocket.AcceptTcpClient();
            using (NetworkStream networkStream = clientSocket.GetStream())
            {
                DeserializeTest(ser,networkStream);
                SerializeTest(ser, networkStream);
            }
        }

    }

    static void SerializeTest(Serialize ser,NetworkStream networkStream)
    {
        Player temp = new Player();
        BinaryWriter writer = new BinaryWriter(networkStream);
        writer.Write(ser.serialize(temp));
        networkStream.Flush();
    }

    static void DeserializeTest(Serialize ser, NetworkStream networkStream)
    {
        Player temp = new Player();
        temp = (Player)ser.deserialize(networkStream);
        Console.WriteLine(temp.flag.ToString() + temp.CharPos.ToString());
        networkStream.Flush();
    }
}

What now happens:

When the DeserializeTest is launched, and protobuf tries to deserialize the data from the networkStream the whole function kind of “Freezes” and starts to loop.
When I’m only serializing the data (in the client) and send it to the server (this code) where its deserialized, everything works like a charm.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T06:20:17+00:00Added an answer on May 28, 2026 at 6:20 am

    A network stream does not end until it is closed, and by default 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 Serialize and Deserialize with SerializeWithLengthPrefix and DeserializeWithLengthPrefix. 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.


    Full example (this is using core .NET, but should translate without issue now):

    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 =>
            {
                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() >= 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("> flag: {0}", fromNetwork.flag);
                    Console.WriteLine("> id: {0}", fromNetwork.id);
                    Console.WriteLine("> spawned: {0}", fromNetwork.spawned);
                    Console.WriteLine("> 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<Player>(inc, PrefixStyle.Base128);
            return obj;
        }
    }
    

    This gives:

    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
    > flag: 123
    > id: 456
    > spawned: True
    > pos: (1,2,3)
    client: connection closed by server
    server: waiting for client...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to serialize an object and send it over the network. I have
I want to serialize this class: public class CarDisplay { public string Name {
I want to serialize a simple object to JSON: public class JsonTreeNode { [DataMember(Name
I want to serialize my enum-value as an int, but i only get the
I want to serialize a class to xml and store that in a field
I have a class which contains a list of items. I want to serialize
I've got a class that serializes data. I may want to serialize this data
I want to serialize an object. I've got this basic class structure: class Controller
I have a class that I want to serialize to xml. The class looks
I have a std::vector<int> and I want serialize it. For this purpose I am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.