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の作法 その557

Last updated at Posted at 2025-02-11

概要

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

練習問題

Tcp Serverとclientをドッキングせよ。

写真

image.png

サンプルコード


using System;
using System.Data;
using System.Linq;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using System.Drawing;
using System.Runtime.InteropServices;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;

namespace Dock
{
	public partial class Win1: WeifenLuo.WinFormsUI.Docking.DockContent {
		public TextBox bo1;
		public Win1() {
			bo1 = new TextBox();
			bo1.Text = "";
			bo1.Multiline = true;
			bo1.Font = new Font("MS Pゴシック", 12);
			bo1.Dock = DockStyle.Fill;
			Controls.AddRange(new Control[] {
				bo1
			});
		}
	}
	public partial class Win2: WeifenLuo.WinFormsUI.Docking.DockContent {
		public string msg = "";
		public Win2() {
			Button btn0 = new Button();
			btn0.Location = new Point(50, 50);
			btn0.Text = "send";
			btn0.Click += btn0_Click;
			Controls.AddRange(new Control[] {
				btn0
			});
		}
		void btn0_Click(object sender, System.EventArgs e) {
			string sendMsg = "send ore";
			string ipOrHost = "127.0.0.1";
			int port = 49152;
			System.Net.Sockets.TcpClient tcp = new System.Net.Sockets.TcpClient(ipOrHost, port);
			Console.WriteLine("cliant: サーバー({0}:{1})と接続しました({2}:{3})。", ((System.Net.IPEndPoint) tcp.Client.RemoteEndPoint).Address,
				((System.Net.IPEndPoint) tcp.Client.RemoteEndPoint).Port, ((System.Net.IPEndPoint) tcp.Client.LocalEndPoint).Address,
				((System.Net.IPEndPoint) tcp.Client.LocalEndPoint).Port);
			msg += "サーバーと接続しました" + Environment.NewLine;
			System.Net.Sockets.NetworkStream ns = tcp.GetStream();
			ns.ReadTimeout = 10000;
			ns.WriteTimeout = 10000;
			System.Text.Encoding enc = System.Text.Encoding.UTF8;
			byte[] sendBytes = enc.GetBytes(sendMsg + '\n');
			ns.Write(sendBytes, 0, sendBytes.Length);
			msg += "cliant: 送信しました。" + Environment.NewLine;
			ns.Close();
			tcp.Close();
			msg += "cliant: 切断しました。" + Environment.NewLine;
		}
	}
	public class DataReceivedEventArgs: EventArgs {
		public string Message;
	}
	public class ClassTcpServer
	{
		public string ipaddress {
			get;
			set;
		}
		public int port {
			get;
			set;
		}
		public int timerinterval {
			get;
	 		set;
		}
		private System.Net.Sockets.TcpListener listener;
		private System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
		public delegate void DataReceivedEventHandler(object sender, DataReceivedEventArgs e);
		public event DataReceivedEventHandler DataReceived;
		public ClassTcpServer() {
		}
		public void Start() {
			listener = new System.Net.Sockets.TcpListener(System.Net.IPAddress.Parse(ipaddress), port);
			listener.Start();
			timer.Interval = timerinterval;
			timer.Tick += new EventHandler(timer_Tick);
			timer.Start();
		}
		public void Stop() {
			timer.Stop();
			if (listener != null)
			{
				listener.Stop();
			}
			listener = null;
		}
		private void timer_Tick(object sender, EventArgs e) {
			//Console.WriteLine("server: mate");
			if (!listener.Pending())
			{
				return;
			}
			Console.WriteLine("server: kita");
			System.Net.Sockets.TcpClient client = listener.AcceptTcpClient();
			System.Net.Sockets.NetworkStream ns = client.GetStream();
			ns.ReadTimeout = 10000;
			ns.WriteTimeout = 10000;
			System.Text.Encoding enc = System.Text.Encoding.UTF8;
			bool disconnected = false;
			System.IO.MemoryStream ms = new System.IO.MemoryStream();
			byte[] resBytes = new byte[256];
			int resSize = 0;
			do
			{
				resSize = ns.Read(resBytes, 0, resBytes.Length);
				if (resSize == 0)
				{
					disconnected = true;
					break;
				}
				ms.Write(resBytes, 0, resSize);
			} while (ns.DataAvailable || resBytes[resSize - 1] != '\n');
			string resMsg = enc.GetString(ms.GetBuffer(), 0, (int) ms.Length);
			ms.Close();
			resMsg = resMsg.TrimEnd('\n');
			Console.WriteLine("server: " + resMsg);
			DataReceivedEventArgs ee = new DataReceivedEventArgs();
			ee.Message = resMsg;
			DataReceived(this, ee);
			if (!disconnected)
			{
				string sendMsg = resMsg.Length.ToString();
				byte[] sendBytes = enc.GetBytes(sendMsg + '\n');
				ns.Write(sendBytes, 0, sendBytes.Length);
				Console.WriteLine("server: send ok");
			}
			ns.Close();
			client.Close();
			Console.WriteLine("server: close");
		}
	}
	public partial class Win3: WeifenLuo.WinFormsUI.Docking.DockContent {
		public string msg = "";
		public Win3() {
			Button btn1 = new Button();
			btn1.Location = new Point(50, 50);
			btn1.Text = "start";
			btn1.Click += btn1_Click;
			Controls.AddRange(new Control[] {
				btn1
			});
		}
		void btn1_Click(object sender, System.EventArgs e) {
			ClassTcpServer classTcpServer = new ClassTcpServer();
			classTcpServer.ipaddress = "0.0.0.0";
			classTcpServer.port = 49152;
			classTcpServer.timerinterval = 100;
			classTcpServer.DataReceived += new ClassTcpServer.DataReceivedEventHandler(TcpServer_DataReceived);
			classTcpServer.Start();
			msg += "server: start" + Environment.NewLine;
		}
		private void TcpServer_DataReceived(object sender, DataReceivedEventArgs e) {
			msg += "server: recv" + Environment.NewLine;
			msg += e.Message + Environment.NewLine;
		}
	}
	public partial class Form1: Form {
		public Win3 left1;
		public Win2 right1;
		public Win1 bottom1;
		public Win1 center1;
		public Form1() {
			ClientSize = new Size(800, 600);
			Text = "dock";
			var dockPanel1 = new DockPanel();
			dockPanel1.ShowDocumentIcon = true;
			dockPanel1.Dock = DockStyle.Fill;
			dockPanel1.DocumentStyle = DocumentStyle.DockingWindow;
			Controls.Add(dockPanel1);
			left1 = new Win3();
			left1.TabText = "left1";
			left1.Show(dockPanel1, DockState.DockLeft);
			right1 = new Win2();
			right1.TabText = "right1";
			right1.Show(dockPanel1, DockState.DockRight);
			bottom1 = new Win1();
			bottom1.TabText = "bottom1";
			bottom1.Show(dockPanel1, DockState.DockBottom);
			center1 = new Win1();
			center1.TabText = "center1";
			center1.Show(dockPanel1, DockState.Document);
			dockPanel1.UpdateDockWindowZOrder(DockStyle.Left, true);
			Timer timer = new Timer {
				Interval = 200
			};
			timer.Tick += timer_Tick;
			timer.Start();
		}
		private void timer_Tick(object sender, EventArgs e) {
			//Console.WriteLine("timer");
			if (left1.msg != "")
			{
				bottom1.bo1.AppendText(left1.msg);
				left1.msg = "";
			}
			if (right1.msg != "")
			{
				bottom1.bo1.AppendText(right1.msg);
				right1.msg = "";
			}
		}
		[STAThread]
		public static void Main() {
			Application.Run(new Form1());
		}
	}
}






以上。

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?