Results 1 to 4 of 4

Thread: [RESOLVED] Poor man's web server with Winsock?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2003
    Posts
    99

    Resolved [RESOLVED] Poor man's web server with Winsock?

    Hello

    I need to write a front-end to a server-less database engine. To simplify things, and because the HTTP protocol is good enough for this (send a SQL query, get an answer), I was thinking of using the MS Inet control on the client side, the MS Winsock control on the server side, and have the server send a basic HTTP header followed by the payload (either OK/NOK for queries that don't return data, or CSV-formatted text for a SELECT).

    This code works... but only for small payloads, eg. for a 19,000-line text file, only about 2,900 lines are displayed on the client. Same behavior when using a regular web browser, so the problem is on the server side.

    Apparently, I need to pace the server down, so that it doesn't flood the client with more data than it can handle. Someone also mentionned that I should be using two Winsock controls instead of one, but I have no idea why, what the code would look like, and whether it's actually needed for a simple query-reply-close-listen setup.

    Here's the code:
    Code:
    Private Sub Form_Load()
    	Winsock1.Protocol = sckTCPProtocol
    	Winsock1.LocalPort = 5555
    	Winsock1.Listen
    End Sub
    
    Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
        If Winsock1.State <> sckClosed Then
            'tell Winsock to stop listening for connections
            Winsock1.Close
        End If
        Winsock1.Accept requestID
    End Sub
    
    Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
        On Error GoTo ErrHandler
        
        Dim sInput As String
        Dim sResult As String
        
        Winsock1.GetData sInput, vbString, bytesTotal
        'Here, we'll send SQL request to DBMS, and get reply
        'For now, just send data from file
    		    
        '19,000-line, CSV-formatted text file
        sResult = ReadFileContents(App.Path & "\data.txt")
    
        sOutput = "HTTP/1.1 200 OK" + vbCrLf
        sOutput = sOutput + "Connection: Close" + vbCrLf
        sOutput = sOutput + "Content-Length: " + Str$(Len(sResult)) + vbCrLf
        sOutput = sOutput + "Content-Type: text/html" + vbCrLf + vbCrLf
        sOutput = sOutput + sResult
        
        'Only sending 2,900 lines instead of 19,000!
        If Winsock1.State = sckConnected Then
            Winsock1.SendData sOutput
            DoEvents
            Winsock1.Close
            DoEvents
            Winsock1.Listen
        End If
        
        Exit Sub
        
    ErrHandler:
        MsgBox Err.Description, vbCritical, "Err #" & Err.Number
        End
    
    End Sub
    
    Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
        Winsock1.Close
    End Sub
    
    Private Sub Winsock1_Close()
        If Winsock1.State <> sckClosed Then
            Winsock1.Close
            DoEvents
            Winsock1.Listen
        End If
    End Sub
    
    Private Sub Winsock1_Error(ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)
        Debug.Print "Err : " & Description
    		
        Winsock1.Close ' close the erraneous connection
        Winsock1.Listen ' listen again
    End Sub
    Thanks for any hint

  2. #2
    Frenzied Member
    Join Date
    Aug 2000
    Location
    O!
    Posts
    1,177

    Re: Poor man's web server with Winsock?

    Well, for one thing, you are closing the connection after doing the send and the data most likely (in fact I would bet on it) has not finished being sent.

    Move the Winsock1.Close & Winsock1.Listen lines to a Winsock1_SendComplete sub. And you can get rid of the DoEvents.

  3. #3
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: Poor man's web server with Winsock?

    ccoder is right, you have to wait until all data is sent, and then close the connection (in the SendComplete event)... or just let the client close the connection...

    Anyways... if you are interested to see how a regular web-server works, see this thread:
    http://www.vbforums.com/showthread.php?t=283785

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Sep 2003
    Posts
    99

    Re: Poor man's web server with Winsock?

    Quote Originally Posted by CVMichael
    ccoder is right, you have to wait until all data is sent, and then close the connection (in the SendComplete event)... or just let the client close the connection...
    Stupid me I should have suspected SendData was asynchronous, and move the Close/Listen bits in the SendComplete event. I can send a 1MB text file with no problem.

    Thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width