Where can I find UdpState? UDP programming problem

  • Thread starter Thread starter quakertistar
  • Start date Start date
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

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;​

Byte[] receiveBytes = u.EndReceive(ar, ref e);
string receiveString = Encoding.ASCII.GetString(receiveBytes);​

Console.WriteLine("Received: {0}", receiveString);
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);​

UdpState s = new UdpState(); ///////////////////////////////////////////////////Problem goes here
s.e = e;
s.u = u;​

Console.WriteLine("listening for messages");
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);
}
}​








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...
 

Similar threads

J
Replies
0
Views
63
Jessi_ca
J
P
Replies
0
Views
176
Policy standard local admin account with Active Di
P
Back
Top