kered pople

Hi,

I transfer an object over a network say (AddressesInfo), which i serialize using the binaryformmater. In this sample code below i am using a loopback ip for testing. I can get the data from the client by using the following code, but it is serialized.

private static TcpListener server = new TcpListener(IPAddress.Loopback, 13000);

private static TcpClient client;

private static bool connected = false;

static void Main(string[] args)

{

try

{

//start the server

StartServer();

//check for connection

IsConnecttion();

//if we have a connection to a client read data

if (connected)

ReadData();

}

catch (SocketException e)

{

Console.WriteLine("SocketException: {0}", e);

}

}

public static void StartServer()

{

//Start listening for client requests

server.Start();

}

public static void IsConnecttion()

{

//waiting for connection

Console.Write("Waiting for a connection... ");

// Perform a blocking call to accept requests.

client = server.AcceptTcpClient();

//check for connection

if (client.Connected)

{

Console.WriteLine("Connected");

connected = true;

}

else

{

Console.WriteLine("No Connection");

connected = false;

}

}

public static void ReadData()

{

//string builder

StringBuilder sb = new StringBuilder();

//Buffer for reading data

byte[] bytes = new byte[1000];

// Get a stream object for reading and writing

NetworkStream netStream = client.GetStream();

//get the data sent by the client.

int i = netStream.Read(bytes, 0, bytes.Length);

// Translate data bytes to a ASCII string

sb.Append(Encoding.ASCII.GetString(bytes, 0, i));

//write data to console

Console.WriteLine("Received: {0}", sb.ToString());

How do i get the object and deserialize it from the network stream .

Thanks in advance

kered



Re: .NET Framework Networking and Communication Deserialize A Networkstream

mwalts

There is an example of this right one MSDN in the BinaryFormatter class

http://msdn2.microsoft.com/en-us/library/system.runtime.serialization.formatters.binary.binaryformatter.aspx

The C# example is about half way down the page. Basically you create a BinaryFormatter object, pass the stream to the Deserialize method, and then caste it's return value to the object you want

Have fun,

-mwalts




Re: .NET Framework Networking and Communication Deserialize A Networkstream

kered pople

Hi, Mwalts

Thanks for your reply. I can get the object to seralize and deserialize when not involveing the networkstream, which your link example shows. The problem i have is when using the networkstream, because i send the object as bytes from the client. When i get the stream from the client (Networkstream) i cannot deseralize it with the binaryformatter. As it was sent as bytes, that is what i need to get from the stream as my sample code shows, which is serialized. My problem i have is where does the binaryformatter come into action as i have seralized data .

Many Thanks

kered





Re: .NET Framework Networking and Communication Deserialize A Networkstream

kered pople

Hi,

Thanks for your help i have solved it now. Below is the revised code hope it helps others.

//client code

string serverIp = "127.0.0.1";

// have my connection established with a Tcp Server

TcpClient client = new TcpClient(serverIp, 9050);

// the formatter that will serialize my object on my stream

IFormatter formatter = new BinaryFormatter();

//get the network stream

NetworkStream strm = client.GetStream();

// the serialization process (addressinfo is my object could be any object)

formatter.Serialize(strm, addressInfo);

//close the client and stream

strm.Close();

client.Close();

//Server Code

//listen for connections from tcp network clients

TcpListener server1 = new TcpListener(IPAddress.Loopback, 9050);

//start listening

server1.Start();

//accepts a pending conection request

TcpClient client = server1.AcceptTcpClient();

//get the network stream

NetworkStream strm = client.GetStream();

//create the formatter

IFormatter formatter = new BinaryFormatter();

// you have to cast the deserialized object

AddressesInfo addressInfo = (AddressesInfo)formatter.Deserialize(strm);

strm.Close();

client.Close();

kered





Re: .NET Framework Networking and Communication Deserialize A Networkstream

Figo Fei - MSFT

Hi kered

Would you please share your solution of the problem to benifit others in the forums

Thanks






Re: .NET Framework Networking and Communication Deserialize A Networkstream

kered pople

Hi,Figo

I have now shown my solution to my problem.

Many thanks

kered





Re: .NET Framework Networking and Communication Deserialize A Networkstream

Figo Fei - MSFT

Thank you for your sharing, Kered




Re: .NET Framework Networking and Communication Deserialize A Networkstream

sendi

Thanks for your try , but if you put this in the loop (the server code) it will not work !!