-
-
Save dj1711572002/44f65dbe2ba498a281464997ccded312 to your computer and use it in GitHub Desktop.
C# UDP Recive Monitor Binary to HEX text
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
using System; | |
using static System.Net.Mime.MediaTypeNames; | |
public class UdpReceiver | |
{ | |
static void Main() | |
{ | |
//バインドするローカルIPとポート番号 | |
string localIpString = "192.168.0.109"; | |
System.Net.IPAddress localAddress = | |
System.Net.IPAddress.Parse(localIpString); | |
int localPort = 10000; | |
int n=0; | |
//UdpClientを作成し、ローカルエンドポイントにバインドする | |
System.Net.IPEndPoint localEP = | |
new System.Net.IPEndPoint(localAddress, localPort); | |
System.Net.Sockets.UdpClient udp = | |
new System.Net.Sockets.UdpClient(localEP); | |
for (; ; ) | |
{ | |
//データを受信する | |
System.Net.IPEndPoint remoteEP = null; | |
byte[] rcvBytes = udp.Receive(ref remoteEP); | |
for (int i = 0; i < rcvBytes.Length; i++) | |
{ | |
string hexdata = rcvBytes[i].ToString("X"); | |
Console.Write(hexdata); | |
} | |
//データを文字列に変換する | |
string rcvMsg = System.Text.Encoding.ASCII.GetString(rcvBytes); | |
//受信したデータと送信者の情報を表示する | |
//Console.WriteLine("受信したデータ:{0}", rcvMsg); | |
Console.WriteLine(); | |
Console.WriteLine(n.ToString()+":送信元アドレス:{0}/ポート番号:{1}", | |
remoteEP.Address, remoteEP.Port); | |
//"exit"を受信したら終了 | |
//ConsoleKeyInfo input = Console.ReadKey(); | |
// Console.WriteLine("押されたキーは" + input.Key.ToString()); | |
if (Console.KeyAvailable == true) | |
{ | |
break; | |
} | |
n++; | |
} | |
//UdpClientを閉じる | |
udp.Close(); | |
Console.WriteLine("終了しました。"); | |
Console.ReadLine(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment