# Writing a simple webserver

## Recommended Posts

Nice Coder    366
I've been thinking about making a very simple webserver. I can't seem to get it working. (in vb). Mozilla isn't sending any requests. It connects, then it just times out. (and says 'the document contains no data'. I connect to telnet, and it just pops up with a 'connection to host lost' thing basically instantly. Does anybody have any information that can shed some light in it? I currently have two winsock controls and come really basic debugging code.
Private Sub Form_Load()
Winsock1.Listen
Me.Hide
End Sub

Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
Winsock1.Close
Winsock1.Connect requestID
'Winsock2.Connect requestID
DoEvents
End Sub

Private Sub Winsock2_Connect()
Debug.Print "Winsock2 connected"
End Sub

Private Sub Winsock2_ConnectionRequest(ByVal requestID As Long)
Debug.Print requestID
End Sub

Private Sub Winsock2_DataArrival(ByVal bytesTotal As Long)
Dim s As String
Winsock1.GetData s, vbString, bytesTotal
Debug.Print s
End Sub


I've tried everything i can think of, but it just isn't connecting! ARGGG! From, Nice coder

##### Share on other sites
marcjulian    959
Hi,

I think it times out, because you aren't replying on the request.
It sends GET .... and is waiting for an answer, the html document.

Marc

##### Share on other sites
Nice Coder    366
It doesn't particularly matter, because:

1. It times out instantly. (it doesn't do anything. Even when i connect via telnet, it looses connection instantly) and

2. It isn't connecting. At all. It goes through the connection code, but it doesn't connect.

and

3. It isn't recieving any data. At all.

From,
nice coder

##### Share on other sites
Alan Kemp    772
I haven't written any VB networking code in a long time, but shouldn't you be calling

Winsock1.accept requestID

in Winsock1_ConnectionRequest, rather than Winsock1.Connect? Also, have you set the port number to be listening on? In code you could write

Winsock1.LocalPort = 80

Just before calling Winsock1.Listen.

Alan

##### Share on other sites
Drew_Benton    1861
If you don't mind looking a few examples...

Now after 5 views of code, you will have to register, but that site is the best [grin]. See if any of those can help you figre out what the problem is.

##### Share on other sites
Nice Coder    366
Quote:
 Original post by Alan KempI haven't written any VB networking code in a long time, but shouldn't you be calling*** Source Snippet Removed ***in Winsock1_ConnectionRequest, rather than Winsock1.Connect? Also, have you set the port number to be listening on? In code you could write *** Source Snippet Removed ***Just before calling Winsock1.Listen.Alan

Spoke too soon.
It recieves the request, but mozilla isn't doing anything. (it times out)

Rate++ anyway

From,
Nice coder

##### Share on other sites
Nice Coder    366
Quote:
 Original post by Drew_BentonIf you don't mind looking a few examples...Link 1 | Link 2 | Link 3 | Link 4Now after 5 views of code, you will have to register, but that site is the best [grin]. See if any of those can help you figre out what the problem is.

Thanks. I've already seen planetsourcecode. (and i have an account, assuming they haven't gotten rid of it.)

Now, does anybody have any idea on how to send things like images, ect?

From,
Nice coder

##### Share on other sites
Nice Coder    366
Ok, its working. (thanks drew :) i needed those egsamples. Turns out i was missing a doevents before i was closing the socket).

Now, how would i do images?

From,
nice coder

##### Share on other sites
Nice Coder    366
Ok, it works now. (with images, and 404's :))

From,
Nice coder

##### Share on other sites
Nice Coder    366
Got directory listings to work.

<html><head><title>Directory list of /Web</title></head>Directory listing of /Web<br>Made by Armada Server<hr>Directories: <hr><a href='Web/Folder'>Folder</a><br><hr> Files <hr> <a href='Web/Something.txt'>Something.txt</a><br><a href='Web/Somethingelse.txt'>Somethingelse.txt</a><br>

Looks really good :).

From,
Nice coder

##### Share on other sites
Nice Coder    366
Ok, it loads images, pages, and it does directories.

I've got some limited multithreading. (i don't know how many cliants/sec it could probably get tho).

About the only thing the server doesn't have is scripting support.

What other then that should be in a webserver?

From,
Nice coder

##### Share on other sites
marcjulian    959
ok,
good that it works now.

I think the User should be able to call an Interpreter (like Perl, PHP),
so he can use dynamic webpages.

Marc

##### Share on other sites
Nice Coder    366
Ok.

Perl, php, ect. is out of my league (to actually program an interpriter for).

Vbscript, is possible, but how would i interface it through all of the tasks?

I've also added soem code that breaks up files into chunks, which will save me if i ever put anything big up for download.

Also, whats the name for that function which returns a free file descripter?
Ie.
Open C for input as #1
open d for input as #2

It woudl return 3, because 3 isn't taken yet.

From,
Nice coder

##### Share on other sites
Nice Coder    366
Anybody?

From,
Nice coder

##### Share on other sites
Nice Coder    366
Anybody?

From,
Nice coder

##### Share on other sites
daerid    354
That's definitley advanced stuff there. You're getting into hosting scripting environments and stuff. Beyond me personally :)

By the way, what are you using to traverse directories and such? Scripting.FileSystemObject?

##### Share on other sites
Nice Coder    366
Dir's and file's. (in VB, it makes some things easy. Others just hard).

Its all less then 200 lines of vb code. (its pretty simple really).
Getting the documents? Check the first line. After the get is what your looking for.
404's - You check to see if the document exists. If it doesn't then you send the 404 page.
File/Directory listings - Change paths on file/dir box, and read off its list. (converting into html as you go.)
Multitasking - This is probably the hardest of the bunch. Go and make an array of winsock controls. When your main control gets a request, you check to see if an element in the winsock array isn't connected to anything currently.
When you find one that isn't, you connect that one to the client that was connecting to your main winsock. (you don't actually connect the main winsock up to anything.)

You then go and have everything rely only on variables that are passed as arguments. (so no globals). From then on, its pretty similar to the normal design.

This is easy to code, hard to explain.

I call it sortov multithreading, because it can sortov do a few things at once. (not really tho.) Even though its only doing one thing at once.

From,
Nice coder

##### Share on other sites
daerid    354
Oh, I know. I've written web servers before. SMTP servers too. I was just curious :)