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 StartServer() { try { // Start listening for incoming TCP connections on the specified port. You can specify any port that's not currently in use. await streamSocketListener.BindServiceNameAsync(port.ToString()); Debug.Log("server is listening..."); } catch (System.Exception ex) { Windows.Networking.Sockets.SocketErrorStatus webErrorStatus = Windows.Networking.Sockets.SocketError.GetStatus(ex.GetBaseException().HResult); Debug.Log(webErrorStatus.ToString() != "Unknown" ? webErrorStatus.ToString() : ex.Message); } }
public static async Task <int> WriteAsync(char[] chars) { int ret = -1; try { using (Stream outputStream = streamSocket.OutputStream.AsStreamForWrite()) { using (var streamWriter = new StreamWriter(outputStream)) { await streamWriter.WriteAsync(chars, 0, chars.Length); await streamWriter.FlushAsync(); ret = chars.Length; } } MainPage.MP.clientListBox.Items.Add(string.Format("client sent the request: \"{0}\"", ("" + chars + ""))); } catch (Exception ex) { Windows.Networking.Sockets.SocketErrorStatus webErrorStatus = Windows.Networking.Sockets.SocketError.GetStatus(ex.GetBaseException().HResult); MainPage.MP.clientListBox.Items.Add(webErrorStatus.ToString() != "Unknown" ? webErrorStatus.ToString() : ex.Message); } return(ret); }
public static async Task <byte[]> ReadAsync() { byte[] bytes = new byte[0]; try { int ReadBufferLength = 1024; char[] chars = new char[ReadBufferLength]; // Read data from the echo server. int responseLength; using (Stream inputStream = streamSocket.InputStream.AsStreamForRead()) { using (StreamReader streamReader = new StreamReader(inputStream)) { responseLength = await streamReader.ReadAsync(chars, 0, ReadBufferLength); //responseLength = await streamReader.ReadAsync(chars, 0, ReadBufferLength).WithCancellation(CancellationToken.None, request.Abort, true)) } } //Truncate the array bytes = Encoding.Unicode.GetBytes(chars); bytes = bytes = bytes.Skip(0).Take(responseLength).ToArray(); string s = System.Text.Encoding.UTF8.GetString(bytes, 0, bytes.Length); MainPage.MP.clientListBox.Items.Add(string.Format("client received the response: \"{0}\" ", s)); } catch (Exception ex) { Windows.Networking.Sockets.SocketErrorStatus webErrorStatus = Windows.Networking.Sockets.SocketError.GetStatus(ex.GetBaseException().HResult); MainPage.MP.clientListBox.Items.Add(webErrorStatus.ToString() != "Unknown" ? webErrorStatus.ToString() : ex.Message); } return(bytes); }
public static async Task <char> GetCh() { char[] bytes = null; char ch = '\0'; try { int ReadBufferLength = 1; bytes = new char[ReadBufferLength]; int responseLength; // Read data from the echo server. do { using (Stream inputStream = streamSocket.InputStream.AsStreamForRead()) { using (StreamReader streamReader = new StreamReader(inputStream)) { responseLength = await streamReader.ReadAsync(bytes, 0, ReadBufferLength); } } } while (responseLength != 1); //Truncate the array ch = bytes[0]; MainPage.MP.clientListBox.Items.Add(string.Format("client received the response: \"{0}\" ", (("" + bytes) + ""))); } catch (Exception ex) { Windows.Networking.Sockets.SocketErrorStatus webErrorStatus = Windows.Networking.Sockets.SocketError.GetStatus(ex.GetBaseException().HResult); MainPage.MP.clientListBox.Items.Add(webErrorStatus.ToString() != "Unknown" ? webErrorStatus.ToString() : ex.Message); } return(ch); }
public async void StartClient(Client client) { // create StreamSocket and establish connection to the server try { using (var streamSocket = new StreamSocket()) { var hostName = new HostName("localhost"); client.ClientStatus = "Connecting to the server ....." + client.PortNr; ClientVmRef.AddToBuffer(client.ClientStatus); await streamSocket.ConnectAsync(hostName, client.PortNr); ClientVmRef.AddToBuffer("Client Connected"); using (Stream outputStream = streamSocket.OutputStream.AsStreamForWrite()) { using (var streamWriter = new StreamWriter(outputStream)) { await streamWriter.WriteLineAsync(client.SentMessage); await streamWriter.FlushAsync(); } ClientVmRef.AddToBuffer(String.Format("send message: \"{0}\"", client.SentMessage)); using (Stream inputStream = streamSocket.InputStream.AsStreamForRead()) { using (var streamReader = new StreamReader(inputStream)) { client.ReceivedMessage = await streamReader.ReadLineAsync(); } } ClientVmRef.AddToBuffer(string.Format("Received message: \"{0}\"", client.ReceivedMessage)); } ClientVmRef.AddToBuffer("Client closed its socket"); } } catch (Exception ex) { Windows.Networking.Sockets.SocketErrorStatus webErrorStatus = Windows.Networking.Sockets.SocketError.GetStatus(ex.GetBaseException().HResult); client.ClientStatus = webErrorStatus.ToString() != "Unknown" ? webErrorStatus.ToString() : ex.Message; ClientVmRef.AddToBuffer(client.ClientStatus); } }
public async void StartClient() { try { var hostName = new Windows.Networking.HostName(ServerIP); Debug.WriteLine("client is trying to connect..."); await streamSocket.ConnectAsync(hostName, Port); Debug.WriteLine("client connected"); Connected = true; } catch (Exception ex) { Windows.Networking.Sockets.SocketErrorStatus webErrorStatus = Windows.Networking.Sockets.SocketError.GetStatus(ex.GetBaseException().HResult); Debug.WriteLine(webErrorStatus.ToString() != "Unknown" ? webErrorStatus.ToString() : ex.Message); } }
public async void Start() { try { streamSocketListener = new Windows.Networking.Sockets.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(baseTCPPort.ToString()); Debug("server is listening..."); } catch (Exception ex) { Windows.Networking.Sockets.SocketErrorStatus webErrorStatus = Windows.Networking.Sockets.SocketError.GetStatus(ex.GetBaseException().HResult); Debug(webErrorStatus.ToString() != "Unknown" ? webErrorStatus.ToString() : ex.Message); } }
public static void CloseSocket() { try { streamSocket.Dispose(); } catch (Exception ex) { Windows.Networking.Sockets.SocketErrorStatus webErrorStatus = Windows.Networking.Sockets.SocketError.GetStatus(ex.GetBaseException().HResult); MainPage.MP.clientListBox.Items.Add(webErrorStatus.ToString() != "Unknown" ? webErrorStatus.ToString() : ex.Message); } }
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"); this.clientListBox.Items.Add("client is trying to connect..."); await streamSocket.ConnectAsync(hostName, StreamSocketAndListenerPage.PortNumber); this.clientListBox.Items.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(); } } this.clientListBox.Items.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(); } } this.clientListBox.Items.Add(string.Format("client received the response: \"{0}\" ", response)); } this.clientListBox.Items.Add("client closed its socket"); } catch (Exception ex) { Windows.Networking.Sockets.SocketErrorStatus webErrorStatus = Windows.Networking.Sockets.SocketError.GetStatus(ex.GetBaseException().HResult); this.clientListBox.Items.Add(webErrorStatus.ToString() != "Unknown" ? webErrorStatus.ToString() : ex.Message); } }
public async Task Send(string request) { try { await streamWriter.WriteLineAsync(request); await streamWriter.FlushAsync(); } catch (Exception ex) { Windows.Networking.Sockets.SocketErrorStatus webErrorStatus = Windows.Networking.Sockets.SocketError.GetStatus(ex.GetBaseException().HResult); MainPage.MP.clientListBox.Items.Add(webErrorStatus.ToString() != "Unknown" ? webErrorStatus.ToString() : ex.Message); } }
public async Task <string> ReceiveLn() { string ret = ""; char[] chars = new char[2]; try { ret = await streamReader.ReadLineAsync(); } catch (Exception ex) { Windows.Networking.Sockets.SocketErrorStatus webErrorStatus = Windows.Networking.Sockets.SocketError.GetStatus(ex.GetBaseException().HResult); MainPage.MP.clientListBox.Items.Add(webErrorStatus.ToString() != "Unknown" ? webErrorStatus.ToString() : ex.Message); } return(ret); }
public async Task SendCh(char ch) { char[] chars = new char[2]; chars[0] = ch; try { await streamWriter.WriteAsync(ch); await streamWriter.FlushAsync(); } catch (Exception ex) { Windows.Networking.Sockets.SocketErrorStatus webErrorStatus = Windows.Networking.Sockets.SocketError.GetStatus(ex.GetBaseException().HResult); MainPage.MP.clientListBox.Items.Add(webErrorStatus.ToString() != "Unknown" ? webErrorStatus.ToString() : ex.Message); } }
public void CloseSocket() { try { outputStream = null; streamWriter = null; inputStream = null; streamReader = null; streamSocket.Dispose(); _Mode = Mode.Disconnected; Status.Text = "Socekt Disconnected"; } catch (Exception ex) { Windows.Networking.Sockets.SocketErrorStatus webErrorStatus = Windows.Networking.Sockets.SocketError.GetStatus(ex.GetBaseException().HResult); MainPage.MP.clientListBox.Items.Add(webErrorStatus.ToString() != "Unknown" ? webErrorStatus.ToString() : ex.Message); } }
private async void StartServer() { try { var streamSocketListener = new Windows.Networking.Sockets.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(StreamSocketAndListenerPage.PortNumber); this.serverListBox.Items.Add("server is listening..."); } catch (Exception ex) { Windows.Networking.Sockets.SocketErrorStatus webErrorStatus = Windows.Networking.Sockets.SocketError.GetStatus(ex.GetBaseException().HResult); this.serverListBox.Items.Add(webErrorStatus.ToString() != "Unknown" ? webErrorStatus.ToString() : ex.Message); } }
public async Task <char> ReceiveCh() { char ch = ' '; char[] chars = new char[2]; try { int responseLength = await streamReader.ReadAsync(chars, 0, 1); if (responseLength == 1) { ch = chars[0]; } } catch (Exception ex) { Windows.Networking.Sockets.SocketErrorStatus webErrorStatus = Windows.Networking.Sockets.SocketError.GetStatus(ex.GetBaseException().HResult); MainPage.MP.clientListBox.Items.Add(webErrorStatus.ToString() != "Unknown" ? webErrorStatus.ToString() : ex.Message); } MainPage.MP.clientListBox.Items.Add("Sent " + ch + "\r\n"); return(ch); }
public static async Task StartClient() { try { // Create the StreamSocket and establish a connection to the echo server. 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("192.168.0.137"); MainPage.MP.clientListBox.Items.Add("client is trying to connect..."); await streamSocket.ConnectAsync(hostName, "1234"); MainPage.MP.clientListBox.Items.Add("client connected"); } catch (Exception ex) { Windows.Networking.Sockets.SocketErrorStatus webErrorStatus = Windows.Networking.Sockets.SocketError.GetStatus(ex.GetBaseException().HResult); MainPage.MP.clientListBox.Items.Add(webErrorStatus.ToString() != "Unknown" ? webErrorStatus.ToString() : ex.Message); } }
private async void DoSomeShit() { try { // Create the StreamSocket and establish a connection to the echo server. using (var 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("1.1.1.1"); this.clientListBox.Items.Add("client is trying to connect..."); await streamSocket.ConnectAsync(hostName, "853", SocketProtectionLevel.Tls12); IBuffer certificateBuffer = streamSocket.Information.ServerCertificate.GetCertificateBlob(); Stream certificateStream = certificateBuffer.AsStream(); //Take the SHA2-256 hash of the DER ASN.1 encoded value Byte[] digest; using (var sha2 = new SHA256Managed()) { digest = sha2.ComputeHash(certificateStream); } //Convert hash to base64 String hash = Convert.ToBase64String(digest); this.clientListBox.Items.Add("client connected"); } this.clientListBox.Items.Add("client closed its socket"); } catch (Exception ex) { Windows.Networking.Sockets.SocketErrorStatus webErrorStatus = Windows.Networking.Sockets.SocketError.GetStatus(ex.GetBaseException().HResult); this.clientListBox.Items.Add(webErrorStatus.ToString() != "Unknown" ? webErrorStatus.ToString() : ex.Message); } }
/// <summary> /// Starts a Proxy that takes local DNS requests and transforms them into /// DNS-over-TLS versions instead, using the provided DNS-over-TLS endpoint. /// </summary> private async void StartDNSToDNSOverTLSProxy() { try { var serverDatagramSocket = new DatagramSocket(); // The ConnectionReceived event is raised when connections are received. serverDatagramSocket.MessageReceived += ServerDatagramSocket_MessageReceived; this.serverListBox.Items.Add("server is about to bind..."); var hostName = new Windows.Networking.HostName("192.168.175.13"); // Start listening for incoming TCP connections on the specified port. You can specify any port that's not currently in use. //await serverDatagramSocket.BindEndpointAsync(hostName, DNS_LISTENING_PORT); await serverDatagramSocket.BindServiceNameAsync(DNS_LISTENING_PORT); this.serverListBox.Items.Add(string.Format("server is bound to port number {0}", DNS_LISTENING_PORT)); } catch (Exception ex) { Windows.Networking.Sockets.SocketErrorStatus webErrorStatus = Windows.Networking.Sockets.SocketError.GetStatus(ex.GetBaseException().HResult); this.serverListBox.Items.Add(webErrorStatus.ToString() != "Unknown" ? webErrorStatus.ToString() : ex.Message); } }
private async Task StartSocketClient() { try { string port = tbPort.Text; string svr = tbSvrName.Text; //// Create the StreamSocket and establish a connection to the echo server. //using (var streamSocket = new Windows.Networking.Sockets.StreamSocket()) //{ 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. string host = tbSvrName.Text; port = tbPort.Text; var hostName = new Windows.Networking.HostName(host); MainPage.MP.clientListBox.Items.Add("client is trying to connect..."); await streamSocket.ConnectAsync(hostName, port); _Mode = Mode.JustConnected; await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { status.Text = "Socket Connected. Getting config data"; }); MainPage.MP.clientListBox.Items.Add("client connected"); string request = "Hello, World"; outputStream = streamSocket.OutputStream.AsStreamForWrite(); streamWriter = new StreamWriter(outputStream); await streamWriter.WriteLineAsync(request); await streamWriter.FlushAsync(); MainPage.MP.clientListBox.Items.Add(string.Format("client sent the request: \"{0}\"", request)); // Read data from the echo server. string response; string json1 = ""; string json2 = ""; char[] chars = new char[1000]; chars[1] = 'Z'; int responseLength; inputStream = streamSocket.InputStream.AsStreamForRead(); streamReader = new StreamReader(inputStream); response = await streamReader.ReadLineAsync(); recvdText.Text = "" + response; responseLength = await streamReader.ReadAsync(chars, 0, 1000); recvdText.Text = "" + chars[0]; if (chars[0] == '@') { _Mode = Mode.JustConnected; await streamWriter.WriteAsync('0'); await streamWriter.FlushAsync(); } responseLength = await streamReader.ReadAsync(chars, 0, 1); recvdText.Text = "" + chars[0]; if (chars[0] == '1') { _Mode = Mode.ACK1; await streamWriter.WriteAsync('2'); await streamWriter.FlushAsync(); } responseLength = await streamReader.ReadAsync(chars, 0, 1); recvdText.Text = "" + chars[0]; if (chars[0] == '3') { _Mode = Mode.ACK3; await streamWriter.WriteAsync('4'); await streamWriter.FlushAsync(); } responseLength = await streamReader.ReadAsync(chars, 0, 1); recvdText.Text = "" + chars[0]; if (chars[0] == '5') { _Mode = Mode.ACK5; await streamWriter.WriteAsync('!'); await streamWriter.FlushAsync(); } responseLength = await streamReader.ReadAsync(chars, 0, 1); recvdText.Text = "" + chars[0]; if (chars[0] == '/') { await streamWriter.WriteAsync('/'); await streamWriter.FlushAsync(); _Mode = Mode.AwaitJson; json1 = await streamReader.ReadLineAsync(); recvdText.Text = json1; _Mode = Mode.JsonConfig; await MainPage.MP.UpdateTextAsync(json1); await streamWriter.WriteAsync('~'); await streamWriter.FlushAsync(); json2 = await streamReader.ReadLineAsync(); recvdText.Text = "" + json2; _Mode = Mode.Config; await MainPage.MP.UpdateTextAsync(json2); recvdText.Text = ""; _Mode = Mode.Running; Listen(); await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { status.Text = "Config data received: Press [Back]"; this.buttonStartRecv.IsEnabled = false; this.buttonStopRecv.IsEnabled = false; }); } MainPage.MP.clientListBox.Items.Add(string.Format("client received the response: \"{0}\" ", "Got Json")); } catch (Exception ex) { Windows.Networking.Sockets.SocketErrorStatus webErrorStatus = Windows.Networking.Sockets.SocketError.GetStatus(ex.GetBaseException().HResult); MainPage.MP.clientListBox.Items.Add(webErrorStatus.ToString() != "Unknown" ? webErrorStatus.ToString() : ex.Message); } }