private async void SendUdpMessage(char[] message) { Stream streamOut = (await socket.GetOutputStreamAsync(serverHost, port)).AsStreamForWrite(); StreamWriter streamWriter = new StreamWriter(streamOut); await streamWriter.WriteLineAsync(message, 0, message.Length); await streamWriter.FlushAsync(); }
private async void ServerDatagramSocket_MessageReceived(Windows.Networking.Sockets.DatagramSocket sender, Windows.Networking.Sockets.DatagramSocketMessageReceivedEventArgs args) { string request; using (DataReader dataReader = args.GetDataReader()) { request = dataReader.ReadString(dataReader.UnconsumedBufferLength).Trim(); } await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => listbox_Logger.Items.Add(request)); // Echo the request back as the response. using (Stream outputStream = (await sender.GetOutputStreamAsync(args.RemoteAddress, "1337")).AsStreamForWrite()) { using (var streamWriter = new StreamWriter(outputStream)) { await streamWriter.WriteLineAsync(request); await streamWriter.FlushAsync(); } } //await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => this.listbox_Logger.Items.Add(string.Format("server sent back the response: \"{0}\"", request))); sender.Dispose(); // await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => this.listbox_Logger.Items.Add("server closed its socket")); }
private async void StartClient() { try { // Create the DatagramSocket and establish a connection to the echo server. var clientDatagramSocket = new Windows.Networking.Sockets.DatagramSocket(); clientDatagramSocket.MessageReceived += ClientDatagramSocket_MessageReceived; // 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 clientDatagramSocket.BindServiceNameAsync("servername"); // Send a request to the echo server. string request = "Hello, World!"; using (var serverDatagramSocket = new Windows.Networking.Sockets.DatagramSocket()) { using (Stream outputStream = (await serverDatagramSocket.GetOutputStreamAsync(hostName, "3655")).AsStreamForWrite()) { using (var streamWriter = new StreamWriter(outputStream)) { //await streamWriter.WriteAsync(); await streamWriter.FlushAsync(); } } } } catch (Exception ex) { Windows.Networking.Sockets.SocketErrorStatus webErrorStatus = Windows.Networking.Sockets.SocketError.GetStatus(ex.GetBaseException().HResult); } }
public void SendTo(byte[] messageData, UdpEndPoint endPoint) { using (var stream = (_Socket.GetOutputStreamAsync(new Windows.Networking.HostName(endPoint.IPAddress), endPoint.Port.ToString()).AsTask().Result)) { using (var outStream = stream.AsStreamForWrite()) { outStream.Write(messageData, 0, messageData.Length); outStream.Flush(); } } }
public async void StartReceiveAsync() { isRun = true; while(isRun) { if(!isReceive) { System.Diagnostics.Debug.WriteLine("Start Receive"); udpSocket = new Windows.Networking.Sockets.DatagramSocket(); udpSocket.MessageReceived += UdpSocket_MessageReceived; Windows.Networking.HostName hostName = null; IReadOnlyList<Windows.Networking.HostName> networkinfo = Windows.Networking.Connectivity.NetworkInformation.GetHostNames(); foreach (Windows.Networking.HostName h in networkinfo) { if (h.IPInformation != null) { Windows.Networking.Connectivity.IPInformation ipinfo = h.IPInformation; if (h.RawName == IPAddr) { hostName = h; break; } } } if (hostName != null) await udpSocket.BindEndpointAsync(hostName, Port); else await udpSocket.BindServiceNameAsync(Port); outstm = await udpSocket.GetOutputStreamAsync(new Windows.Networking.HostName("255.255.255.255"), "49002"); await outstm.FlushAsync(); Windows.Storage.Streams.DataWriter dw = new Windows.Storage.Streams.DataWriter(outstm); dw.WriteString("Start Receive"); await dw.StoreAsync(); isReceive = true; } else { if(CurrentStat.GPSStatus !=null & CurrentStat.ATTStatus != null) { if (onXPlaneStatReceived != null) onXPlaneStatReceived.Invoke(this, CurrentStat); } System.Diagnostics.Debug.WriteLine("Try To Sleep"); isReceive = false; await udpSocket.CancelIOAsync(); udpSocket.MessageReceived -= UdpSocket_MessageReceived; udpSocket.Dispose(); udpSocket = null; } await Task.Delay(waitSeconds*1000); } }
public async static Task SendMessage(Windows.Networking.Sockets.DatagramSocket socket, HostName hostName, string port, string request) { UTF8Encoding utf8 = new UTF8Encoding(); var temp = utf8.GetBytes(request); string request8 = utf8.GetString(temp); using (Stream outputStream = (await socket.GetOutputStreamAsync(hostName, port)).AsStreamForWrite()) { using (var streamWriter = new StreamWriter(outputStream)) { await streamWriter.WriteAsync(request8); await streamWriter.FlushAsync(); } } }
/// <summary> /// Stop voice recording. /// </summary> private async void StopCapture(MediaElement mediaElement) { if (Status != RecorderStatus.Recording) { return; } Debug.WriteLine("Stopping recording"); await _mediaCapture.StopRecordAsync(); Debug.WriteLine("Stop recording successful"); Status = RecorderStatus.Initial; _recordingStream.Seek(0); //mediaElement.AutoPlay = true; //mediaElement.SetSource(_recordingStream, ""); //mediaElement.Play(); //var buffer = new Windows.Storage.Streams.Buffer(4000000); //await _recordingStream.ReadAsync(buffer, buffer.Capacity, InputStreamOptions.None); //var bytes = buffer.ToArray(); var clientDatagramSocket = new Windows.Networking.Sockets.DatagramSocket(); var hostName = new Windows.Networking.HostName(HostName); await clientDatagramSocket.ConnectAsync(hostName, Port); using (var serverDatagramSocket = new Windows.Networking.Sockets.DatagramSocket()) { using (Stream outputStream = (await serverDatagramSocket.GetOutputStreamAsync(hostName, Port)).AsStreamForWrite()) { var buffer = new Windows.Storage.Streams.Buffer(MaxUdpPackageSize); while (true) { var retrievedBuffer = _recordingStream.ReadAsync(buffer, MaxUdpPackageSize, InputStreamOptions.None).GetResults(); if (retrievedBuffer == null || retrievedBuffer.Length < 1) { break; } outputStream.Write(retrievedBuffer.ToArray(), 0, (int)retrievedBuffer.Length); outputStream.Flush(); } } } }
private async void StartClient() { try { // Create the DatagramSocket and establish a connection to the echo server. var clientDatagramSocket = new Windows.Networking.Sockets.DatagramSocket(); clientDatagramSocket.MessageReceived += ClientDatagramSocket_MessageReceived; // 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 about to bind..."); await clientDatagramSocket.BindServiceNameAsync(UDPSocketPage.ClientPortNumber); this.clientListBox.Items.Add(string.Format("client is bound to port number {0}", UDPSocketPage.ClientPortNumber)); // Send a request to the echo server. string request = "Hello, World!"; using (var serverDatagramSocket = new Windows.Networking.Sockets.DatagramSocket()) { using (Stream outputStream = (await serverDatagramSocket.GetOutputStreamAsync(hostName, UDPSocketPage.ServerPortNumber)).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)); } 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); } }
private async void Characteristic_ValueChanged(GattCharacteristic sender, GattValueChangedEventArgs args) { // BT_Code: An Indicate or Notify reported that the value has changed. // Display the new value with a timestamp. var newValue = FormatValueByPresentation(args.CharacteristicValue, presentationFormat); Windows.Networking.Sockets.DatagramSocket socket = new Windows.Networking.Sockets.DatagramSocket(); Windows.Networking.HostName clientName = new Windows.Networking.HostName("127.0.0.1"); string clientPort = "10001"; Stream streamOut = (await socket.GetOutputStreamAsync(clientName, clientPort)).AsStreamForWrite(); StreamWriter writer = new StreamWriter(streamOut); await writer.WriteLineAsync("beat"); await writer.FlushAsync(); var message = $"Value at {DateTime.Now:hh:mm:ss.FFF}: {newValue}"; await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => CharacteristicLatestValue.Text = message); }