服务端
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Sockets;
using System.Net;
namespace SocketMultiplayerGameServer
{
internal class Program
{
private static Socket socket; // 定义一个 Socket 对象
private static byte[] buffer = new byte[1024]; // 定义一个大小为 1024 的字节数组作为缓冲区
static void Main(string[] args) // 主函数
{
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); // 创建一个新的 Socket 对象,指定地址族为 InterNetwork,套接字类型为 Stream,协议类型为 TCP
socket.Bind(new IPEndPoint(IPAddress.Any, 6666)); // 将 Socket 绑定到指定的 IP 地址和端口号,IPAddress.Any 表示任意可用的 IP 地址
socket.Listen(0); // 开始监听传入的连接请求,并设置最大连接数量为 0(表示无限制,可以同时处理多个连接