Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private async void testUdpSocketServer(string message)
- {
- DatagramSocket socket = new DatagramSocket();
- socket.MessageReceived += Socket_MessageReceived;
- //You can use any port that is not currently in use already on the machine. We will be using two separate and random
- //ports for the client and server because both the will be running on the same machine.
- string serverPort = "1337";
- string clientPort = "1338";
- //Because we will be running the client and server on the same machine, we will use localhost as the hostname.
- Windows.Networking.HostName serverHost = new Windows.Networking.HostName("255.255.255.255");
- //Bind the socket to the clientPort so that we can start listening for UDP messages from the UDP echo server.
- await socket.BindServiceNameAsync(clientPort);
- //Write a message to the UDP echo server.
- Stream streamOut = (await socket.GetOutputStreamAsync(serverHost, serverPort)).AsStreamForWrite();
- StreamWriter writer = new StreamWriter(streamOut);
- await writer.WriteLineAsync(message);
- await writer.FlushAsync();
- }
- private async void Socket_MessageReceived(DatagramSocket sender,
- DatagramSocketMessageReceivedEventArgs args)
- {
- //Read the message that was received from the UDP echo server.
- Stream streamIn = args.GetDataStream().AsStreamForRead();
- StreamReader reader = new StreamReader(streamIn);
- string message = await reader.ReadLineAsync();
- }
Add Comment
Please, Sign In to add comment