public void start() { Cryptor crypt = Cryptor.getInstance(); crypt.setAESKey("SocketRouterSvr"); m_socket.init("127.0.0.1", 8300); m_socket.setRecvCallback(recv); m_socket.connect(); m_socket.loop(); /* * { * //测试登录 * ReqLogin pack = new ReqLogin(connid, "test3003", "123456"); * byte[] buf = pack.pack(); * clientSocket.Send(buf); * * int receiveLength = clientSocket.Receive(result); * ushort len = System.BitConverter.ToUInt16(result, 0); * byte[] packbuf = new byte[len]; * Array.Copy(result, 2, packbuf, 0, len); * string decstr = ""; * crypt.decryptAES(packbuf, out decstr); * * RespLogin resp = new RespLogin(); * resp.make(UTF8Encoding.UTF8.GetBytes(decstr)); * Console.WriteLine("登录回复:flag=" + resp.flag + ",numid=" + resp.numid); * } */ Console.ReadKey(); }
public bool init(string ip, int port) { m_ip = IPAddress.Parse(ip); m_port = port; m_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); m_crypt = Cryptor.getInstance(); return(true); }
/// <summary> /// class >> buf /// 打包结束 /// </summary> /// <returns></returns> public byte[] packEnd() { this.buf = this.byteSource.ToArray(); replaceHandLen(); //加密 Cryptor crypt = Cryptor.getInstance(); string crystr = ""; crypt.encryptAES(this.buf, out crystr); byte[] len = getFromUShort((ushort)crystr.Length); byte[] crtbuf = UTF8Encoding.UTF8.GetBytes(crystr); byte[] endbuf = new byte[crtbuf.Length + 2]; endbuf[0] = len[0]; endbuf[1] = len[1]; Array.Copy(crtbuf, 0, endbuf, 2, crtbuf.Length); this.buf = endbuf; return(this.buf); }