Advertisement
akass

receive

May 24th, 2015
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.66 KB | None | 0 0
  1. DatagramSocket socket = new DatagramSocket(5001);  
  2.  public void Recive()
  3.         {
  4.             WifiManager wifi = (WifiManager)ApplicationContext.GetSystemService(Context.WifiService);
  5.             Android.Net.Wifi.WifiManager.MulticastLock multicastLock = wifi.CreateMulticastLock("MediaPlayer");
  6.  
  7.             if (multicastLock != null)
  8.             {
  9.                 multicastLock.Acquire();
  10.             }
  11.             Android.Net.Wifi.WifiManager.WifiLock castLock = wifi.CreateWifiLock("MediaPlayer");
  12.             if (castLock != null)
  13.             {
  14.                 castLock.Acquire();
  15.             }
  16.  
  17.  
  18.            
  19.          
  20.             Task.Run(async () =>
  21.             {
  22.                 socket.Broadcast = true;
  23.                 byte[] buf = new byte[1024];
  24.                 DatagramPacket packet = new DatagramPacket(buf, buf.Length);
  25.                 LinearLayout parent = (LinearLayout)FindViewById(Resource.Id.chat);
  26.               View view = LayoutInflater.Inflate(Resource.Layout.Opponent, parent, false);
  27.              
  28.                
  29.                 while (true)
  30.                 {
  31.                  
  32.                     await socket.ReceiveAsync(packet);
  33.                     if (packet.GetData() != null)
  34.                     {
  35.                         message mess = new message(packet);
  36.                         view.FindViewById<TextView>(Resource.Id.nick).Text = (string)mess.nick;//"Opponent";
  37.                         view.FindViewById<TextView>(Resource.Id.message).Text = (string)mess.mess;
  38.                         parent.AddView(view);
  39.                        
  40.                     }
  41.                 }
  42.             });
  43.            
  44.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement