unity学习(15)——服务器组装(1)

1.新建好的c#项目如下:

unity学习(15)——服务器组装(1)_第1张图片

文件夹中内容如下:

unity学习(15)——服务器组装(1)_第2张图片

此时已经可以通过vs2022打开.sln文件,就可以打开项目了

2.我们把逆向后(主程序)的内容的代码粘贴过去。有些逆向功底,很快可以定位到,服务器的入口函数ServerStart.start(10100);

其原始代码如下:

namespace GameServer.Hnalder
{
  internal class ServerStart
  {
    private static Socket server;

    public static void start(int port)
    {
      ServerStart.server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
      ServerStart.server.Bind((EndPoint) new IPEndPoint(IPAddress.Any, port));
      ServerStart.server.Listen(20);
      ServerStart.server.BeginAccept(new AsyncCallback(ServerStart.AcceptCallBack), (object) ServerStart.server);
    }

    public static void stop()
    {
      ServerStart.server.Dispose();
      ServerStart.server.Close();
    }

    private static void AcceptCallBack(IAsyncResult ar)
    {
      MyLog.form.textAdd("有客户端连接");
      try
      {
        Socket asyncState = (Socket) ar.AsyncState;
        Socket socket = asyncState.EndAccept(ar);
        Session session = new Session();
        session.socket = socket;
        LogicHandler.getInstance().sessionOpen(session);
        socket.BeginReceive(session.message, 0, session.message.Length, SocketFlags.None, new AsyncCallback(ServerStart.ReceiveCallBack), (object) session);
        asyncState.BeginAccept(new AsyncCallback(ServerStart.AcceptCallBack), (object) asyncState);
      }
      catch
      {
      }
    }

    private static void ReceiveCallBack(IAsyncResult ar)
    {
      Session asyncState = (Session) ar.AsyncState;
      try
      {
        int count = asyncState.socket.EndReceive(ar);
        if (count == 0)
        {
          MyLog.form.textAdd("有客户端断开连接");
          LogicHandler.getInstance().sessionClose(asyncState);
          return;
        }
        byte[] numArray = new byte[count];
        Buffer.BlockCopy((Array) asyncState.message, 0, (Array) numArray, 0, count);
        ServerStart.readMessage(asyncState, numArray);
      }
      catch (SocketException ex)
      {
        LogicHandler.getInstance().sessionClose(asyncState);
        asyncState.socket.Close();
        return;
      }
      asyncState.socket.BeginReceive(asyncState.message, 0, asyncState.message.Length, SocketFlags.None, new AsyncCallback(ServerStart.ReceiveCallBack), (object) asyncState);
    }

    public static void readMessage(Session session, byte[] bytes)
    {
      try
      {
        ByteArray byteArray = new ByteArray(bytes);
        int num1 = byteArray.ReadInt();
        int num2 = byteArray.ReadInt();
        int num3 = byteArray.ReadInt();
        int length = byteArray.ReadInt();
        string str = (string) null;
        if (length > 0)
          str = byteArray.ReadUTFBytes((uint) length);
        LogicHandler.getInstance().process(session, new SocketModel()
        {
          Type = num1,
          Area = num2,
          Command = num3,
          Message = str
        });
      }
      catch
      {
      }
    }
  }
}

 直接沾进去,一共16个问题

unity学习(15)——服务器组装(1)_第3张图片

你可能感兴趣的:(学习)