client/server socket problem

P

Paul Fi

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
 
G

Guest

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top