- This topic has 6 replies, 2 voices, and was last updated 11 years, 3 months ago by
Anonymous.
-
AuthorPosts
-
August 20, 2014 at 14:36 #2960
Anonymous
InactiveHello,Object type cannot send and receive?
August 20, 2014 at 22:44 #2961Anonymous
InactiveHeya 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
August 21, 2014 at 03:11 #2962Anonymous
InactiveHello, 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);August 21, 2014 at 08:22 #2964Anonymous
InactivePlease 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
August 21, 2014 at 08:48 #2968Anonymous
InactiveThank 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
August 21, 2014 at 08:59 #2969Anonymous
InactiveMy aim is to become a universal receiver
August 21, 2014 at 21:40 #2971Anonymous
InactiveAn object is sent over a network as an array of bytes, e.g:
115
100
103
100
52It 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+22Which is the correct answer?
Marc
-
AuthorPosts
- You must be logged in to reply to this topic.
