System.Net/Sockets, How do I modify this?

Hi. I am using the code guru article.

I think this code I have sends a string over the port. I don't understand it completely, but I need to be able to send an object, or other data besides a string.

I've modified it a little but I can't figure out how to send an object. I want the client to send an object to the server, instead of a string.


public void OnDataReceived(IAsyncResult asyn)

{

SocketPacket socketData = (SocketPacket)asyn.AsyncState ;

try

{

// Complete the BeginReceive() asynchronous call by EndReceive() method

// which will return the number of characters written to the stream

// by the client

int iRx = socketData.m_currentSocket.EndReceive (asyn);

char[] chars = new char[iRx + 1];

// Extract the characters as a buffer

System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder();

int charLen = d.GetChars(socketData.dataBuffer,

0, iRx, chars, 0);

System.String szData = new System.String(chars);

string msg = "" + socketData.m_clientNumber + ":";

AppendToRichEditControl(msg + szData);

// Send back the reply to the client

string replyMsg = "Server Reply:" + szData.ToUpper();

// Convert the reply to byte array

byte[] byData = System.Text.Encoding.ASCII.GetBytes(replyMsg);

Socket workerSocket = (Socket)socketData.m_currentSocket;

workerSocket.Send(byData);

// Continue the waiting for data on the Socket

WaitForData(socketData.m_currentSocket, socketData.m_clientNumber );

}

catch (ObjectDisposedException )

{

System.Diagnostics.Debugger.Log(0,"1","\nOnDataReceived: Socket has been closed\n");

}

catch(SocketException se)

{

if(se.ErrorCode == 10054) // Error code for Connection reset by peer

{

string msg = "Client " + socketData.m_clientNumber + " Disconnected" + "\n";

AppendToRichEditControl(msg);

// Remove the reference to the worker socket of the closed client

// so that this object will get garbage collected

m_workerSocketList[socketData.m_clientNumber - 1] = null;

UpdateClientListControl();

}

else

{

MessageBox.Show (se.Message );

}

}

}


i'm trying to understand how to get an object from the client, and I don't know what of this code I need or how to modify it.

http://www.dev-archive.com/csharp/csharp/cs_network/sockets/article.php/c7695
http://www.dev-archive.com/Csharp/Csharp/cs_network/sockets/article.php/c8781

Can someone please show me what I need to do? So I can understand it?

Thanks.
[2864 byte] By [DanDandy] at [2007-11-20 10:40:36]
# 1 Re: System.Net/Sockets, How do I modify this?
i found this on another forum. it looks like i cant send an object, but i can use a memorystream to send a copy of the memory to another computer.

i will try this method but i've never done it before so in the mean time i will check back here to see if someone can help


rawCoder,

Basically, you have to use serialization. You will want to serialize
your object to a MemoryStream (or some other stream, but MemoryStream will
return a byte array to you, which will help you later). Once you have the
stream, get the bytes from it (with a MemoryStream you would call the
ToArray method). From there, just pass the bytes to the Send method on the
socket, and you should be fine.

You just have to read the bytes into a MemoryStream (or some other
stream on the other end) and then pass that stream to a formatter for
deserialization.

The things you have to look out for are the length of the message.
Since not all objects serialized into a fixed-length format, you need to
send a prefix over the socket indicating how long the byte stream for the
object will be. Then, on the other side, you will know how many bytes you
need to read to get the contents of the object.

Also, both sides have to have a reference to the assembly containing the
type.

Hope this helps.


i will try to fix it myself too i'll post back if i get it working

thanks

EDIT
I tried the edit the code it compiles but I know its not right. I dont understand what I'm supposed to do.


public void OnDataReceived(IAsyncResult asyn) {

SocketPacket socketData = (SocketPacket)asyn.AsyncState;

try {

// COMPLETE THE SOCKET BeginReceive AND RETURN THE NUMBER OF BYTES WRITTEN ONTO THE STREAM

int length = socketData.m_currentSocket.EndReceive(asyn);

char[] chars = new char[length + 1];

// EXTRACT THE CHARACTERS AS A BUFFER

System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder();

int charLen = d.GetChars(socketData.dataBuffer, 0, length, chars, 0);

// TODO: PROCESS RECEIVED DATA

using(MemoryStream mstream = new MemoryStream(length)) {

IFormatter formatter = new BinaryFormatter();

object transmittedObject = formatter.Deserialize(mstream);

}



// Continue the waiting for data on the Socket

WaitForData(socketData.m_currentSocket, socketData.m_clientNumber);


I am using the socket packet class from that article i think i need to get the buffer data from that class. i think i will contain the object i am sending, but i am not sure.
DanDandy at 2007-11-9 11:35:39 >