Q
quakertistar
Guest
Hi!
Recently, I'm working on a project using UDP protocol. When it comes to receiving data from another terminal, the system gives no response because the infinite listen using this :
Code Block
So, I've tried to use asynchronous way. I looked up the MSDN, and I found an example:
Code Block
According to the comment I made, the UdpState class cannot be found in System.Net or System.Net.Sockets namespace. So, where can I find it or how can I realize asynchronous sending and receiving?
Continue reading...
Recently, I'm working on a project using UDP protocol. When it comes to receiving data from another terminal, the system gives no response because the infinite listen using this :
Code Block
UdpClient listener = new UdpListener();
listener.Receive(buffer);
So, I've tried to use asynchronous way. I looked up the MSDN, and I found an example:
Code Block
public static bool messageReceived = false;
public static void ReceiveCallback(IAsyncResult ar)
{
UdpClient u = (UdpClient)((UdpState)(ar.AsyncState)).u;
IPEndPoint e = (IPEndPoint)((UdpState)(ar.AsyncState)).e;
{
UdpClient u = (UdpClient)((UdpState)(ar.AsyncState)).u;
IPEndPoint e = (IPEndPoint)((UdpState)(ar.AsyncState)).e;
Byte[] receiveBytes = u.EndReceive(ar, ref e);
string receiveString = Encoding.ASCII.GetString(receiveBytes);
string receiveString = Encoding.ASCII.GetString(receiveBytes);
Console.WriteLine("Received: {0}", receiveString);
messageReceived = true;
}
messageReceived = true;
}
public static void ReceiveMessages()
{
// Receive a message and write it to the console.
IPEndPoint e = new IPEndPoint(IPAddress.Any, listenPort);
UdpClient u = new UdpClient(e);
{
// Receive a message and write it to the console.
IPEndPoint e = new IPEndPoint(IPAddress.Any, listenPort);
UdpClient u = new UdpClient(e);
UdpState s = new UdpState(); ///////////////////////////////////////////////////Problem goes here
s.e = e;
s.u = u;
s.e = e;
s.u = u;
Console.WriteLine("listening for messages");
u.BeginReceive(new AsyncCallback(ReceiveCallback), s);
u.BeginReceive(new AsyncCallback(ReceiveCallback), s);
// Do some work while we wait for a message. For this example,
// we'll just sleep
while (!messageReceived)
{
Thread.Sleep(100);
}
}
// we'll just sleep
while (!messageReceived)
{
Thread.Sleep(100);
}
}
According to the comment I made, the UdpState class cannot be found in System.Net or System.Net.Sockets namespace. So, where can I find it or how can I realize asynchronous sending and receiving?
Continue reading...