I have often seen many ugly tutorials on the web where people use doevents in a loop while waiting for a response from the remote server. VB is event driven - and the winsock control has enough events to handle things without needing a loop.

The code below is part of a pop3 server class that uses the winsock control. Just thought this contribution could make a difference to sonebody looking to reduce the processor usage from 100% to almost nothing while their program sits in a loop doing nothing useful. Maybe more posts will spurn this thing on a bit... Any comments...?

VB Code:
  1. Forward the request to the remote server if the command is an
  2. 'authentication command, otherwise get the popclient to process
  3. 'the command locally(as the popclient has already downloaded and
  4. 'filtered messages)
  5. Private Sub mwsServer_DataArrival(ByVal bytesTotal As Long)
  6.     Dim strRequest As String
  7.     Dim strArg As String
  8.     Dim strParts() As String
  9.     Dim lngNumber As Long
  10.     Dim PassThrough As POP3SyntaxEnum
  11.     mwsServer.GetData strRequest, , bytesTotal
  12.    
  13.     'these commands are allowed to authenticate the
  14.     'mail client on the network and to initiate
  15.     'the client server communication
  16.     PassThrough = POP_USER Or POP_PASS Or _
  17.                     POP_QUIT Or POP_NOOP
  18.    
  19.     mpopState = MessageToCommand(strRequest)
  20.     strParts = Split(strRequest, " ")
  21.     If UBound(strParts) > 0 Then
  22.         If IsNumeric(strParts(1)) Then
  23.             lngNumber = CLng(strParts(1))
  24.             mlngMessage = lngNumber
  25.         Else
  26.             strArg = strParts(1)
  27.         End If
  28.     End If
  29.        
  30.     If (mpopState And PassThrough) = mpopState Then
  31.         mobjPOPClient.ProcessRequest strRequest
  32.     Else
  33.         'the other commands issued really act on the mail
  34.         'store object which contains messages that have been
  35.         'received by this server.
  36.         Select Case mpopState
  37.         Case mpopState = POP_RSET
  38.             mlngListPtr = 0
  39.             mstrList = mobjMessages.GetRSETMsg
  40.             mwsServer.SendData mstrList(mlngListPtr)
  41.             mlngListPtr = mlngListPtr + 1
  42.         Case mpopState = POP_STAT
  43.             mlngListPtr = 0
  44.             mstrList = mobjMessages.GetSTATMsg
  45.             mwsServer.SendData mstrList(mlngListPtr)
  46.             mlngListPtr = mlngListPtr + 1
  47.         Case mpopState = POP_DELE
  48.             mlngListPtr = 0
  49.             mstrList = mobjMessages.GetDELEMsg(lngNumber)
  50.             mwsServer.SendData mstrList(mlngListPtr)
  51.             mlngListPtr = mlngListPtr + 1
  52.         Case mpopState = POP_LIST
  53.             mlngListPtr = 0
  54.             mstrList = mobjMessages.GetLISTMsg(lngNumber)
  55.             mwsServer.SendData mstrList(mlngListPtr)
  56.             mlngListPtr = mlngListPtr + 1
  57.         Case mpopState = POP_RETR   'must be able to handle chunked data!!!
  58.             mlngListPtr = 0
  59.             mstrList = mobjMessages.GetRETRMsg(lngNumber)
  60.             mwsServer.SendData mstrList(mlngListPtr)
  61.             mlngListPtr = mlngListPtr + 1
  62.         End Select
  63.     End If
  64. End Sub