LoginSignup
0
0

More than 1 year has passed since last update.

cscの作法 その295

Posted at

概要

cscの作法、調べてみた。
udpを叩いてみた。
練習問題やってみた。

練習問題

ブロードキャストで、helloを送信して、返事を受け取る。

サンプルコード

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

namespace ConsoleTest
{
	class Program {
		static void Main(string[] args) {
			UdpClient udp = new UdpClient(50000);
			udp.EnableBroadcast = true;
			while (true)
			{
				Byte[] sendBytes = Encoding.ASCII.GetBytes("hello");
				udp.Send(sendBytes, sendBytes.Length, new IPEndPoint(IPAddress.Broadcast, 49152));
				Console.WriteLine(Encoding.ASCII.GetString(sendBytes));
				IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, 0);
				Byte[] receiveBytes = udp.Receive(ref endPoint);
				string returnData = Encoding.ASCII.GetString(receiveBytes);
				Console.WriteLine("recv: " + returnData.ToString());
			}
		}
	}
}

以上。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0