Advertisement
akass

Why Receive() is not being called

Mar 19th, 2015
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.87 KB | None | 0 0
  1. public class MainActivity : Activity
  2. {
  3.     private readonly UdpClient udp = new UdpClient(45000);
  4.  
  5.     protected override void OnCreate (Bundle bundle)
  6.     {
  7.         base.OnCreate(bundle);
  8.         SetContentView(Resource.Layout.Main);
  9.         StartListening();
  10.         Button bt = FindViewById<Button>(Resource.Id.button1);
  11.         bt.Click += delegate { StartListening(); };
  12.     }
  13.  
  14.     public void StartListening()
  15.     {
  16.         this.udp.BeginReceive(Receive, new object());
  17.     }
  18.  
  19.     public void Receive(IAsyncResult ar)
  20.     {
  21.         IPEndPoint ip = new IPEndPoint(IPAddress.Any, 45000);
  22.         byte[] bytes = udp.EndReceive(ar, ref ip);
  23.         DisplayMessage(Encoding.ASCII.GetString(bytes));
  24.         StartListening();
  25.     }
  26.  
  27.     public void DisplayMessage(string message)
  28.     {
  29.         FindViewById<TextView>(Resource.Id.textView1).Text = message;
  30.     }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement