473,800 Members | 2,413 Online
Bytes | Software Development & Data Engineering Community
+ 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(ser ver);
IPAddress hostadd = hostent.Address List[0];
IPEndPoint EPhost = new IPEndPoint(host add, port);

Socket soc = new Socke(AddressFa mily.InterNetwo rk,
SocketType.Stre am,ProtocolType .Tcp);

soc.Connect(EPh ost);

ArrayList al = new ArrayList();

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

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

bf.Serialize(ms ,al);

Console.WriteLi ne("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,So cketFlags.None) ;

Console.WriteLi ne("data is sent");
Console.ReadLin e();

and my server code :

IPAddress ipAddress = Dns.Resolve("lo calhost").Addre ssList[0];
TcpListener server = new TcpListene(ipAd dress,8085);
server.Start();

Console.WriteLi ne("Server started listening");
while (true)
{
Socket s = server.AcceptSo cket();
byte[] bytes = new Byte[256];
s.Receive(bytes , 0, s.Available,
SocketFlags.Non e);

MemoryStream ms = new MemoryStream(by tes);
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 3006
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(ser ver)
IPAddress hostadd = hostent.Address List[0]
IPEndPoint EPhost = new IPEndPoint(host add, port)

Socket soc = new Socke(AddressFa mily.InterNetwo rk
SocketType.Stre am,ProtocolType .Tcp)

soc.Connect(EPh ost)

ArrayList al = new ArrayList()

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

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

bf.Serialize(ms ,al)

Console.WriteLi ne("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,So cketFlags.None)

Console.WriteLi ne("data is sent")
Console.ReadLin e()

and my server code

IPAddress ipAddress = Dns.Resolve("lo calhost").Addre ssList[0]
TcpListener server = new TcpListene(ipAd dress,8085)
server.Start()

Console.WriteLi ne("Server started listening")
while (true)

Socket s = server.AcceptSo cket()
byte[] bytes = new Byte[256]
s.Receive(bytes , 0, s.Available
SocketFlags.Non e)

MemoryStream ms = new MemoryStream(by tes)
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
4498
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 computations, the only data transfered is user mouse/kbd input. It works synchronously, but somehow, when I play in client window, both client and server have 17 fps, while when playing in server window, server has 44 fps while client ...
4
8202
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 TcpClient, NetworkStream and StreamWriter classes. but with low level socket it doesn't work (When using the Socket class Send method).
0
1465
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 clients. Also there's a commented code where i'm using TCPListener instead of simple Socket Class. The problem i'm having is that when I run this code using Socket class, the server starts well but "OnClientConnect" method never gets called. On...
0
1691
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 by line).. the code for server is given as /* A simple server in the internet domain using TCP The port number is passed as an argument */ #include <iostream.h> #include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include...
0
9551
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10275
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10033
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7576
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6811
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5471
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5606
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4149
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2945
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.