public void SendServerMessage(int cmd, string message) { CMD_BASE_MESSAGE rep = new CMD_BASE_MESSAGE(); rep.Cmd = cmd; rep.Message = message; MemoryStream ms = MessageTransformation.Serialize <CMD_BASE_MESSAGE>(rep); byte[] entityData = ms.ToArray(); byte[] messagedate = MessageCoder.MessageEncoding(entityData); _socket.Send(messagedate, messagedate.Length, 0); }
public void SendTestMessage() { CMD_BASE_MESSAGE rep = new CMD_BASE_MESSAGE(); rep.Cmd = 11; rep.Message = "bsadsdfasf"; MemoryStream ms = MessageTransformation.Serialize <CMD_BASE_MESSAGE>(rep); byte[] entityData = ms.ToArray(); byte[] messagedate = MessageCoder.MessageEncoding(entityData); _socket.Send(messagedate, messagedate.Length, 0); }
static void CreatOneConnect() { try { int port = 2012; string host = "127.0.0.1"; //string host = "134.175.17.67"; IPAddress ip = IPAddress.Parse(host); IPEndPoint ipe = new IPEndPoint(ip, port); //把ip和端口转化为IPEndPoint实例 Socket c = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //创建一个Socket Console.WriteLine("Conneting..."); c.Connect(ipe); //连接到服务器 CMD_BASE_MESSAGE rep = new CMD_BASE_MESSAGE(); rep.Cmd = 0x10000001; rep.Message = "bsadsdfasf"; MemoryStream ms = MessageTransformation.Serialize <CMD_BASE_MESSAGE>(rep); byte[] entityData = ms.ToArray(); byte[] messagedate = MessageCoder.MessageEncoding(entityData); c.Send(messagedate, messagedate.Length, 0); c.Send(messagedate, messagedate.Length, 0); c.Send(messagedate, messagedate.Length, 0); c.Send(messagedate, messagedate.Length, 0); c.Send(messagedate, messagedate.Length, 0); c.Send(messagedate, messagedate.Length, 0); //Console.Write("断开连接"); c.Close(); } catch (ArgumentNullException e) { Console.WriteLine("ArgumentNullException: {0}", e); } catch (SocketException e) { Console.WriteLine("SocketException: {0}", e); } }
private void PushLocalBuffer(byte[] buffer, int offset, int count) { if ((_ReceiveOffset + count) > _ReceiveBuffer.Length) { throw new Exception("客户端信息数据超出当前接收缓存大小!"); } Array.Copy(buffer, offset, _ReceiveBuffer, _ReceiveOffset, count); _ReceiveOffset += count; //Console.WriteLine("添加数据缓存 当前缓存长度:{0}", _ReceiveOffset); List <byte[]> messagebuffers = MessageCoder.MessageDecoding(ref _ReceiveBuffer, ref _ReceiveOffset); foreach (var mm in messagebuffers) { MemoryStream ms = new MemoryStream(mm, 0, mm.Length); CMD_BASE_MESSAGE mss2 = MessageTransformation.Deserialize <CMD_BASE_MESSAGE>(ms); Console.WriteLine("AI:{0}", mss2.Message); } }