PDA

Click to See Complete Forum and Search --> : Winsock Examples


MikkyThomeon
May 26th, 2004, 05:23 PM
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...?



Forward the request to the remote server if the command is an
'authentication command, otherwise get the popclient to process
'the command locally(as the popclient has already downloaded and
'filtered messages)
Private Sub mwsServer_DataArrival(ByVal bytesTotal As Long)
Dim strRequest As String
Dim strArg As String
Dim strParts() As String
Dim lngNumber As Long
Dim PassThrough As POP3SyntaxEnum
mwsServer.GetData strRequest, , bytesTotal

'these commands are allowed to authenticate the
'mail client on the network and to initiate
'the client server communication
PassThrough = POP_USER Or POP_PASS Or _
POP_QUIT Or POP_NOOP

mpopState = MessageToCommand(strRequest)
strParts = Split(strRequest, " ")
If UBound(strParts) > 0 Then
If IsNumeric(strParts(1)) Then
lngNumber = CLng(strParts(1))
mlngMessage = lngNumber
Else
strArg = strParts(1)
End If
End If

If (mpopState And PassThrough) = mpopState Then
mobjPOPClient.ProcessRequest strRequest
Else
'the other commands issued really act on the mail
'store object which contains messages that have been
'received by this server.
Select Case mpopState
Case mpopState = POP_RSET
mlngListPtr = 0
mstrList = mobjMessages.GetRSETMsg
mwsServer.SendData mstrList(mlngListPtr)
mlngListPtr = mlngListPtr + 1
Case mpopState = POP_STAT
mlngListPtr = 0
mstrList = mobjMessages.GetSTATMsg
mwsServer.SendData mstrList(mlngListPtr)
mlngListPtr = mlngListPtr + 1
Case mpopState = POP_DELE
mlngListPtr = 0
mstrList = mobjMessages.GetDELEMsg(lngNumber)
mwsServer.SendData mstrList(mlngListPtr)
mlngListPtr = mlngListPtr + 1
Case mpopState = POP_LIST
mlngListPtr = 0
mstrList = mobjMessages.GetLISTMsg(lngNumber)
mwsServer.SendData mstrList(mlngListPtr)
mlngListPtr = mlngListPtr + 1
Case mpopState = POP_RETR 'must be able to handle chunked data!!!
mlngListPtr = 0
mstrList = mobjMessages.GetRETRMsg(lngNumber)
mwsServer.SendData mstrList(mlngListPtr)
mlngListPtr = mlngListPtr + 1
End Select
End If
End Sub