By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,187 Members | 1,629 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,187 IT Pros & Developers. It's quick & easy.

Serialize/Deserialize object[] over an async socket

P: n/a
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);
}
}
Nov 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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" wrote:
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);
}
}

Nov 17 '05 #2

P: n/a
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 [C# MVP]" wrote:
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" wrote:
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);
}
}

Nov 17 '05 #3

P: n/a
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" wrote:
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 [C# MVP]" wrote:
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" wrote:
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);
}
}

Nov 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.