By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,302 Members | 1,788 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,302 IT Pros & Developers. It's quick & easy.

client/server socket problem

P: n/a

I have this client code:

string server = "localhost";
int port = 8085;
IPHostEntry hostent = Dns.Resolve(server);
IPAddress hostadd = hostent.AddressList[0];
IPEndPoint EPhost = new IPEndPoint(hostadd, port);

Socket soc = new Socke(AddressFamily.InterNetwork,
SocketType.Stream,ProtocolType.Tcp);

soc.Connect(EPhost);

ArrayList al = new ArrayList();

al.Add("hello");
al.Add("world!");

MemoryStream ms = new MemoryStream();
BinaryFormatter bf = new BinaryFormatter();

bf.Serialize(ms,al);

Console.WriteLine("Length : " + ms.Length + "Pos : " + ms.Position);

ms.Position = 0 ;

byte[] data = new byte[ms.Length];

int i = ms.Read(data,0,data.Length);

soc.Send(data,0,data.Length,SocketFlags.None);

Console.WriteLine("data is sent");
Console.ReadLine();

and my server code :

IPAddress ipAddress = Dns.Resolve("localhost").AddressList[0];
TcpListener server = new TcpListene(ipAddress,8085);
server.Start();

Console.WriteLine("Server started listening");
while (true)
{
Socket s = server.AcceptSocket();
byte[] bytes = new Byte[256];
s.Receive(bytes, 0, s.Available,
SocketFlags.None);

MemoryStream ms = new MemoryStream(bytes);
BinaryFormatter bf = new BinaryFormatter();
ms.Position = 0;
object al = bf.Deserialize(ms);
s.Close();
}
so what my client is doing is serializing my arraylist object into a
byte stream and sending the array of bytes over to the server process
where the server will wrap the byte array with the same type of stream
used at the client side and then deserialzed into its original state
which is arraylist type.

But when deserializing at the server side im getting this error which i
cant figure out:

Exception : Binary stream does not contain a valid BinaryHeader, 0
possible causes, invalid stream or object version change between
serialization and deserialization.

can any one help me here pls
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Paul
You are getting the exception because the bytes as an array of 256 bytes. The data sent is only 130 bytes. So, when deserialize, you have junk bytes at the end causing the exception

Tu-Thac

----- Paul Fi wrote: ----
I have this client code

string server = "localhost"
int port = 8085
IPHostEntry hostent = Dns.Resolve(server)
IPAddress hostadd = hostent.AddressList[0]
IPEndPoint EPhost = new IPEndPoint(hostadd, port)

Socket soc = new Socke(AddressFamily.InterNetwork
SocketType.Stream,ProtocolType.Tcp)

soc.Connect(EPhost)

ArrayList al = new ArrayList()

al.Add("hello")
al.Add("world!")

MemoryStream ms = new MemoryStream()
BinaryFormatter bf = new BinaryFormatter()

bf.Serialize(ms,al)

Console.WriteLine("Length : " + ms.Length + "Pos : " + ms.Position)

ms.Position = 0

byte[] data = new byte[ms.Length]

int i = ms.Read(data,0,data.Length)

soc.Send(data,0,data.Length,SocketFlags.None)

Console.WriteLine("data is sent")
Console.ReadLine()

and my server code

IPAddress ipAddress = Dns.Resolve("localhost").AddressList[0]
TcpListener server = new TcpListene(ipAddress,8085)
server.Start()

Console.WriteLine("Server started listening")
while (true)

Socket s = server.AcceptSocket()
byte[] bytes = new Byte[256]
s.Receive(bytes, 0, s.Available
SocketFlags.None)

MemoryStream ms = new MemoryStream(bytes)
BinaryFormatter bf = new BinaryFormatter()
ms.Position = 0
object al = bf.Deserialize(ms)
s.Close()

so what my client is doing is serializing my arraylist object into
byte stream and sending the array of bytes over to the server proces
where the server will wrap the byte array with the same type of strea
used at the client side and then deserialzed into its original stat
which is arraylist type

But when deserializing at the server side im getting this error which
cant figure out

Exception : Binary stream does not contain a valid BinaryHeader,
possible causes, invalid stream or object version change betwee
serialization and deserialization

can any one help me here pl
*** Sent via Developersdex http://www.developersdex.com **
Don't just participate in USENET...get rewarded for it

Nov 15 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.