日期:2014-05-20  浏览次数:20625 次

简单问题,四十分,在线等
try
{
     int   port   =   10028;
     string   host   =   "61.240.233.90 ";
     IPAddress   ip   =   IPAddress.Parse(host);
     IPEndPoint   ipe   =   new   IPEndPoint(ip,   port);//把ip和端口转化为IPEndPoint实例
     Socket   c   =   new   Socket(AddressFamily.InterNetwork,   SocketType.Stream,   ProtocolType.Tcp);//创建一个Socket
     c.Connect(ipe);//连接到服务器
     string   sendStr   =   xml.buildXmToLT(mainnumber);
     byte[]   bs   =   Encoding.ASCII.GetBytes(sendStr);
     c.Send(bs,   bs.Length,   0);//发送测试信息
     string   recvStr   =   " ";
     byte[]   recvBytes   =   new   byte[1024];
     int   bytes;
     bytes   =   c.Receive(recvBytes,   recvBytes.Length,   0);//从服务器端接受返回信息,怎么样在规定时间没有接收到返回信息就跳到catch()
     recvStr   +=   Encoding.ASCII.GetString(recvBytes,   0,   bytes);
//Response.Write(recvStr);
     c.Close();
return   recvStr;
    }   
    catch(Exception   ex)
    {
     //Response.Write(ex.ToString());
return   null;
    }

从服务器端接受返回信息,怎么样在规定时间没有接收到返回信息就跳到catch(),释放服务器资源



------解决方案--------------------
Socket.ReceiveTimeout属性