Skip to content

Instantly share code, notes, and snippets.

@knasa21
Last active March 23, 2023 08:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save knasa21/60086bd8239da91003f61d27c8727d7b to your computer and use it in GitHub Desktop.
Save knasa21/60086bd8239da91003f61d27c8727d7b to your computer and use it in GitHub Desktop.
テキストボックスの中身をUDPで送信するUWPアプリ
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Text;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Networking.Sockets;
using Windows.Storage.Streams;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
// 空白ページの項目テンプレートについては、https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x411 を参照してください
namespace UDPSender
{
/// <summary>
/// それ自体で使用できる空白ページまたはフレーム内に移動できる空白ページ。
/// </summary>
public sealed partial class MainPage : Page
{
DatagramSocket socket = new DatagramSocket();
string LOCAL_HOST = "127.0.0.1";
string REMOTE_HOST = "192.168.180.112";
int port = 3333;
public MainPage()
{
this.InitializeComponent();
}
private async Task SendMessage(string message, string ip, int port)
{
using (var stream = await socket.GetOutputStreamAsync(new Windows.Networking.HostName(ip), port.ToString())){
using (var writer = new DataWriter(stream)) {
var data = Encoding.UTF8.GetBytes(message);
writer.WriteBytes(data);
await writer.StoreAsync();
}
}
}
private async void Button_Click(object sender, RoutedEventArgs e)
{
Debug.WriteLine(InputText.Text);
await SendMessage(InputText.Text, LOCAL_HOST, port);
}
private async void Button_Click_1(object sender, RoutedEventArgs e)
{
Debug.WriteLine(InputText.Text);
await SendMessage(InputText.Text, REMOTE_HOST, port);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment