服务器端:
Conn类
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Net;
using System.Net.Sockets;
public class Conn
{
public const int BUFFER_SIZE = 1024;
public Socket socket;
public byte[] readBuff = new byte[BUFFER_SIZE];
public int buffCount = 0; //处理分包和粘包
public bool isUse;
/// <summary>
/// 初始化
/// </summary>
/// <param name="Scok"></param>
public void Init(Socket Scok)
{
socket = Scok;
isUse = true;
buffCount = 0;
}
//返回剩余buff缓存
public int BuffRemain()
{
return BUFFER_SIZE - buffCount;
}
//客户端地址
public string GetAdress()
{
if (!isUse)
return "无法获取当前用户地址";
return socket.RemoteEndPoint.ToString();
}
public void Close()
{
if (!isUse)
return;
socket.Close();
isUse = false;
}
}
服务器类
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Net;
using System.Net.Sockets;
using System;
public class Serv
{
public Socket listenfd;
public Conn[] conns;
public int maxCon = 30;
public int NewIndex()
{
if (conns == null)
return -1;
for (int i =