M
Moiv
Hi, I am building an Async socket server class in Visual C# express 2008.
I am using the example here
http://www.codeguru.com/csharp/csharp/cs_network/sockets/article.php/c7695
What I'm trying to do is build a string from received data and upon receipt
of an end string identifier (in this case "#*") raise an event which my main
program will have an event handler to do something with the string. The
problem is ReceivedString only ever gets the first characer received added
to it and the following chars are not appended. I'm assuming this has
something to do with threading so I've locked the code that appends to the
ReceivedData string but this doesnt help.
What am I doing wrong?
Callback function is listed below and I've attached the full source in case
it's needed.
// This the call back function which will be invoked when the socket
// detects any client writing of data on the stream
void OnDataReceived(IAsyncResult asyn)
{
try
{
SocketPacket socketData = (SocketPacket)asyn.AsyncState;
int iRx = 0 ;
// Complete the BeginReceive() asynchronous call by
EndReceive() method
// which will return the number of characters written to the
stream
// by the client
iRx = socketData.m_currentSocket.EndReceive (asyn);
char[] chars = new char[iRx + 1];
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);
Object objData = szData.ToString();
SendString(objData);
lock (ReceivedString)
{
ReceivedString = ReceivedString + szData;
//MessageBox.Show(ReceivedString);
}
// Continue the waiting for data on the Socket
WaitForData( socketData.m_currentSocket );
MyEventArgs e = new MyEventArgs(szData);
EventHandler<MyEventArgs> handler = DataReceived;
if (handler != null) handler(this, e);
/*
if (ReceivedString.Length > 2)
{
int stringstart = ReceivedString.Length - 2;
if (ReceivedString.Substring(stringstart) == "#*")
{
MyEventArgs e = new MyEventArgs(ReceivedString);
EventHandler<MyEventArgs> handler = DataReceived;
if (handler != null) handler(this, e);
}
}*/
}
catch (ObjectDisposedException )
{
System.Diagnostics.Debugger.Log(0,"1","\nOnDataReceived:
Socket has been closed\n");
}
catch(SocketException se)
{
MessageBox.Show (se.Message );
}
}
I am using the example here
http://www.codeguru.com/csharp/csharp/cs_network/sockets/article.php/c7695
What I'm trying to do is build a string from received data and upon receipt
of an end string identifier (in this case "#*") raise an event which my main
program will have an event handler to do something with the string. The
problem is ReceivedString only ever gets the first characer received added
to it and the following chars are not appended. I'm assuming this has
something to do with threading so I've locked the code that appends to the
ReceivedData string but this doesnt help.
What am I doing wrong?
Callback function is listed below and I've attached the full source in case
it's needed.
// This the call back function which will be invoked when the socket
// detects any client writing of data on the stream
void OnDataReceived(IAsyncResult asyn)
{
try
{
SocketPacket socketData = (SocketPacket)asyn.AsyncState;
int iRx = 0 ;
// Complete the BeginReceive() asynchronous call by
EndReceive() method
// which will return the number of characters written to the
stream
// by the client
iRx = socketData.m_currentSocket.EndReceive (asyn);
char[] chars = new char[iRx + 1];
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);
Object objData = szData.ToString();
SendString(objData);
lock (ReceivedString)
{
ReceivedString = ReceivedString + szData;
//MessageBox.Show(ReceivedString);
}
// Continue the waiting for data on the Socket
WaitForData( socketData.m_currentSocket );
MyEventArgs e = new MyEventArgs(szData);
EventHandler<MyEventArgs> handler = DataReceived;
if (handler != null) handler(this, e);
/*
if (ReceivedString.Length > 2)
{
int stringstart = ReceivedString.Length - 2;
if (ReceivedString.Substring(stringstart) == "#*")
{
MyEventArgs e = new MyEventArgs(ReceivedString);
EventHandler<MyEventArgs> handler = DataReceived;
if (handler != null) handler(this, e);
}
}*/
}
catch (ObjectDisposedException )
{
System.Diagnostics.Debugger.Log(0,"1","\nOnDataReceived:
Socket has been closed\n");
}
catch(SocketException se)
{
MessageBox.Show (se.Message );
}
}