/// <summary> /// Send a message to an address via TCP /// </summary> /// <returns>Returns client-side success as bool</returns> public bool SendMsgTcp(string nMsg, MsgType nType = MsgType.MSG) { if (Ready) { if (Connected) { TcpSend = AssemblePacket(Encoding.ASCII.GetBytes(nMsg), nType); if (TcpSend != null && TcpSend.Length > 0) { TcpStream.Write(TcpSend, 0, TcpSend.Length); LocalConsole.Instance.Log("Sent " + nMsg + " (length:" + TcpSend.Length + ")", true); return(true); } else { LocalConsole.Instance.Log("Invalid message data to send"); return(false); } } else { LocalConsole.Instance.Log("Not currently connected to a server"); return(false); } } else { LocalConsole.Instance.Log("HubrisNet is not ready"); return(false); } }
private void Ping(object state) { if (!IsConnected) { return; } var requestData = new ServerPingRequest().GetRequestData(); TcpStream.Write(requestData, 0, requestData.Length); }
/*向服务器写入命令的方法*/ private void WriteStream(string strCmd) { Stream TcpStream; //定义操作对象 strCmd = strCmd + "\r\n"; //加入换行符 TcpStream = this.GetStream(); //获取数据流 //将命令行转化为byte[] byte[] bWrite = Encoding.GetEncoding(strCharset).GetBytes(strCmd.ToCharArray()); //由于每次写入的数据大小是有限制的,那么我们将每次写入的数据长度定在75个字节,一旦命令长度超过了75,就分步写入。 int start = 0; int length = bWrite.Length; int page = 0; int size = 75; int count = size; try { if (length > 75) { //数据分页 if ((length / size) * size < length) { page = length / size + 1; } else { page = length / size; } for (int i = 0; i < page; i++) { start = i * size; if (i == page - 1) { count = length - (i * size); } TcpStream.Write(bWrite, start, count);//将数据写入到服务器上 } } else { TcpStream.Write(bWrite, 0, bWrite.Length); } } catch (Exception) { } }
public bool OperaStream(TcpClient tcpc, string strCmd) { try { NetworkStream TcpStream; if (strCmd != "") { strCmd += CRLF; } TcpStream = tcpc.GetStream(); byte[] bWrite = Encoding.Default.GetBytes(strCmd.ToCharArray()); TcpStream.Write(bWrite, 0, bWrite.Length); string sp = ""; string returndata = getResult(tcpc); sp = returndata.Substring(0, 3); if (returndata.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries).Length > 1) { sp = returndata.Substring(returndata.IndexOf("\r\n") + 2, 3); } if (sp == "250" || sp == "220") { return(true); } return(false); } catch (Exception) { //Console.WriteLine(err.Message); return(false); } }