Socket Receive 避免 Blocking

我们知道 Socket Blocking 属性默认true . 表明Socket 处于同步调用 , Connect , 或 Send , Receive 需等待动作 完成才能继续执行。

有一种应用场景 , Socket 处于 同步调用状态。 我们希望 Receive 时,若没数据,立即返回,而不是阻塞状态。 这里用到两个属性:

Available : 返回可读取字节数

Poll : 检测Socket 状态(是否可读,可写,及异常情况)。无法检测物理层断开产生异常

示例代码:

             #region socket 测试

            Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            s.Blocking = true;
// Connects to host using IPEndPoint.
s.Connect("42.121.252.58", 80);
// s.Connect("192.168.113.128", 11000);
if (!s.Connected)
{
Console.WriteLine("Unable to connect to host");
} byte[] byt = new byte[4096];
int length;
if (!s.Poll(100, SelectMode.SelectRead))
{
Console.WriteLine("socket not read...");
} if (!s.Poll(50, SelectMode.SelectWrite))
{
Console.WriteLine("socket not write...");
} string strRequest = @"GET http://www.cnblogs.com/ HTTP/1.1
Host: www.cnblogs.com
User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:47.0) Gecko/20100101 Firefox/47.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: deflate
Cookie: __utma=226521935.655192264.1463987470.1467102370.1467253211.26; __utmz=226521935.1467253212.26.16.utmcsr=baidu|utmccn=(organic)|utmcmd=organic; _ga=GA1.2.655192264.1463987470; CNZZDATA2017822=cnzz_eid%3D985476095-1464072054-null%26ntime%3D1464072054; pgv_pvi=6786726912; AJSTAT_ok_times=10; CNZZDATA1000085291=2001773208-1464232710-null%7C1464232710; Hm_lvt_5d6cc4516947da0ee2209a48877c77e9=1464232694,1464232700,1464233056,1464234704; Hm_lvt_3c14eade5a5788be714db0f403aeaf46=1464592341,1464597543; CNZZDATA2081367=cnzz_eid%3D999995175-1464743380-null%26ntime%3D1464743380; CNZZDATA1943205=cnzz_eid%3D1332194-1464919429-null%26ntime%3D1464919429; CNZZDATA706677=cnzz_eid%3D1663091018-1465265577-null%26ntime%3D1465265950; CNZZDATA5343953=cnzz_eid%3D1996425634-1465290765-null%26ntime%3D1465290765; a8178_times=1; CNZZDATA1028890=cnzz_eid%3D713750658-1465715751-null%26ntime%3D1465715751; CNZZDATA2686777=cnzz_eid%3D1376577942-1465895035-http%253A%252F%252Fwww.cnblogs.com%252F%26ntime%3D1465895035; SyntaxHighlighter=csharp; CNZZDATA1258105384=1452308944-1466386727-http%253A%252F%252Fwww.cnblogs.com%252F%7C1466397796; CNZZDATA1259569345=1538663036-1466750971-http%253A%252F%252Fwww.cnblogs.com%252F%7C1466750971; __utmc=226521935; .CNBlogsCookie=6A451D1565F1FA00C8931473EEFE1D227C0F57F40018603D027DDA2C24697584375F41D8BA527CDF6EB23CD3ABFA9D5BCAB249EE942E36822F74DCFC65AC447C4FCCA67EF6A15F0ABA6BF930FB3434BFC90254CA
Connection: keep-alive
If-Modified-Since: Thu, 30 Jun 2016 03:33:14 GMT ";
int i = s.Send(Encoding.UTF8.GetBytes(strRequest)); Console.WriteLine("成功发送数据:{0}", i); //发送数据后等待 50 ms
System.Threading.Thread.Sleep(50); if (!s.Poll(200, SelectMode.SelectRead))
{
Console.WriteLine("re: socket not read...");
}
else
{
// byt = new byte[1];
do
{
length = s.Receive(byt);
Console.WriteLine(System.Text.Encoding.UTF8.GetString(byt, 0, length));
//} while (s.Poll(500, SelectMode.SelectRead) && s.Available > 0 && s.Connected);
} while (s.Available > 0);
//Poll 可检测缓冲区是否还有数据可读。 , 如 socket 处于 blocking 状态 , Receive 时 防止 blocking , 可先调用 Poll 检测是否可读, 同时 Available 属性 应大于 0 , 再调用 Receive
// } while (s.Poll(500, SelectMode.SelectRead));
} if (!s.Poll(50, SelectMode.SelectWrite))
{
Console.WriteLine("re: socket not write...");
} s.Shutdown(SocketShutdown.Both);
s.Close(); #endregion
上一篇:hdoj1016 [dfs]


下一篇:【LeetCode】#7 Reverse Integer