111,074
社区成员




private static bool SendString(string strMessage, ref string rsp)
{
bool result = false;
Socket c = null;
int sendBytes = 0;
try
{
string host = _desthost;
int port = _destport;
IPAddress ip = IPAddress.Parse(host);
IPEndPoint ipe = new IPEndPoint(ip, port);//把ip和端口转化为IPEndPoint实例
c = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//创建一个Socket
c.ReceiveTimeout = 30000;
c.Connect(ipe);//连接到服务器
byte[] bs = Encoding.Default.GetBytes(strMessage);
sendBytes = c.Send(bs, bs.Length, 0);//发送信息
WriteLog("SEND=" + strMessage);
byte[] recvBytes = new byte[1024];
c.Receive(recvBytes, recvBytes.Length, 0);//从服务器端接受返回信息
rsp = Encoding.Default.GetString(recvBytes, 0, recvBytes.Length).TrimEnd('\0');
WriteLog("RECV=" + rsp);
c.Close();
result = true;
}
catch (Exception ex)
{
rsp = ex.Message;
result = false;
WriteSysLog(ex.Message + "\r\n[data=" + strMessage + "]");
}
finally
{
if(c != null)
c.Close();
}
return result;
}