[RESOLVED] Using Winsock To Send A Message To A Web Browser...
VB6 - Windows 10
I can set up the Winsock control to receive incoming connections from a web browser when the my IP and Port that I've opened is entered into the address bar. My program can see what is being requested (i.e.; "http://555.555.555.555:22332/request". The problem is that I can't seem to get Winsock to send a message.
I've tried
Code:
Winsock(index).SendData "Hello"
and
Code:
Winsock(index).SendData "<html>Hello</html>"
but the browser only comes back with
555.555.555.555 sent an invalid response.
ERR_INVALID_HTTP_RESPONSE
Any help here would be greatly appreciate. Thanks!
Re: Using Winsock To Send A Message To A Web Browser...
I wrote a very, very basic web server a while ago (there was a Reddit programming challenge to do so). I'd be embarrassed to show all of my code for it, but I can tell you that the response back to the client browser needs specific header information.
For an HTML Page, this is what should be sent to the client:
Code:
HTTP/1.0 200 OK
Content-type: text/html
<html>Hello</html>
So, this is an example of some code from my program. It determines the file extension of the file being requested and assigns an appropriate header for a few basic types:
Code:
Select Case pstrExt
Case "jpg", "jpeg"
pstrContentType = "Content-type: image/jpeg"
Case "gif"
pstrContentType = "Content-type: image/gif"
Case "htm", "html"
pstrContentType = "Content-type: text/html"
Case Else
pstrContentType = "Content-type: */*"
End Select
strOutData = "HTTP/1.0 200 OK" & vbCrLf & _
pstrContentType & vbCrLf & vbCrLf
After that, I read in the contents of the file being requested and append those contents to the strOutData variable and then when that is done just do a
Code:
wsockWeb.SendData strOutData
Re: Using Winsock To Send A Message To A Web Browser...
Re: [RESOLVED] Using Winsock To Send A Message To A Web Browser...
What about sending a file back, like an image or a pre-generated .html file?
Re: [RESOLVED] Using Winsock To Send A Message To A Web Browser...
Quote:
Originally Posted by
mcoulter876
What about sending a file back, like an image or a pre-generated .html file?
As I said in my post, I read in the contents of the file being requested and append those contents to the strOutData variable. So you need to define the "root" path of your web server (the local path on the web server where the website contents are stored), and you need to determine the path\filename that is being requested by the web server.
Once you have that you can build the path to the local file being requested, and then read in the contents of the file and append those contents to strOutData.
So, if you say that "C:\Inetpub\wwwroot\" is the local root path, and the user requests "http://www.yoursite.com/Subfolder/Index.html", you need code that will parse out the "Subfolder/Index.html" piece, and then the file that you serve up should come from "C:\Inetpub\wwwroot\Subfolder\Index.html"
Note that in my code, if the file is .htm/html, I am retrieving the data from the file by opening the file "For Input". For image or unknown file types, I'm opening the file "For Binary Access Read"
Re: [RESOLVED] Using Winsock To Send A Message To A Web Browser...
Or see VB6 - Yet Another Web Server. Post #20 has the latest public version for downloading.
In this approach the "web server" piece is a UserControl. It can handle static requests for stock items (HTML, images, etc.) by itself. It raises events on dynamic client requests so that its host Form can take custom actions based on each HTTP request.