|
-
Jul 31st, 2007, 11:52 AM
#1
Thread Starter
Lively Member
[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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|