private async void StartServer() { try { var streamSocketListener = new StreamSocketListener(); // The ConnectionReceived event is raised when connections are received. streamSocketListener.ConnectionReceived += this.StreamSocketListener_ConnectionReceived; // Start listening for incoming TCP connections on the specified port. You can specify any port that's not currently in use. await streamSocketListener.BindServiceNameAsync(PortNumber); await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync( CoreDispatcherPriority.Normal, () => ServerItems.Add("server is listening...")); } 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, () => ServerItems.Add(webErrorStatus.ToString() != "Unknown" ? webErrorStatus.ToString() : ex.Message)); } //RaisePropertyChanged("ServerItems"); }
private async void StreamSocketListener_ConnectionReceived(Windows.Networking.Sockets.StreamSocketListener sender, Windows.Networking.Sockets.StreamSocketListenerConnectionReceivedEventArgs args) { string request; using (var streamReader = new StreamReader(args.Socket.InputStream.AsStreamForRead())) { request = await streamReader.ReadLineAsync(); } await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync( CoreDispatcherPriority.Normal, () => ServerItems.Add(string.Format("server received the request: \"{0}\"", request))); // Echo the request back as the response. using (Stream outputStream = args.Socket.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, () => ServerItems.Add(string.Format("server sent back the response: \"{0}\"", request))); sender.Dispose(); await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync( CoreDispatcherPriority.Normal, () => ServerItems.Add("server closed its socket")); //RaisePropertyChanged("ServerItems"); }
public async void StopServer() { CloseServerConnection(); streamSocketListener?.Dispose(); serverIsRunning = false; await CoreApplication.MainView.CoreWindow. Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => ServerItems.Add("server closed its socket")); }
private async void StreamSocketListener_ConnectionReceived(StreamSocketListener sender, StreamSocketListenerConnectionReceivedEventArgs args) { bool clientconnected = true; await CoreApplication.MainView.CoreWindow. Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => ServerItems.Add(string.Format("client connected from {0}:{1} ...", args.Socket.Information.RemoteAddress, args.Socket.Information.RemotePort))); while (clientconnected) { try { string request; if (streamReader == null) { streamReader = new StreamReader(args.Socket.InputStream.AsStreamForRead()); } request = await streamReader.ReadLineAsync(); await CoreApplication.MainView.CoreWindow. Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => ServerItems.Add(string.Format("server received the request: \"{0}\"", request))); // Echo the request back as the response. if (streamWriter == null) { Stream ServeroutputStream = args.Socket.OutputStream.AsStreamForWrite(); streamWriter = new StreamWriter(ServeroutputStream); } await streamWriter.WriteLineAsync(request); await streamWriter.FlushAsync(); await CoreApplication.MainView.CoreWindow.Dispatcher. RunAsync(CoreDispatcherPriority.Normal, () => ServerItems.Add(string.Format("server sent back the response: \"{0}\"", request))); } catch (Exception ex) { clientconnected = false; CloseServerConnection(); SocketErrorStatus webErrorStatus = SocketError.GetStatus(ex.GetBaseException().HResult); await CoreApplication.MainView.CoreWindow.Dispatcher. RunAsync(CoreDispatcherPriority.Normal, () => ServerItems.Add(webErrorStatus.ToString() != "Unknown" ? webErrorStatus.ToString() : ex.Message)); } } }
internal IServerItem[] AddServerItems(IEnumerable <string> hostQueryAddresses) { var result = new List <IServerItem>(hostQueryAddresses.Count()); foreach (var hostQueryAddress in hostQueryAddresses) { var pos = hostQueryAddress.IndexOf(':'); var addr = hostQueryAddress.Substring(0, pos); var port = 0; int.TryParse(hostQueryAddress.Substring(pos + 1), out port); var serverItem = new ServerItem { Host = IPAddress.Parse(addr), QueryPort = port, Name = addr }; ServerItems.Add(serverItem); result.Add(serverItem); } return(result.ToArray()); }
public async void StartServer() { try { streamSocketListener = new StreamSocketListener(); // The ConnectionReceived event is raised when connections are received. streamSocketListener.ConnectionReceived += StreamSocketListener_ConnectionReceived; // Start listening for incoming TCP connections on the specified port. You can specify any port that's not currently in use. await streamSocketListener.BindServiceNameAsync(PortNumber); serverIsRunning = true; ServerItems.Add(string.Format("server is listening on port {0} ...", PortNumber)); } catch (Exception ex) { SocketErrorStatus webErrorStatus = SocketError.GetStatus(ex.GetBaseException().HResult); ServerItems.Add(webErrorStatus.ToString() != "Unknown" ? webErrorStatus.ToString() : ex.Message); } }