public bool Send(int cmd, string message) { bool ret = false; if (_Socket == null) { LogHelper.Error("发送信息失败!连接已经断开"); return(ret); } 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); SendAsync(messagedate, 0, messagedate.Length); ret = true; return(ret); }
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); }
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 = 11; 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); } }