[编程语言][C#]TcpListener与TcpClient

TcpListener

program.cs

csharp 复制代码
using System.Net;
using System.Net.Sockets;
using System.Text;

namespace TCPListener
{
	internal class Program
	{
		static async Task Main(string[] args)
		{
			EchoServerListener listener = new EchoServerListener(28888);
			listener.Start();

			while (true)
			{
			}
		}
	}
}

TCPListener.cs

csharp 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace TCPListener
{
	class Client
	{
		public TcpClient TcpClient { get; private set; }
		public NetworkStream Stream { get; private set; }
		public int BufferSize { get; private set; }
		public byte[] Buffer { get; private set; }

		public Client(TcpClient tcpClient, NetworkStream stream, int bufferSize = 1024)
		{
			TcpClient = tcpClient;
			Stream = stream;
			BufferSize = bufferSize;
			Buffer = new byte[BufferSize];
		}
	}

	internal class EchoServerListener
	{
		private readonly IPEndPoint _IPEndPoint;
		private readonly TcpListener _Listener;
		private readonly List<Client> _Clients;

		public EchoServerListener(int port)
		{
			_IPEndPoint = new IPEndPoint(IPAddress.Any, port);
			_Listener = new TcpListener(_IPEndPoint);
			_Clients = new List<Client>();
		}

		public async void Start()
		{
			try
			{
				_Listener.Start();

				try
				{
					while (true)
					{
						using TcpClient tcpClient = await _Listener.AcceptTcpClientAsync();
						await using NetworkStream stream = tcpClient.GetStream();
						Client client = new Client(tcpClient, stream);
						_Clients.Add(client);

						string message = "Who are you?";
						Encoding.UTF8.GetBytes(message, 0, message.Length, client.Buffer, 0);
						await stream.WriteAsync(client.Buffer, 0, message.Length);

						int received = await stream.ReadAsync(client.Buffer);
						message = Encoding.UTF8.GetString(client.Buffer, 0, received);

						message = $"Hello, {message}!";
						Encoding.UTF8.GetBytes(message, 0, message.Length, client.Buffer, 0);
						await stream.WriteAsync(client.Buffer, 0, message.Length);
					}
				}
				catch
				{

				}
			}
			finally
			{
				_Listener.Stop();
			}
		}

		public void Stop()
		{
			_Listener.Stop();
		}
	}
}

TcpClient

Program.cs

csharp 复制代码
using System.Net;
using System.Net.Sockets;
using System.Text;

namespace TCPClient
{
	internal class Program
	{
		static async Task Main(string[] args)
		{
			using TcpClient tcpClient = new TcpClient("localhost", 28888);
			await using NetworkStream stream = tcpClient.GetStream();

			byte[] buffer = new byte[1024];
			int received = await stream.ReadAsync(buffer);
			string message = Encoding.UTF8.GetString(buffer, 0, received);
			Console.WriteLine($"[Server] \"{message}\"");

			message = Console.ReadLine();
			byte[] bytes = Encoding.UTF8.GetBytes(message);
			await stream.WriteAsync(bytes);

			received = await stream.ReadAsync(buffer);
			message = Encoding.UTF8.GetString(buffer, 0, received);
			Console.WriteLine($"[Server] \"{message}\"");
		}
	}
}
相关推荐
E***q5392 分钟前
C++内存对齐优化
开发语言·c++
q***d17324 分钟前
Kotlin在后台服务中的框架
android·开发语言·kotlin
周杰伦fans31 分钟前
C# 中的 `Hashtable`
开发语言·c#
习习.y36 分钟前
关于python中的面向对象
开发语言·python
lingggggaaaa36 分钟前
免杀对抗——C2远控篇&PowerShell&有无文件落地&C#参数调用&绕AMSI&ETW&去混淆特征
c语言·开发语言·笔记·学习·安全·microsoft·c#
技术净胜37 分钟前
MATLAB 基因表达数据处理与可视化全流程案例
开发语言·matlab
友友马37 分钟前
『Qt』多元素控件
开发语言·qt
hmbbcsm1 小时前
练习python题目小记(六)
开发语言·python
4***V2021 小时前
Vue3响应式原理详解
开发语言·javascript·ecmascript
q***98521 小时前
VS Code 中如何运行Java SpringBoot的项目
java·开发语言·spring boot