Skip to content

Instantly share code, notes, and snippets.

@dj1711572002
Created January 1, 2024 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dj1711572002/7ad1588f44521920fe436f978c96f36c to your computer and use it in GitHub Desktop.
Save dj1711572002/7ad1588f44521920fe436f978c96f36c to your computer and use it in GitHub Desktop.
C# UDP recieve test pgm
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
namespace UDP_test00
{
public partial class Form1 : Form
{
int n, i;
public Form1()
{
InitializeComponent();
richTextBox1.AppendText("INIT");
}
void udprRcv()
{
//バインドするローカルIPとポート番号
string localIpString = "192.168.0.109";
System.Net.IPAddress localAddress =System.Net.IPAddress.Parse(localIpString);
int localPort = 10000;
DateTime nowt = DateTime.Now;
//string fn = "C:\RTK_LOG\udp_" + nowt.ToString("HH:mm:ss")+".bin"; ;
System.IO.FileStream fs = new FileStream(@"C:\RTK_LOG\udp.bin", FileMode.Create, FileAccess.ReadWrite);
//UdpClientを作成し、ローカルエンドポイントにバインドする
System.Net.IPEndPoint localEP =
new System.Net.IPEndPoint(localAddress, localPort);
System.Net.Sockets.UdpClient udp =
new System.Net.Sockets.UdpClient(localEP);
richTextBox1.AppendText("START");
for (; ; )
{
//データを受信する
System.Net.IPEndPoint remoteEP = null;
byte[] rcvBytes = udp.Receive(ref remoteEP);
// byte[] bufw = new byte[10] { 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9 };
fs.Write(rcvBytes, 0, 512); // ファイルに10バイト書き込む。
// 書き込むデータはbufwのインデックス0~9に格納されているデータ。
n++;
DateTime now = DateTime.Now;
//Console.WriteLine(now.Millisecond + "ミリ秒");
//データを文字列に変換する
// string rcvMsg = System.Text.Encoding.UTF8.GetString(rcvBytes);
//for (int i = 0; i < rcvBytes.Length; i++) {
richTextBox1.AppendText("n=" + n.ToString() + "datanum=" + rcvBytes.Length.ToString() + "time=" + now.Millisecond+"\r");
Debug.Print("n=" + n.ToString() + "datanum=" + rcvBytes.Length.ToString() + "time=" +now.ToString("mm:ss.") + now.Millisecond + "\r");
// }
//richTextBox1.AppendText("送信元アドレス:" + remoteEP.Address.ToString()+"PortNo="+remoteEP.Port.ToString());
//remoteEP.Address, remoteEP.Port);
//受信したデータと送信者の情報を表示する
//Console.WriteLine("受信したデータ:{0}", rcvMsg);
//Console.WriteLine("送信元アドレス:{0}/ポート番号:{1}",
//remoteEP.Address, remoteEP.Port);
//"exit"を受信したら終了
if (textBox1.Text=="exit")
{
break;
}
}
//UdpClientを閉じる
udp.Close();
textBox1.Text="終了しました。";
}
private void button1_Click(object sender, EventArgs e)
{
richTextBox1.AppendText("Button1");
udprRcv();
}
private void button2_Click(object sender, EventArgs e)
{
textBox1.Text = "exit";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment