#### Archived

This topic is now archived and is closed to further replies.

# C# serialization across a network

This topic is 5012 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am attempting to send a class from a client to a server, and I seem to be doing something incorrectly. The class name is Points (and it, of course, is serializable), and I declare it in both the server and client. Points is the exact same in the server and client, but it is in each executable, not a separate dll used by each exe. Here is the code in the client and server for the serializing part. Client: // clientSockStream is a NetworkStream Points send_class = new Points(); Stream stream = (Stream) clientSockStream; BinaryFormatter bformatter = new BinaryFormatter(); bformatter.Serialize(stream, send_class); clientSockStream.Flush(); Server: // serverSockStream is a NetworkStream Points recd = null; BinaryFormatter bformatter = new BinaryFormatter(); Stream stream = (Stream) serverSockStream; recd = (Points)bformatter.Deserialize(stream); serverSockStream.Flush(); When I compile my code, it serializes without and exception and sends apparently with no problem, but when I try to deserialize on the server end, it doesn''t work. Anyone have an idea why this doesn''t work. And as a side note, I can serialize to a file and deserialize back out of it from the client, so I''m pretty sure that end is working. Jason

##### Share on other sites
define "doesn''t work". does it crash? does it load but with incorrect data?

please post you Points class implementation. and use [ source ] [ /source ] tags (without the spaces) around the code so we don''t go insane.

does you Points class by any chance have pointers as some of it''s member variables. if so, you have to remember to override the serealize method to serialize the data that the pointers point to rather than just sending the pointers. does C# even have pointers? if not, ignore this paragraph.

-me

##### Share on other sites
Sorry about the bad format of my last post, that was my first post here. What I mean by not working is that the data will not serialize. An exception is thrown on:

recd = (Points)bformatter.Deserialize(stream);

Here is my Points class:

   [Serializable()]public class Points:ISerializable{   public int [] x;   public int [] y;   public Points()   {      x = new int[20];      y = new int[20];      int a;      for (a=0; a<20; a++)      {         x[a] = 0;	 y[a] = 0;      }   }   //Deserialization constructor.   public Points(SerializationInfo info, StreamingContext ctxt)   {      x = new int[20];      y = new int[20];      int a;      for (a=0; a<20; a++)      {	 this.x[a] = (int)info.GetValue("x" + a.ToString(),                                         typeof(int)); 	 this.y[a] = (int)info.GetValue("y" + a.ToString(),                                        typeof(int));      }   }           //Serialization function.   public void GetObjectData(SerializationInfo info,                              StreamingContext ctxt)   {      int a;      for (a=0; a<20; a++)      {	 info.AddValue("x"+a.ToString(), x[a]);	 info.AddValue("y"+a.ToString(), y[a]);      }   }}

Hopefully that'll help.

Jason

[edited by - jlutz777 on March 24, 2004 2:53:50 PM]

##### Share on other sites
ah, so i think your problem is the pointer issue i talked about. again i don't really know C#, i'm just assuming it's similar enough to c/c++ that i'm right

the problem is that when you serialize your class you're actually sending only 4 bytes for your x and y arrays because you are just sending the pointer to the array. you have to override your serialize method so that when you serialize the class you actually send all 20 members of each array. i believe that you're crashing on deserialize because you are trying to read in 20*sizeof(int) for both x and y when you're only actually sending 4 bytes for each.

i don't know how to specify that for your program.

incedentally we can prove me wrong pretty easily. when you serialize to a file and deserialize from a file are you closing your program between those events? i bet you're not. so when you read back in the pointers they are pointing to valid memory spaces that still contain your data. as an experiment try serializing to a file, exiting your program, then restarting your program and just reading in from the file. you'll need to probably just do a simple rewrite / recompile of your app between those program runs. should only take about 5 minutes. my bet is that your app will crash. this will also make it easier to debug since you won't need to have a server running to get your stuff working.

-me

[edited by - Palidine on March 24, 2004 3:03:34 PM]

##### Share on other sites
I don''t think this is the problem, but I could be wrong. Does anyone else have any ideas? This might be a C#-specific problem . . .

Jason

##### Share on other sites
Update:

I got it to work when I put the class Points in a dll. I''m not sure how to do it if the class is declared in the exes, but I got it work this way, so I guess I''ll just stick with it.

##### Share on other sites
Well, if it''s in .DLL, you use the same class in both the client and the server. Otherwise, you just have two different classes who happen to look exactly the same.

At least that''s the way I figure it.

• ### Forum Statistics

• Total Topics
628686
• Total Posts
2984237

• 16
• 13
• 13
• 10
• 10