< 일반적인 select 사용 >
try { ArrayList selectList = new ArrayList(); selectList.Add(mySock); if (selectList.Count == 0) { Thread.Sleep(10); return; }
Socket.Select(selectList, null, null, 1000000);
foreach (Socket sock in selectList) { if (sock == mySock) { // do something with mySock... } } } catch (Exception ex) { Trace.WriteLine(ex.ToString()); }
< TCP Connection 타임아웃 >
Socket clientSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); clientSock.Blocking = false; ... public bool Connect(IPAddress serverIP, int serverPort, int timeout) { try { clientSock.Connect(serverIP, serverPort); return true; } catch (SocketException ex) { if (ex.SocketErrorCode == SocketError.WouldBlock) { ArrayList selectArray = new ArrayList(); selectArray.Add(clientSock);
Socket.Select(null, selectArray, null, timeout * 1000000);
if (selectArray.Count == 0) { Trace.WriteLine(ex.ToString()); return false; } return true; } } catch (Exception ex) { Trace.WriteLine(ex.ToString()); return false; } }
댓글 없음:
댓글 쓰기