在这里插入代码片
```using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
/// <summary>
/// 服务器网络管理
/// </summary>
class NetManager : Singleton<NetManager>
{
Socket serverSocket = null;
public List<Client> clientList;
public void Start()
{
clientList = new List<Client>();
serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
serverSocket.Bind(new IPEndPoint(IPAddress.Any, 10010));
serverSocket.Listen(2);
Console.WriteLine("Server is open");
serverSocket.BeginAccept(AsyEndAccept, null);
}
/// <summary>
/// 客户端连接到服务器
/// </summary>
/// <param name="ar"></param>
private void AsyEndAccept(IAsyncResult ar)
{
Socket cSocket = serverSocket.EndAccept(ar);
IPEndPoint ip = cSocket.RemoteEndPoint as IPEndPoint;
Console.WriteLine($"Client{ip.Address}/{ip.Port}Enter!!!!!");
Client client = new Client();
client.clientSocket = cSocket;
///开始接收数据
client
C#网络通信基础篇——服务器端(含粘包)
最新推荐文章于 2023-08-23 22:33:42 发布