public async Task Test_Bad_Service(EchoClient_Rfc_862.ProtocolType protocol) { var host = new HostName("localhost"); var serverOptions = new EchoServer_Rfc_862.ServerOptions() { LoggingLevel = EchoServer_Rfc_862.ServerOptions.Verbosity.None }; var clientOptions = new EchoClient_Rfc_862.ClientOptions() { LoggingLevel = EchoClient_Rfc_862.ClientOptions.Verbosity.None }; using (var server = new EchoServer_Rfc_862(serverOptions)) { bool serverOk = await server.StartAsync(); if (!serverOk) { Infrastructure.Error($"unable to start server"); return; } var client = new EchoClient_Rfc_862(clientOptions); var result = await client.WriteAsync(host, "79", protocol, "WontBeEchoed"); Infrastructure.IfTrueError(result.Succeeded != EchoClient_Rfc_862.EchoResult.State.Failed, "result.Succeeded "); Infrastructure.IfTrueError(!String.IsNullOrEmpty(result.Value), "result.Value is not null"); // ConnectionRefused is for TCP // ConnectionResetByPeer is for UDP Infrastructure.IfTrueError(result.Error != SocketErrorStatus.ConnectionRefused && result.Error != SocketErrorStatus.ConnectionResetByPeer, $"result.Error is wrong ({result.Error})"); } }
public static async Task <EchoClient_Rfc_862.EchoResult> RunTcpAsync() { // Hint: the using here means that the server is automatically closed // after the code is done. If you don't do that, the server stays open. using (var server = new EchoServer_Rfc_862()) { await server.StartAsync(); var client = new EchoClient_Rfc_862(); // You only get the echo'd result from TCP with the final CloseAsync() // But for UDP you get the returned packet. var partialResult = await client.WriteAsync( new Windows.Networking.HostName("localhost"), server.Options.Service, EchoClient_Rfc_862.ProtocolType.Tcp, "Hello, echo server!"); // TODO: remove magic value? // Wait for the echo to have worked. The server is right here, so the // echo should be almost instantaneous. await Task.Delay(100); var completeResult = await client.CloseAsync(); // Close is essential for TCP. return(completeResult); } }
public async Task Test_Echo_Good_Path_Udp() { using (var server = new EchoServer_Rfc_862()) { var client = new EchoClient_Rfc_862(); await server.StartAsync(); var result1 = await client.WriteAsync(new HostName("localhost"), server.Options.Service, EchoClient_Rfc_862.ProtocolType.Udp, "ABC"); Infrastructure.IfTrueError(result1.Succeeded != EchoClient_Rfc_862.EchoResult.State.Succeeded, "Close TCP status should be succeeded"); Infrastructure.IfTrueError(result1.Value != "ABC", $"Should have gotten back abcDEF123456, not {result1.Value}"); var result2 = await client.WriteAsync(new HostName("localhost"), server.Options.Service, EchoClient_Rfc_862.ProtocolType.Udp, "123"); Infrastructure.IfTrueError(result2.Succeeded != EchoClient_Rfc_862.EchoResult.State.Succeeded, "Close TCP status should be succeeded"); Infrastructure.IfTrueError(result2.Value != "123", $"Should have gotten back abcDEF123456, not {result2.Value}"); } }
public async Task Test_Echo_Good_Path_Tcp() { using (var server = new EchoServer_Rfc_862()) { var client = new EchoClient_Rfc_862(); await server.StartAsync(); var result1 = await client.WriteAsync(new HostName("localhost"), server.Options.Service, EchoClient_Rfc_862.ProtocolType.Tcp, "ABCdef"); Infrastructure.IfTrueError(result1.Succeeded != EchoClient_Rfc_862.EchoResult.State.InProgress, "TCP status should be in progress"); var result2 = await client.WriteAsync(new HostName("localhost"), server.Options.Service, EchoClient_Rfc_862.ProtocolType.Tcp, "123456"); Infrastructure.IfTrueError(result2.Succeeded != EchoClient_Rfc_862.EchoResult.State.InProgress, "TCP status should be in progress"); await Task.Delay(100); //TODO: artificial delay to get the results. var result3 = await client.CloseAsync(); Infrastructure.IfTrueError(result3.Succeeded != EchoClient_Rfc_862.EchoResult.State.Succeeded, "Close TCP status should be succeeded"); Infrastructure.IfTrueError(result3.Value != "ABCdef123456", $"Should have gotten back abcDEF123456, not {result3.Value}"); } }
public async Task Test_Bad_Host(EchoClient_Rfc_862.ProtocolType protocol) { var host = new HostName("invalid.host.doesnt.exist.example.com"); var clientOptions = new EchoClient_Rfc_862.ClientOptions() { LoggingLevel = EchoClient_Rfc_862.ClientOptions.Verbosity.None }; var client = new EchoClient_Rfc_862(clientOptions); var result = await client.WriteAsync(host, "10013", protocol, "ABCDEF"); Infrastructure.IfTrueError(result.Succeeded != EchoClient_Rfc_862.EchoResult.State.Failed, "result.Succeeded "); Infrastructure.IfTrueError(!String.IsNullOrEmpty(result.Value), "result.Value is not null"); Infrastructure.IfTrueError(result.Error != SocketErrorStatus.HostNotFound, $"result.Error is wrong ({result.Error})"); }
public static async Task <EchoClient_Rfc_862.EchoResult> RunUdpAsync() { // Hint: the using here means that the server is automatically closed // after the code is done. If you don't do that, the server stays open. using (var server = new EchoServer_Rfc_862()) { await server.StartAsync(); var client = new EchoClient_Rfc_862(); var result = await client.WriteAsync( new Windows.Networking.HostName("localhost"), server.Options.Service, EchoClient_Rfc_862.ProtocolType.Udp, "Hello, echo server!"); await client.CloseAsync(); // Close is really needed for TCP. return(result); } }
private async void OnSend(object sender, RoutedEventArgs e) { try { var host = new HostName(uiAddress.Text); var service = uiService.Text; var data = uiData.Text; var ptype = uiProtocolType.IsOn ? EchoClient_Rfc_862.ProtocolType.Udp : EchoClient_Rfc_862.ProtocolType.Tcp; // double-checked; off is TCP. if (client == null) { client = new EchoClient_Rfc_862(); client.LogEvent += Client_LogEvent; } await client.WriteAsync(host, service, ptype, data); } catch (Exception ex) { Client_LogEvent(this, $"ERROR: Client: Write exception {ex.Message} for host {uiAddress.Text}"); } }