PC Review


Reply
Thread Tools Rate Thread

client/server socket problem

 
 
Paul Fi
Guest
Posts: n/a
 
      17th Feb 2004

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!
 
Reply With Quote
 
 
 
 
=?Utf-8?B?VHUtVGhhY2g=?=
Guest
Posts: n/a
 
      17th Feb 2004
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

 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with socket communication for client (On handheld) / server (on PC) application jd_ie Microsoft Dot NET Compact Framework 3 29th Jun 2006 04:18 PM
acynchronous client and server problem using socket =?Utf-8?B?d3VrdW4=?= Microsoft Dot NET Framework 0 22nd Feb 2006 07:20 AM
Socket Server to Socket Client..Client function not working when called from Server... trint Microsoft C# .NET 2 13th Apr 2005 09:43 PM
Server socket problem.. client sees connection close before all data received CHRISM Microsoft VB .NET 1 12th Nov 2004 02:42 PM
C# .NET server Socket class And VB Client Winsock API(Problem Recognising disconnection) Abhishek Microsoft Dot NET Framework 0 26th Jun 2004 08:13 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:26 PM.