cscの作法 その447

# 概要
cscの作法、調べてみた。
udpやってみた。

# 参考にしたページ

https://qiita.com/inf102/items/8de08e0bdf83a09bbe2b

# 写真

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/18104/9b365925-f885-e46a-79bc-9efc0298a61d.png)


# サンプルコード

```

using System;
using System.Net;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;
using System.Net.Sockets;
using System.Threading;
using System.Linq;
using System.Text;

namespace App {
	public partial class MainWindow: Form {
		string SENDIpAddr;
		int SendPort;
		int RcvPort;
		UdpClient SendUdp = new UdpClient();
		TextBox TEXT1;
		TextBox IPADDR;
		TextBox SENDPORT;
		TextBox RCVPORT;
		ListBox LIST1;
		public MainWindow() {
			Size = new Size(400, 500);

			LIST1 = new ListBox();
			LIST1.Location = new Point(30, 20);
			LIST1.Size = new Size(200, 200);
			LIST1.TabIndex = 3;
			Controls.Add(LIST1);

			IPADDR = new TextBox();
			IPADDR.Location = new Point(30, 240);
			IPADDR.Text = "localhost";
			IPADDR.Size = new Size(50, 20);
			Controls.Add(IPADDR);

			SENDPORT = new TextBox();
			SENDPORT.Location = new Point(30, 280);
			SENDPORT.Text = "6000";
			SENDPORT.Size = new Size(50, 20);
			Controls.Add(SENDPORT);

			RCVPORT = new TextBox();
			RCVPORT.Location = new Point(30, 320);
			RCVPORT.Text = "6000";
			RCVPORT.Size = new Size(50, 20);
			Controls.Add(RCVPORT);

			Button button1 = new Button();
			button1.Location = new Point(30, 360);
			button1.Text = "set";
			button1.Click += new EventHandler(button1_Click);
			this.Controls.Add(button1);

			TEXT1 = new TextBox();
			TEXT1.Location = new Point(30, 400);
			TEXT1.Text = "test desu";
			TEXT1.Size = new Size(200, 20);
			Controls.Add(TEXT1);

			Button button0 = new Button();
			button0.Location = new Point(250, 400);
			button0.Text = "send";
			button0.Click += new EventHandler(button0_Click);
			this.Controls.Add(button0);

		}
		public async void Rcv() {
			IPEndPoint localEP = new IPEndPoint(IPAddress.Any, RcvPort);
			UdpClient UDP = new UdpClient(localEP);
			IPEndPoint remoteEP = null;
			await Task.Run(() => {
				while (true)
				{
					byte[] rcvBytes = UDP.Receive(ref remoteEP);
					string rcvMsg = Encoding.UTF8.GetString(rcvBytes);
					
					Invoke((Action) (() => {
						LIST1.Items.Add("受信:" + rcvMsg);
					}));
				}
			});
		}
		public async void SendMsg() {
			if (TEXT1.Text == "") 
				return;
			byte[] sendBytes = Encoding.UTF8.GetBytes(TEXT1.Text);
			await SendUdp.SendAsync(sendBytes, sendBytes.Length, SENDIpAddr, SendPort);
			LIST1.Items.Add ("送信:" + TEXT1.Text);
			TEXT1.Text = "";
			TEXT1.Focus();
		}
		private void TEXT1_KeyDown(object sender, KeyEventArgs e) {
			if (e.KeyData == Keys.Return)
			{
				SendMsg();
			}
		}
		private void button0_Click(object sender, EventArgs e) {
			SendMsg();
		}
		private void button1_Click(object sender, EventArgs e) {
			SENDIpAddr = IPADDR.Text;
			SendPort = int.Parse(SENDPORT.Text);
			RcvPort = int.Parse(RCVPORT.Text);
			Text = "[UDP SendAddr " + SENDIpAddr + "] [SendPort " + SendPort + "] [RecvPort " + RcvPort + "]";
			LIST1.Items.Add("設定完了");
			Rcv();
		}
		[STAThread]
		public static void Main() {
			Application.Run(new MainWindow());
		}
	}
}





```

以上。