473,471 Members | 1,696 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

client/server socket problem


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
1 2989
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Michael Rybak | last post by:
hi, everyone. I'm writing a 2-players game that should support network mode. I'm now testing it on 1 PC since I don't have 2. I directly use sockets, and both client and server do...
4
by: yaron | last post by:
Hi, I have a problem when sending data over TCP socket from c# client to java server. the connection established ok, but i can't send data from c# client to java server. it's work ok with...
0
by: Usman | last post by:
Hi I'm having problem with a scenarion where I have a server written in C# and client written in VC6++. Here is the server code that i'm using including the Callback function for handling...
0
by: ankuragt | last post by:
(problem in writing a string into a file on client side.) hey i have written a code of server and client.what i want to do is to transfer the contents of file on server side to the client side (line...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.