Home Forums Support How to send and receive object type

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #2960
    Anonymous
    Inactive

    Hello,Object type cannot send and receive?

    #2961
    Anonymous
    Inactive

    Heya a77a77a777,

    You cannot receive a generic object type. It is impossible to determine what should happen with incoming byte[] if the final object type is unknown. This is not a limitation of NetworkComms.Net, rather a limitation of converting objects to byte[] in order to be sent over a network.

    Marc

    #2962
    Anonymous
    Inactive

    Hello, I use byte[] to receive, unable to deserialize

    The sending end:

    string mgs = Console.ReadLine();
    User uu = new User()
    {
    Id = 1,
    Name = mgs
    };
    MemoryStream stream = new MemoryStream();
    IFormatter formatter = new BinaryFormatter();
    formatter.Serialize(stream, uu);
    stream.Flush();
    byte[] theBytes = stream.ToArray();
    stream.Close();
    Console.WriteLine(NetworkComms.SendReceiveObject<byte[], string>(“Message”, “127.0.0.1”, 6003, “hehe”, 1000, theBytes));
    Console.ReadLine();

    The receiving end:

    NetworkComms.AppendGlobalIncomingPacketHandler<byte[]>(“Message”, (header, connection, message) =>
    {
    IFormatter formatter = new BinaryFormatter();
    MemoryStream stream = new MemoryStream(message);
    User t = formatter.Deserialize(stream) as User;
    stream.Close();
    connection.SendObject(“hehe”, t.Name);
    });
    Connection.StartListening(ConnectionType.TCP,new IPEndPoint(IPAddress.Parse(“127.0.0.1”),6003));
    Console.ReadKey(true);

    #2964
    Anonymous
    Inactive

    Please see our tutorial on sending custom object type https://networkcomms.net/custom-objects/, if the client and server are both NetworkComms.Net you should not be doing this:

    IFormatter formatter = new BinaryFormatter();
    MemoryStream stream = new MemoryStream(message);
    User t = formatter.Deserialize(stream) as User;
    stream.Close();

    Marc

    #2968
    Anonymous
    Inactive

    Thank you for your reply,The key is the receiving end can not know the sending end to send what type of data to come over, I’ve seen your help document, your type is known, and I received end is not fixed

    #2969
    Anonymous
    Inactive

    My aim is to become a universal receiver

    #2971
    Anonymous
    Inactive

    An object is sent over a network as an array of bytes, e.g:

    115
    100
    103
    100
    52

    It is impossible to know what these bytes mean unless the correct type is known. For example:

    int integer = BitConverter.ToInt32(result, 0);
    //integer = 1684497523
    
    string str = Encoding.ASCII.GetString(result);
    //str = sdgd4
    
    float dbl = BitConverter.ToSingle(result, 0);
    //dbl = 1.7073744E+22

    Which is the correct answer?

    Marc

Viewing 7 posts - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.