Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. private static Int32 port = 11000;
  2. private static UdpClient udpClient = new UdpClient(port);
  3.  
  4. public static void receive_threaded()
  5. {
  6. Thread t = new Thread(() =>
  7. {
  8. while (true)
  9. {
  10. IPEndPoint remoteIPEndPoint = new IPEndPoint(IPAddress.Any, port);
  11. byte[] content = udpClient.Receive(ref remoteIPEndPoint);
  12.  
  13. if (content.Length > 0)
  14. {
  15. string message = Encoding.UTF8.GetString(content);
  16. if (action_message_receive != null) action_message_receive(String.Format("Recv({0}): {1}", remoteIPEndPoint.Port, message));
  17. parseMessage(message);
  18. }
  19. }
  20. });
  21. t.Start();
  22. }
  23.  
  24. private static void send_message(string ip, string message)
  25. {
  26. byte[] packetData = System.Text.UTF8Encoding.UTF8.GetBytes(message);
  27.  
  28. int port = 11000;
  29.  
  30. IPEndPoint ep = new IPEndPoint(IPAddress.Parse(ip), port);
  31. Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  32. client.SendTo(packetData, ep);
  33.  
  34. if (action_message_send != null) action_message_send("Send: " + message);
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement