Created
May 30, 2014 11:54
-
-
Save ochilab/93d08e5a0147ba61d26e to your computer and use it in GitHub Desktop.
C#でのUDP通信サンプル
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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