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
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