Guest User

Untitled

a guest
Oct 17th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. private async void testUdpSocketServer(string message)
  2. {
  3.  
  4.  
  5. DatagramSocket socket = new DatagramSocket();
  6.  
  7. socket.MessageReceived += Socket_MessageReceived;
  8.  
  9. //You can use any port that is not currently in use already on the machine. We will be using two separate and random
  10. //ports for the client and server because both the will be running on the same machine.
  11. string serverPort = "1337";
  12. string clientPort = "1338";
  13.  
  14. //Because we will be running the client and server on the same machine, we will use localhost as the hostname.
  15. Windows.Networking.HostName serverHost = new Windows.Networking.HostName("255.255.255.255");
  16.  
  17. //Bind the socket to the clientPort so that we can start listening for UDP messages from the UDP echo server.
  18. await socket.BindServiceNameAsync(clientPort);
  19.  
  20. //Write a message to the UDP echo server.
  21. Stream streamOut = (await socket.GetOutputStreamAsync(serverHost, serverPort)).AsStreamForWrite();
  22. StreamWriter writer = new StreamWriter(streamOut);
  23.  
  24. await writer.WriteLineAsync(message);
  25. await writer.FlushAsync();
  26.  
  27.  
  28. }
  29.  
  30. private async void Socket_MessageReceived(DatagramSocket sender,
  31. DatagramSocketMessageReceivedEventArgs args)
  32. {
  33. //Read the message that was received from the UDP echo server.
  34. Stream streamIn = args.GetDataStream().AsStreamForRead();
  35. StreamReader reader = new StreamReader(streamIn);
  36. string message = await reader.ReadLineAsync();
  37. }
Add Comment
Please, Sign In to add comment