Skip to content

Instantly share code, notes, and snippets.

@ochilab
Created May 30, 2014 11:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ochilab/93d08e5a0147ba61d26e to your computer and use it in GitHub Desktop.
Save ochilab/93d08e5a0147ba61d26e to your computer and use it in GitHub Desktop.
C#でのUDP通信サンプル
System.Text.Encoding enc = System.Text.Encoding.UTF8;
//サーバ側
System.Net.Sockets.UdpClient udp = new System.Net.Sockets.UdpClient(11000);
byte[] sendBytes = enc.GetBytes(str);
////リモートホストを指定してデータを送信する
udp.Send(sendBytes, sendBytes.Length,"192.168.1.25", 11000);
//以下クライアント側
//ローカルポート番号localPortにバインドする
System.Net.Sockets.UdpClient udp = new System.Net.Sockets.UdpClient(11000);
byte[] rcvBytes = udp.Receive(ref remoteEP);
string rcvMsg = enc.GetString(rcvBytes);
Console.WriteLine("受信データ:{0}", rcvMsg);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment