NETCORE - Socket 通信
创建两个.net项目,一个做为服务端,一个做为客户端。
项目1:
NETCORE.SSocket.Server (.net6 控制台应用程序)
创建 socket 服务类: SocketServer.cs
using System; using System.Collections.Generic; using System.Linq; using System.Net.Sockets; using System.Net; using System.Text; using System.Threading.Tasks; namespace NETCORE.SSocket.Server { internal class SocketServer { static Socket tcpServer; public static void Init() { tcpServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//创建socket对象 //tcpServer.Bind(new IPEndPoint(IPAddress.Parse("172.21.0.16"),18200)); tcpServer.Bind(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 18200)); tcpServer.Listen(100); //Thread serverThread = new Thread(serverRead); Console.WriteLine("服务器已启动,等待连接......."); while (true) { Socket clientSocket = tcpServer.Accept(); var epoint = (clientSocket.RemoteEndPoint as IPEndPoint); Console.WriteLine(epoint.Address + ":" + epoint.Port + "已连接"); //serverThread.Start(); Task.Run(() => serverRead(clientSocket)); //_Client = new Client(clientSocket); } } private static void serverRead(Socket clientSocket) { string recvStr = ""; byte[] recvBytes = new byte[1024]; int bytes; do { bytes = clientSocket.Receive(recvBytes, recvBytes.Length, 0); //从客户端接受消息 recvStr += Encoding.ASCII.GetString(recvBytes, 0, bytes); } while (bytes == 1024); Console.WriteLine("{0} 收到:{1}", clientSocket.RemoteEndPoint.ToString(), recvStr); } } }
在 Program.cs 文件中引入
// See https://aka.ms/new-console-template for more information using NETCORE.SSocket.Server; Console.WriteLine("Hello, World!"); SocketServer.Init();
项目2:
NETCORE.SSocket.Client (.net6 控制台应用程序)
创建 socket 客户端类: SocketClient.cs
using System; using System.Collections.Generic; using System.Linq; using System.Net.Sockets; using System.Net; using System.Text; using System.Threading.Tasks; namespace NETCORE.SSocket.Client { internal class SocketClient { public static void Init() { TcpClient client = new TcpClient(); client.Client.Connect(IPAddress.Parse("127.0.0.1"), 18200); //Task.Run(() => ReadData(client)); Task.Run(() => ReadDataLoop(client)); client.Client.Send(Encoding.ASCII.GetBytes("{\"TID\":1111,\"blabla\":{}}")); } private static void ReadDataLoop(TcpClient client) { while (true) { if (!client.Connected) break; string xxx = ""; xxx = ReadData(client); Console.WriteLine(xxx); } } private static string ReadData(TcpClient client) { string retVal; byte[] data = new byte[1024]; NetworkStream stream = client.GetStream(); byte[] myReadBuffer = new byte[1024]; StringBuilder myCompleteMessage = new StringBuilder(); int numberOfBytesRead = 0; do { numberOfBytesRead = stream.Read(myReadBuffer, 0, myReadBuffer.Length); myCompleteMessage.AppendFormat("{0}", Encoding.ASCII.GetString(myReadBuffer, 0, numberOfBytesRead)); } while (stream.DataAvailable); retVal = myCompleteMessage.ToString(); return retVal; } } }
在 Program.cs 中引入:
// See https://aka.ms/new-console-template for more information using NETCORE.SSocket.Client; Console.WriteLine("Hello, World!"); SocketClient.Init();
先启动 服务项目,再启动客户端项目
标签:Socket,NETCORE,System,通信,client,new,using From: https://www.cnblogs.com/1285026182YUAN/p/16823308.html