[RESOLVED] Networking with Winsock, webserver code return.
Ok here's what I'm doing it's not complicated if you know Winsock.
Im using a hosts file in my app to block the adverts, and as you know to do this I redirect ad servers to 127.0.0.1. This is not a problem and I can block the ads successfully.
I want to make a vb6 file to listen on port 80 of 127.0.0.1 (itself) and then when it gets a get request simply return the page BLOCK.HTM at C:\
Why?
The Hosts file redirects to loopback/localhost. At localhost I want to run a webpage (block.htm) so that when an ad is in the browser, it displays block.htm where I can write that an advert was blocked. .See?
I would use index.html but WebServer 2.0 (see above) does not have default starting pages.
I hope you can see now. I can already handle the popup blocking, I simply want to make a project with a winsock control so that I can respond to requests on 127.0.0.1 instead of HOSTS returning a page cannot be displayed. This will be an efficient way of showing the user an ad was blocked and I will be able to give them options to see page anyway.
Basically in WebServer 2.0, it returns the page depending on your query string. Any web server does, for example, If I sent www.blahblah.com/hey.asp to an IIS server it would respond with c:\inetpub\hey.asp (example), because I cannot specify urls in hosts file. I want it so that 127.0.0.1/ or 127.0.0.1/whateveriswrittenhere always returns the page Block.htm
So somethng like
(SECURITY IMPLEMENTATION) NO ACCESS TO OUT OF ROOT)
If rData like *get* blah, etc...
then *GET BLOCK.HTM
I just don't get this last bit.
Anyone please help?
Re: Networking with Winsock, webserver code return.
I am sorry I thought I was replying to a thread but accidentally made a new one, changed title now. Thanks.
Re: Networking with Winsock, webserver code return.
vb Code:
Dim CurrentConnection As Integer
Dim HTML1
Private Sub Winsock1_Close(Index As Integer)
Winsock1(Index).Close
End Sub
Private Sub Winsock1_ConnectionRequest(Index As Integer, ByVal requestID As Long)
CurrentConnection = CurrentConnection + 1
Load Winsock1(CurrentConnection)
If Winsock1(CurrentConnection).State = sckClosed Then Winsock1(CurrentConnection).Close
Winsock1(CurrentConnection).Accept (requestID)
End Sub
Private Sub Winsock1_DataArrival(Index As Integer, ByVal bytesTotal As Long)
GetWEBPAGE
Winsock1(CurrentConnection).SendData HTML1
End Sub
Private Sub Winsock1_Error(Index As Integer, 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)
Winsock1(Index).Close
End Sub
Private Sub Winsock1_SendComplete(Index As Integer)
Winsock1(Index).Close
End Sub
Private Sub Command1_Click()
Winsock1(CurrentConnection).LocalPort = "80"
Winsock1(CurrentConnection).Listen
End Sub
Public Function GetWEBPAGE()
Dim fFile As Integer
Dim Data As String
fFile = FreeFile
Open "c:\test.html" For Binary As #fFile
Data = String(FileLen("c:\test.html"), " ")
Get #fFile, , Data
HTML1 = Data
End Function
Private Sub Command2_Click()
For i = 0 To CurrentConnection
Winsock1(i).Close
Next i
End Sub
Re: Networking with Winsock, webserver code return.
you can post project? because i cant copy and paste vbcode comes out in word wrap format.
thks
Re: Networking with Winsock, webserver code return.
Re: Networking with Winsock, webserver code return.
THANK YOU SO MUCH!!!!!!!!!
lol sorry bout caps.
again thank you for your effort and support.
Re: Networking with Winsock, webserver code return.
No Problem ;-)
If that worked, please change the title to
[RESOLVED] in the front.
Thanks!
Re: Networking with Winsock, webserver code return.
Just a few suggestions and possible bugs in that program above
- HTML1 is of variant data type
- GetWEBPAGE function is used in the way a sub should be used. It returns nothhing and the return type is of variant
- Line9: you're saying if its closed then close it. I'd replace this with Winsock().close because a connection cannot be accepted without it being closed, no need for any if statement.
- Line 26: no need for quotes around an integer, they are for string literals
- Line 25: ALWAYS close the socket before trying to listen
- Biggest problem: The program creates a new socket each time a client connects, if this app is ran for a long period of time it will crash after a certain point, and it will eat the system ram, imagine running it for 1day and get say 15 connections, thats 15 loaded unused sockets, imagine it after a week.
What should be happening is when a connection is made, CHECK if there is any available sockets to use, if yes accept the connection on that socket, if not, load a new one up. Thats the procedure it should take. Do not load a new socket up upon every request unless absolutely required.
One more thing, if this is only going to run on one computer locally, whats the need for multiple clients?
Re: [RESOLVED] Networking with Winsock, webserver code return.
Yeah thanks for the input on it i see exactly what you mean, it was just something quick, and i wasn't sure how many connections he was gonna have that's why there's an array. It wouldn't be hard to modify it to fix all that, just givin him somewhere to start, not gonna do the whole thing myself :-D