Serialize/Deserialize object[] over an async socket

G

Guest

I need to send an object[] between nodes on a network. Each node currently
communicates fine with sending strings around but I can't figure out how to
deserialize objects using the same basic infrastructure. I'm currently using
MS's basic server example for my code:

public static void AcceptCallback(IAsyncResult ar)
{
// Signal the main thread to continue.
allDone.Set();

// Get the socket that handles the client request.
Socket listener = (Socket)ar.AsyncState;
Socket handler = listener.EndAccept(ar);

// Create the state object.
StateObject state = new StateObject();
state.workSocket = handler;
handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(ReadCallback), state);
}

public static void ReadCallback(IAsyncResult ar)
{
try
{
String content = String.Empty;

StateObject state = (StateObject)ar.AsyncState;
Socket handler = state.workSocket;

int bytesRead = handler.EndReceive(ar);

if (bytesRead > 0)
{
// There might be more data, so store the data received
so far.
state.sb.Append(Encoding.ASCII.GetString(
state.buffer, 0, bytesRead));

// Check for end-of-file tag. If it is not there, read
// more data.
content = state.sb.ToString();
if (content.IndexOf("<EOF>") > -1)
{
//Do Work with content

state.sb = new StringBuilder();
handler.BeginReceive(state.buffer, 0,
StateObject.BufferSize, 0,
new AsyncCallback(ReadCallback), state);
}
else
{
handler.BeginReceive(state.buffer, 0,
StateObject.BufferSize, 0,
new AsyncCallback(ReadCallback), state);
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
 
G

Guest

Check the documentation on the BinaryFormatter class. This enables you to
provide a MemoryStream and an object, and get back a byte array that can
easily be sent over sockets. At the other end, in reverse, you provide a type
and a byte array and can cast it back to your object.

Peter
 
G

Guest

Since my first post I've figured that out. My problem now is that it isn't
totally asynchronous. If I send a large object I have to wait for that to
finish before I can send the next one if it is comming from the same
connection. Is there a way to make it overlap?

Peter Bromberg said:
Check the documentation on the BinaryFormatter class. This enables you to
provide a MemoryStream and an object, and get back a byte array that can
easily be sent over sockets. At the other end, in reverse, you provide a type
and a byte array and can cast it back to your object.

Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com




Eric said:
I need to send an object[] between nodes on a network. Each node currently
communicates fine with sending strings around but I can't figure out how to
deserialize objects using the same basic infrastructure. I'm currently using
MS's basic server example for my code:

public static void AcceptCallback(IAsyncResult ar)
{
// Signal the main thread to continue.
allDone.Set();

// Get the socket that handles the client request.
Socket listener = (Socket)ar.AsyncState;
Socket handler = listener.EndAccept(ar);

// Create the state object.
StateObject state = new StateObject();
state.workSocket = handler;
handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(ReadCallback), state);
}

public static void ReadCallback(IAsyncResult ar)
{
try
{
String content = String.Empty;

StateObject state = (StateObject)ar.AsyncState;
Socket handler = state.workSocket;

int bytesRead = handler.EndReceive(ar);

if (bytesRead > 0)
{
// There might be more data, so store the data received
so far.
state.sb.Append(Encoding.ASCII.GetString(
state.buffer, 0, bytesRead));

// Check for end-of-file tag. If it is not there, read
// more data.
content = state.sb.ToString();
if (content.IndexOf("<EOF>") > -1)
{
//Do Work with content

state.sb = new StringBuilder();
handler.BeginReceive(state.buffer, 0,
StateObject.BufferSize, 0,
new AsyncCallback(ReadCallback), state);
}
else
{
handler.BeginReceive(state.buffer, 0,
StateObject.BufferSize, 0,
new AsyncCallback(ReadCallback), state);
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
 
G

Guest

We are really talking about 2 different things. If you want to use
BinaryFormatter (or any other Serializer) to serialize / deserialize an
object, you have no choice but to wait until it's done with that particular
object. However, if each operation is done on a ThreadPool thread, you have
achieved asynchronous operations.
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com




Eric said:
Since my first post I've figured that out. My problem now is that it isn't
totally asynchronous. If I send a large object I have to wait for that to
finish before I can send the next one if it is comming from the same
connection. Is there a way to make it overlap?

Peter Bromberg said:
Check the documentation on the BinaryFormatter class. This enables you to
provide a MemoryStream and an object, and get back a byte array that can
easily be sent over sockets. At the other end, in reverse, you provide a type
and a byte array and can cast it back to your object.

Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com




Eric said:
I need to send an object[] between nodes on a network. Each node currently
communicates fine with sending strings around but I can't figure out how to
deserialize objects using the same basic infrastructure. I'm currently using
MS's basic server example for my code:

public static void AcceptCallback(IAsyncResult ar)
{
// Signal the main thread to continue.
allDone.Set();

// Get the socket that handles the client request.
Socket listener = (Socket)ar.AsyncState;
Socket handler = listener.EndAccept(ar);

// Create the state object.
StateObject state = new StateObject();
state.workSocket = handler;
handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(ReadCallback), state);
}

public static void ReadCallback(IAsyncResult ar)
{
try
{
String content = String.Empty;

StateObject state = (StateObject)ar.AsyncState;
Socket handler = state.workSocket;

int bytesRead = handler.EndReceive(ar);

if (bytesRead > 0)
{
// There might be more data, so store the data received
so far.
state.sb.Append(Encoding.ASCII.GetString(
state.buffer, 0, bytesRead));

// Check for end-of-file tag. If it is not there, read
// more data.
content = state.sb.ToString();
if (content.IndexOf("<EOF>") > -1)
{
//Do Work with content

state.sb = new StringBuilder();
handler.BeginReceive(state.buffer, 0,
StateObject.BufferSize, 0,
new AsyncCallback(ReadCallback), state);
}
else
{
handler.BeginReceive(state.buffer, 0,
StateObject.BufferSize, 0,
new AsyncCallback(ReadCallback), state);
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top