0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

cscの作法 その549

Last updated at Posted at 2025-01-31

概要

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

練習問題

IPv6を叩け。

環境

windows11

参考にしたページ

サンプルコード

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

class Program {
	private static bool ListenFlag = true;
	static void Client(string ipAddr_v6, string ipAddr_v4, int port) {
		Console.WriteLine("IPv6 で送ります。");
		Socket socket = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
		socket.Connect(new IPEndPoint(IPAddress.Parse(ipAddr_v6), port));
		socket.Disconnect(false);
		socket.Dispose();
		Console.WriteLine("3秒ほどWaitします");
		System.Threading.Thread.Sleep(3000);
		Console.WriteLine("IPv4 で送ります。");
		socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
		socket.Connect(new IPEndPoint(IPAddress.Parse(ipAddr_v4), port));
		socket.Disconnect(false);
		socket.Dispose();
	}
	static void Server(int port) {
		Console.WriteLine("Start IPv6 Listen");
		Listen(IPAddress.IPv6Any, port);
		Console.WriteLine("Start IPv4 Listen");
		Listen(IPAddress.Any, port);
		while (ListenFlag)
		{
			System.Threading.Thread.Sleep(10000);
			Console.WriteLine("Listen now ...");
		}
		Console.WriteLine("End Listen");
	}
	private static void Listen(IPAddress ipaddr, int port) {
		TcpListener listen = new TcpListener(ipaddr, port);
		listen.Start();
		listen.BeginAcceptSocket(DoAcceptSocketCallback, listen);
	}
	private static void DoAcceptSocketCallback(IAsyncResult ar) {
		TcpListener listen = ar.AsyncState as TcpListener;
		Socket socket = listen.EndAcceptSocket(ar);
		Console.WriteLine("Connect Success.");
		if (socket.AddressFamily == AddressFamily.InterNetworkV6)
		{
			Console.WriteLine("IPv6");
			Console.WriteLine("送信元IP");
			Console.WriteLine(socket.LocalEndPoint.ToString());
		}
		else if (socket.AddressFamily == AddressFamily.InterNetwork)
		{
			Console.WriteLine("IPv4");
			Console.WriteLine("送信元IP");
			Console.WriteLine(socket.LocalEndPoint.ToString());
		}
		listen.BeginAcceptSocket(DoAcceptSocketCallback, listen);
	}
	static void Main(string[] args) {
		string ipaddr_v6 = "fe80::*:*:*:cfa6%16";
		string ipaddr_v4 = "192.168.0.104";
		Console.WriteLine("0	:Client");
		Console.WriteLine("other:Server");
		int num = 0;
		try
		{
			num = int.Parse(Console.ReadLine());
		}
		catch (Exception)
		{
			num = int.MaxValue;
		}
		switch (num)
		{
		case 0:
			Console.WriteLine("Start Client");
			Client(ipaddr_v6, ipaddr_v4, 9999);
			Console.WriteLine("End Client");
		break;
		default:
			Console.WriteLine("Start Server");
			Server(9999);
			Console.WriteLine("End Server");
		break;
		}
	}
}

実行結果

server


>v6
0       :Client
other:Server
1
Start Server
Start IPv6 Listen
Start IPv4 Listen
Listen now ...
Connect Success.
IPv6
送信元IP
[fe80::*:*:*:cfa6%16]:9999
Connect Success.
IPv4
送信元IP
192.168.0.104:9999
Listen now ...
Listen now ...

client

>v6
0       :Client
other:Server
0
Start Client
IPv6 で送ります。
3秒ほどWaitします
IPv4 で送ります。
End Client

以上。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?