1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
|
newvar docbase = "C:\Visual Basic\D++\DLL\";
function main()
{
winsock server;
call listen();
}
function listen()
{
title "SimpleWeb";
newvar port=81,state;
sock[server].listen(port);
AddText("Info> Listening on port "& port & "...");
}
function server_close()
{
sock[server].Close();
AddText("Info> Closed.");
listen();
}
function server_request(requestID)
{
newvar remote;
sock[server].close();
sock[server].accept(requestID);
sock[server].getinfo("remoteip", remote);
AddText("Info> Connection request from " & remote);
AddText("Info> Connection accepted.");
'call SendData("Hello! Welcome to SimpleServer!");
}
function server_incoming(data, bytesTotal)
{
newvar path;
'AddText("Client> " & Data);
path = parseHTTP(data);
sendfile(path);
'close the server... we're done!
sock[server].close();
listen();
}
function server_error(number, description)
{
AddText("Error> " & description);
server_Close();
}
function AddText(text)
{
screenput text; screen;
}
function SendData(data)
{
sock[server].Send(Data);
AddText("Server> Sending " & len(Data) & " bytes of data.");
doevents;
}
function sendfile(file)
{
newvar data, f;
newvar fsize, totalsize, chunk=2048;
file = docbase & file;
AddText("Info> File: " & file);
if fileexist(file) = false then
data = "<html><body><h1>404 File Not Found</h1></body></html>" & dpp.crlf;
data = data & "File: " & file;
senddata(data);
return;
endif
f = f_open(file, "b");
totalsize = lof(f);
do until fsize = totalsize;
if loc(f) > chunk then chunk = lof(1) - loc(1); endif
fsize = fsize + chunk;
senddata(f_getchunk(f, chunk));
doevents;
loop
f_close(f);
}
function parseHTTP(http)
{
newvar start=1, endpos;
newvar path;
start = instr(1, http, "GET ");
if start = 0 then
parseHTTP = "nothing";
endif
endpos = instr(start, http, "HTTP");
if endpos = 0 then
parseHTTP = "nothing";
endif
start = start + 4;
endpos--;
path = mid(http, start + 1, endpos - start);
if path = "" then
path = "/index.html";
endif
return path;
}
function getFooter()
{
getFooter = getFooter & "<br><hr><br>";
getFooter = getFooter & "<em>D++ Web Server</em>";
}
|