Hey.
I am new with the Asynchronous sockets and I am trying to make a little program.
I want when the client sends the string "doitall", the server does something, I succeeded to do that with the client but I faild with the server. This is the code:
public void OnDataReceived(IAsyncResult asyn)
{
try
{
CSocketPacket theSockId = (CSocketPacket)asyn.AsyncState;
int byteCount = 0;
byteCount = theSockId.thisSocket.EndReceive(asyn);
char[] chars = new char[byteCount + 1];
Encoding.UTF8.GetDecoder().GetChars(theSockId.dataBuffer, 0, byteCount, chars, 0);
string str = new string(chars);
AppendText(str);
if (str == "doitall")
{
MessageBox.Show("works.");
}
WaitForData(m_socWorker);
}
catch (SocketException se)
{
MessageBox.Show(se.Message);
}
}
Whats the problem?
And how to send data to a specific IP address?
Loading
Sam HobbsPosted Feb 20, 2010, 7:17 PM
The only reason I know how to do asynchronous programmeing is because I have read the Microsoft documentation and articles. If you have a more specific question then I will try to help. I really think however that you need to spend some time learning instead of attempting to peice together sample code without understanding how it works.
or nakashPosted Feb 20, 2010, 4:22 AM
public void WaitForData(System.Net.Sockets.Socket soc)
{
try
{
if (pfnWorkerCallBack == null)
{
pfnWorkerCallBack = new AsyncCallback(OnDataReceived);
}
CSocketPacket theSocPkt = new CSocketPacket();
theSocPkt.thisSocket = soc;
soc.BeginReceive(theSocPkt.dataBuffer, 0, theSocPkt.dataBuffer.Length, SocketFlags.None, pfnWorkerCallBack, theSocPkt);
}
catch (SocketException se)
{
MessageBox.Show(se.Message);
}
}
That is the WaitForData code.
Sam HobbsPosted Feb 19, 2010, 7:05 PM