客户端
public class NetManager : Singleton<NetManager>
{
Queue<byte[]> queue = new Queue<byte[]>();
byte[] data = new byte[1024];
Socket socket;
public void Init()
{
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.BeginConnect("127.0.0.1", 8888, OnConnect, null);
}
private void OnConnect(IAsyncResult ar)
{
try
{
socket.EndConnect(ar);
Debug.Log("连接成功");
socket.BeginReceive(data, 0, data.Length, SocketFlags.None, OnReceive, null);
}
catch (Exception ex)
{
Debug.Log("连接失败 ");
Debug.Log(ex);
}
}
private void OnReceive(IAsyncResult ar)
{
try
{
int len = socket.EndReceive(ar);
if (len > 0)
{
byte[] rdata = new byte[len];
Buffer.BlockCopy(data, 0, rdata, 0, len);
while (rdata.Length > 4)
{
int bodylen = BitConverter.ToInt32(rdata, 0);
byte[] bodydata = new byte[bodylen];
Buffer.BlockCopy(rdata, 4, bodydata, 0, bodylen);
queue.Enqueue(bodydata);
int sylen = rdata.Length - 4 - bodylen;
byte[] sydata = new byte[sylen];
Buffer.BlockCopy(rdata, 4 + bodylen, sydata, 0, sylen);
rdata = sydata;
}
socket.BeginReceive(data, 0, data.Length, SocketFlags.None, OnReceive, null);
}
}
catch (Exception ex)
{
Debug.Log(ex);
}
}
public void OnSend(int id, byte[] data)
{
int len = data.Length + 4;
byte[] arr = new byte[0];
arr = arr.Concat(BitConverter.GetBytes(len)).Concat(BitConverter.GetBytes(id)).Concat(data).ToArray();
socket.BeginSend(arr, 0, arr.Length, SocketFlags.None, Send, null);
}
private void Send(IAsyncResult ar)
{
int len= socket.EndSend(ar);
Debug.Log(len);
}
// Update is called once per frame
public void OnUpdate()
{
if (queue.Count > 0)
{
byte[] arr = queue.Dequeue();
int msgid = BitConverter.ToInt32(arr, 0);
byte[] info = new byte[arr.Length - 4];
Buffer.BlockCopy(arr, 4, info, 0, arr.Length - 4);
MessageManager<byte[]>.Instance.OnBoratListener(msgid, info);
}
}
服务器
internal class NetManager : Singleton<NetManager>
{
Socket socket;
public List<Client> allcli = new List<Client>();
public void Init()
{
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Bind(new IPEndPoint(IPAddress.Any, 8888));
socket.Listen(1000);
Console.WriteLine("服务器已开启");
socket.BeginAccept(OnAccept, null);
}
private void OnAccept(IAsyncResult ar)
{
try
{
Socket socket_cli = socket.EndAccept(ar);
Client client = new Client();
Console.WriteLine("连接成功");
client.socket = socket_cli;
allcli.Add(client);
socket.BeginAccept(OnAccept, null);
socket_cli.BeginReceive(client.data, 0, client.data.Length, SocketFlags.None, OnReceive, client);
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
private void OnReceive(IAsyncResult ar)
{
try
{
Client client = ar.AsyncState as Client;
int len = client.socket.EndReceive(ar);
if (len > 0)
{
byte[] rdata = new byte[len];
Buffer.BlockCopy(client.data, 0, rdata, 0, len);
while (rdata.Length > 4)
{
int bodylen = BitConverter.ToInt32(rdata, 0);
byte[] bodydata = new byte[bodylen];
Buffer.BlockCopy(rdata, 4, bodydata, 0, bodylen);
int msgid = BitConverter.ToInt32(bodydata, 0);
byte[] info = new byte[bodydata.Length - 4];
Buffer.BlockCopy(bodydata, 4, info, 0, bodydata.Length - 4);
MesData msg = new MesData();
msg.client = client;
msg.data = info;
MessageManager<MesData>.Instance.OnBoratListener(msgid, msg);
int sylen = rdata.Length - 4 - bodylen;
byte[] sydata = new byte[sylen];
Buffer.BlockCopy(rdata, 4 + bodylen, sydata, 0, sylen);
rdata = sydata;
}
client.socket.BeginReceive(client.data, 0, client.data.Length, SocketFlags.None, OnReceive, client);
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
public void OnSend(int id, byte[] data, Client cli)
{
int len = data.Length + 4;
byte[] arr = new byte[0];
arr = arr.Concat(BitConverter.GetBytes(len)).Concat(BitConverter.GetBytes(id)).Concat(data).ToArray();
cli.socket.BeginSend(arr, 0, arr.Length, SocketFlags.None, Send, cli);
}
private void Send(IAsyncResult ar)
{
try
{
Client cli = ar.AsyncState as Client;
cli.socket.EndSend(ar);
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
}
}