public void CloseClient() { inputStream?.Dispose(); outputStream?.Dispose(); inputStream = null; outputStream = null; streamSocket.Dispose(); clientIsConnected = false; ClientItems.Add("client closed its socket"); }
public async void StartClient() { try { // Create the StreamSocket and establish a connection to the echo server. streamSocket = new StreamSocket(); // The server hostname that we will be establishing a connection to. In this example, the server and client are in the same process. var hostName = new Windows.Networking.HostName(ServerAddress); ClientItems.Add(string.Format("client is trying to connect to {0}:{1} ...", ServerAddress, ServerPort)); await streamSocket.ConnectAsync(hostName, ServerPort); clientIsConnected = true; ClientItems.Add("client connected"); } catch (Exception ex) { SocketErrorStatus webErrorStatus = SocketError.GetStatus(ex.GetBaseException().HResult); ClientItems.Add(webErrorStatus.ToString() != "Unknown" ? webErrorStatus.ToString() : ex.Message); } }
public async void SendClientMessage() { try { // Send a request to the echo server. //string request = "Hello, World!"; if (outputStream == null) { outputStream = streamSocket.OutputStream.AsStreamForWrite(); } var streamWriter = new StreamWriter(outputStream); await streamWriter.WriteLineAsync(ClientMessage); await streamWriter.FlushAsync(); ClientItems.Add(string.Format("client sent the request: \"{0}\"", ClientMessage)); // Read data from the echo server. string response; if (inputStream == null) { inputStream = streamSocket.InputStream.AsStreamForRead(); } StreamReader streamReader = new StreamReader(inputStream); response = await streamReader.ReadLineAsync(); ClientItems.Add(string.Format("client received the response: \"{0}\" ", response)); } catch (Exception ex) { SocketErrorStatus webErrorStatus = SocketError.GetStatus(ex.GetBaseException().HResult); ClientItems.Add(webErrorStatus.ToString() != "Unknown" ? webErrorStatus.ToString() : ex.Message); CloseClient(); } }
private async void StartClient() { try { // Create the StreamSocket and establish a connection to the echo server. using (var streamSocket = new Windows.Networking.Sockets.StreamSocket()) { // The server hostname that we will be establishing a connection to. In this example, the server and client are in the same process. var hostName = new Windows.Networking.HostName("localhost"); await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync( CoreDispatcherPriority.Normal, () => ClientItems.Add("client is trying to connect...")); await streamSocket.ConnectAsync(hostName, PortNumber); await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync( CoreDispatcherPriority.Normal, () => ClientItems.Add("client connected")); // Send a request to the echo server. string request = "Hello, World!"; using (Stream outputStream = streamSocket.OutputStream.AsStreamForWrite()) { using (var streamWriter = new StreamWriter(outputStream)) { await streamWriter.WriteLineAsync(request); await streamWriter.FlushAsync(); } } await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync( CoreDispatcherPriority.Normal, () => ClientItems.Add(string.Format("client sent the request: \"{0}\"", request))); // Read data from the echo server. string response; using (Stream inputStream = streamSocket.InputStream.AsStreamForRead()) { using (StreamReader streamReader = new StreamReader(inputStream)) { response = await streamReader.ReadLineAsync(); } } await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync( CoreDispatcherPriority.Normal, () => ClientItems.Add(string.Format("client received the response: \"{0}\" ", response))); } await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync( CoreDispatcherPriority.Normal, () => ClientItems.Add("client closed its socket")); } catch (Exception ex) { Windows.Networking.Sockets.SocketErrorStatus webErrorStatus = Windows.Networking.Sockets.SocketError.GetStatus(ex.GetBaseException().HResult); await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync( CoreDispatcherPriority.Normal, () => ClientItems.Add(webErrorStatus.ToString() != "Unknown" ? webErrorStatus.ToString() : ex.Message)); } //RaisePropertyChanged("ClientItems"); }