[编程语言][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}\"");
		}
	}
}
相关推荐
canyuemanyue几秒前
go语言连续监控事件并回调处理
开发语言·后端·golang
杜杜的man3 分钟前
【go从零单排】go语言中的指针
开发语言·后端·golang
萧鼎1 小时前
Python并发编程库:Asyncio的异步编程实战
开发语言·数据库·python·异步
学地理的小胖砸1 小时前
【一些关于Python的信息和帮助】
开发语言·python
疯一样的码农1 小时前
Python 继承、多态、封装、抽象
开发语言·python
^velpro^1 小时前
数据库连接池的创建
java·开发语言·数据库
秋の花1 小时前
【JAVA基础】Java集合基础
java·开发语言·windows
小松学前端2 小时前
第六章 7.0 LinkList
java·开发语言·网络
可峰科技2 小时前
斗破QT编程入门系列之二:认识Qt:编写一个HelloWorld程序(四星斗师)
开发语言·qt
全栈开发圈2 小时前
新书速览|Java网络爬虫精解与实践
java·开发语言·爬虫