[编程语言][C#]TcpListener与TcpClient
创始人
2024-11-15 04:33:04
0

TcpListener

program.cs

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

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 _Clients;  		public EchoServerListener(int port) 		{ 			_IPEndPoint = new IPEndPoint(IPAddress.Any, port); 			_Listener = new TcpListener(_IPEndPoint); 			_Clients = new List(); 		}  		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

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}\""); 		} 	} }  

相关内容

热门资讯

微信炸金花购买房卡/微信斗牛如... 炸金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:8488009许多玩家在游戏中会购买房卡来享受...
炸金花微信群购买房卡/牛牛链接... 炸金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:33903369许多玩家在游戏中会购买房卡来享...
拼三张从哪里买房卡/新海贝大厅... 拼三张是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:55051770许多玩家在游戏中会购买房卡来享...
微信炸金花房卡一张多少钱/微信... 炸金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:8488009许多玩家在游戏中会购买房卡来享受...
微信链接炸金花房卡在哪买的/微... 炸金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:33903369许多玩家在游戏中会购买房卡来享...
微信群链接炸金花房卡/微信里斗... 炸金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:55051770许多玩家在游戏中会购买房卡来享...
怎么买炸金花房间链接房卡/微信... 炸金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:8488009许多玩家在游戏中会购买房卡来享受...
微信玩链接牛牛房卡/新人皇大厅... 斗牛是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:33903369许多玩家在游戏中会购买房卡来享受...
拼三张房卡链接去哪里买/橘子大... 拼三张是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:55051770许多玩家在游戏中会购买房卡来享...
微信玩炸金花怎么买房卡/欢乐游... 炸金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:8488009许多玩家在游戏中会购买房卡来享受...
炸金花房卡链接在哪买的/狂飙大... 炸金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:33903369许多玩家在游戏中会购买房卡来享...
如何创建牛牛房间卡/牛至尊大厅... 牛牛是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:55051770许多玩家在游戏中会购买房卡来享受...
微信里上玩拼三张购买房卡/神牛... 拼三张是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:8488009许多玩家在游戏中会购买房卡来享受...
微信里面斗牛链接房卡/九酷大厅... 斗牛是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:33903369许多玩家在游戏中会购买房卡来享受...
炸金花如何开好友房间房卡/微信... 炸金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:8488009许多玩家在游戏中会购买房卡来享受...
微信炸金花在哪里充值房卡/新天... 炸金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:33903369许多玩家在游戏中会购买房卡来享...
微信里面拼三张房卡哪里买/新皇... 拼三张是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:55051770许多玩家在游戏中会购买房卡来享...
微信群开牛牛房卡/新天地大厅牛... 牛牛是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:8488009许多玩家在游戏中会购买房卡来享受更...
微信打炸金花链接房卡怎么买/怎... 炸金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:33903369许多玩家在游戏中会购买房卡来享...
微信玩炸金花房卡怎么买/开牛牛... 炸金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:55051770许多玩家在游戏中会购买房卡来享...