472,950 Members | 2,190 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,950 software developers and data experts.

a simple http server plz help

hi,
i am creating an simple http server ie it serves only static pages .
u can compile the code then use ur browser if it is IE then the it
shows the page but the http header is also shown
HTTP/1.0 200 OK Content-Type: text/html
nweb Web Server Sorry:
but if the browser is mozilla/Firefox it shows the recieved data as an
application and tells me to save it.

also i want that the program should continiously listen on the port and
send data to more than one client

also is it possible to invoke a thread that calls a function with
arguments

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;

class Program
{
static int BUFSIZE= 8096;

enum msg
{
ERROR =42,
SORRY =43,
LOG = 44,
}

static void log(msg type,string s1,string s2) //used for logging
of errors
{
string logfile = "http.log";
string logbuffer;
FileStream file = new FileStream(logfile, FileMode.Append);
StreamWriter sw = new StreamWriter(file);
switch (type)
{
case msg.ERROR: logbuffer = "Error: " + s1 +" "+ s2 + "
exiting ";
sw.WriteLine(logbuffer);
sw.WriteLine();
sw.Close();
break;
case msg.SORRY: logbuffer = "sorry: " + s1 + " " + s2 + "
";
sw.WriteLine(logbuffer);
//add funtion to send data to user;
sw.WriteLine();
sw.Close();
break;
case msg.LOG: logbuffer = "info: " + s1 + " " + s1 + " ";
sw.WriteLine(logbuffer);
sw.WriteLine();
sw.Close();
break;
}
if (type == msg.ERROR || type == msg.SORRY)
{
System.Environment.Exit(0);
}

}
public static extn[] fileextsused( )
{
extn []extentions =new extn[10];
extentions[1].ext= "gif";
extentions[1].filetype="image/gif" ;
extentions[2].ext="jpg";
extentions[2].filetype="image/jpeg";
extentions[3].ext="png";
extentions[3].filetype= "image/png";
extentions[4].ext="zip";
extentions[4].filetype="image/zip";
extentions[5].ext="gz";
extentions[5].filetype= "image/gz";
extentions[6].ext="tar";
extentions[6].filetype= "image/tar";
extentions[7].ext="htm";
extentions[7].filetype="text/html";
extentions[8].ext="html";
extentions[8].filetype="text/html";
extentions[9].ext="0";
extentions[9].filetype="0";
return extentions;

}
public struct extn
{
public String ext;
public string filetype;
}
public static void web(Socket suck ) //used for sending data to
the //client
{
Encoding enc = Encoding.Unicode;
byte [] rbuffer =new byte[BUFSIZE+1];//recieve buffer
byte [] sbuffer =new byte[BUFSIZE+1];//data to be send
byte[] header = enc.GetBytes("HTTP/1.0 200
OK\r\nContent-Type: %s\r\n\r\n");//http header
string str="<HTML><BODY><H1>nweb Web Server
Sorry:</H1></BODY></HTML>\r\n";//actual page

sbuffer = enc.GetBytes(str);
suck.Receive(rbuffer);
suck.Send(header);
suck.Send(sbuffer);

}

static void Main(string[] args)
{
DirectoryInfo dir;

args = new string[2];
args[0] = "80";
args[1] = @"c:\a";
int port;
port = Convert.ToInt32(args[0]);
dir = new DirectoryInfo(args[1]);

int i=0;
extn []extentions =new extn[10];
extentions=fileextsused();
if (args.Length <2 || args.Length >2 || args[0] == "/?")
{
Console.WriteLine("hint: nweb Port-Number
Top-Directory\n\n"+
"\tnweb is a small and very safe mini web server\n"+
"\tnweb only servers out file/web pages with extensions named
below\n"+
"\t and only from the named directory or its
sub-directories.\n"+
"\tThere is no fancy features = safe and secure.\n\n"+
"\tExample: nweb 8181\n c:\\web &\n\n"+
"\tOnly Supports:");
for(i=0;i<extentions.Length;i++)
{

Console.WriteLine("\t"+extentions[i].ext+"\t"+extentions[i].filetype+"");
}

Console.WriteLine("\n\tNot Supported: URLs including
\"..\",Java, Javascript, CGI\n"+
"\tNot Supported: directories \n\tc:\\ " + "\n\t"
+
Environment.GetFolderPath(Environment.SpecialFolde r.System)+"\n\t"

+Environment.GetFolderPath(Environment.SpecialFold er.ProgramFiles) +
"\n\t"
+
Environment.GetFolderPath(Environment.SpecialFolde r.Personal) + "\n\t"
+
Environment.GetFolderPath(Environment.SpecialFolde r.CommonProgramFiles)+"\n"
+"\tNo warranty given or implied\n\tUmesh Tangnu
umeshktan...@gmail.com\n");
Console.ReadLine();
System.Environment.Exit(0);
}

else
{
if ((args[1] ==
Environment.GetFolderPath(Environment.SpecialFolde r.System)) ||
(args[1] ==
Environment.GetFolderPath(Environment.SpecialFolde r.Personal)) ||
(args[1] ==
Environment.GetFolderPath(Environment.SpecialFolde r.ProgramFiles)))
{
Console.WriteLine("ERROR: Bad top directory "+
args[1]+" see nweb /?\n");
Console.ReadLine();

System.Environment.Exit(0);
}
if(!dir.Exists)
{
Console.WriteLine("ERROR : Top Directory
"+args[1]+" doesnot exist");
Console.Read();
System.Environment.Exit(0);
}

}
if (port < 0 || port > 60000)
log(msg.ERROR, "Invalid port number (try 1->60000)",
args[0]);
log(msg.LOG, "nweb starting", args[0]);

IPAddress iadd =IPAddress.Parse("192.168.1.2");
IPEndPoint ipe = new IPEndPoint(iadd, port);
Socket sock = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
sock.Bind(ipe);
sock.Listen(32);
sock = sock.Accept();
web(sock);
sock.Shutdown(SocketShutdown.Both);
sock.Close();
}
}

Dec 2 '05 #1
2 2921
have you looked at how someone else might have done this already?

http://www.devhood.com/tools/tool_de...px?tool_id=351

--
Regards

John Timney
ASP.NET MVP
Microsoft Regional Director

"lucifer" <um**********@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
hi,
i am creating an simple http server ie it serves only static pages .
u can compile the code then use ur browser if it is IE then the it
shows the page but the http header is also shown
HTTP/1.0 200 OK Content-Type: text/html
nweb Web Server Sorry:
but if the browser is mozilla/Firefox it shows the recieved data as an
application and tells me to save it.

also i want that the program should continiously listen on the port and
send data to more than one client

also is it possible to invoke a thread that calls a function with
arguments

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;

class Program
{
static int BUFSIZE= 8096;

enum msg
{
ERROR =42,
SORRY =43,
LOG = 44,
}

static void log(msg type,string s1,string s2) //used for logging
of errors
{
string logfile = "http.log";
string logbuffer;
FileStream file = new FileStream(logfile, FileMode.Append);
StreamWriter sw = new StreamWriter(file);
switch (type)
{
case msg.ERROR: logbuffer = "Error: " + s1 +" "+ s2 + "
exiting ";
sw.WriteLine(logbuffer);
sw.WriteLine();
sw.Close();
break;
case msg.SORRY: logbuffer = "sorry: " + s1 + " " + s2 + "
";
sw.WriteLine(logbuffer);
//add funtion to send data to user;
sw.WriteLine();
sw.Close();
break;
case msg.LOG: logbuffer = "info: " + s1 + " " + s1 + " ";
sw.WriteLine(logbuffer);
sw.WriteLine();
sw.Close();
break;
}
if (type == msg.ERROR || type == msg.SORRY)
{
System.Environment.Exit(0);
}

}
public static extn[] fileextsused( )
{
extn []extentions =new extn[10];
extentions[1].ext= "gif";
extentions[1].filetype="image/gif" ;
extentions[2].ext="jpg";
extentions[2].filetype="image/jpeg";
extentions[3].ext="png";
extentions[3].filetype= "image/png";
extentions[4].ext="zip";
extentions[4].filetype="image/zip";
extentions[5].ext="gz";
extentions[5].filetype= "image/gz";
extentions[6].ext="tar";
extentions[6].filetype= "image/tar";
extentions[7].ext="htm";
extentions[7].filetype="text/html";
extentions[8].ext="html";
extentions[8].filetype="text/html";
extentions[9].ext="0";
extentions[9].filetype="0";
return extentions;

}
public struct extn
{
public String ext;
public string filetype;
}
public static void web(Socket suck ) //used for sending data to
the //client
{
Encoding enc = Encoding.Unicode;
byte [] rbuffer =new byte[BUFSIZE+1];//recieve buffer
byte [] sbuffer =new byte[BUFSIZE+1];//data to be send
byte[] header = enc.GetBytes("HTTP/1.0 200
OK\r\nContent-Type: %s\r\n\r\n");//http header
string str="<HTML><BODY><H1>nweb Web Server
Sorry:</H1></BODY></HTML>\r\n";//actual page

sbuffer = enc.GetBytes(str);
suck.Receive(rbuffer);
suck.Send(header);
suck.Send(sbuffer);

}

static void Main(string[] args)
{
DirectoryInfo dir;

args = new string[2];
args[0] = "80";
args[1] = @"c:\a";
int port;
port = Convert.ToInt32(args[0]);
dir = new DirectoryInfo(args[1]);

int i=0;
extn []extentions =new extn[10];
extentions=fileextsused();
if (args.Length <2 || args.Length >2 || args[0] == "/?")
{
Console.WriteLine("hint: nweb Port-Number
Top-Directory\n\n"+
"\tnweb is a small and very safe mini web server\n"+
"\tnweb only servers out file/web pages with extensions named
below\n"+
"\t and only from the named directory or its
sub-directories.\n"+
"\tThere is no fancy features = safe and secure.\n\n"+
"\tExample: nweb 8181\n c:\\web &\n\n"+
"\tOnly Supports:");
for(i=0;i<extentions.Length;i++)
{

Console.WriteLine("\t"+extentions[i].ext+"\t"+extentions[i].filetype+"");
}

Console.WriteLine("\n\tNot Supported: URLs including
\"..\",Java, Javascript, CGI\n"+
"\tNot Supported: directories \n\tc:\\ " + "\n\t"
+
Environment.GetFolderPath(Environment.SpecialFolde r.System)+"\n\t"

+Environment.GetFolderPath(Environment.SpecialFold er.ProgramFiles) +
"\n\t"
+
Environment.GetFolderPath(Environment.SpecialFolde r.Personal) + "\n\t"
+
Environment.GetFolderPath(Environment.SpecialFolde r.CommonProgramFiles)+"\n"
+"\tNo warranty given or implied\n\tUmesh Tangnu
umeshktan...@gmail.com\n");
Console.ReadLine();
System.Environment.Exit(0);
}

else
{
if ((args[1] ==
Environment.GetFolderPath(Environment.SpecialFolde r.System)) ||
(args[1] ==
Environment.GetFolderPath(Environment.SpecialFolde r.Personal)) ||
(args[1] ==
Environment.GetFolderPath(Environment.SpecialFolde r.ProgramFiles)))
{
Console.WriteLine("ERROR: Bad top directory "+
args[1]+" see nweb /?\n");
Console.ReadLine();

System.Environment.Exit(0);
}
if(!dir.Exists)
{
Console.WriteLine("ERROR : Top Directory
"+args[1]+" doesnot exist");
Console.Read();
System.Environment.Exit(0);
}

}
if (port < 0 || port > 60000)
log(msg.ERROR, "Invalid port number (try 1->60000)",
args[0]);
log(msg.LOG, "nweb starting", args[0]);

IPAddress iadd =IPAddress.Parse("192.168.1.2");
IPEndPoint ipe = new IPEndPoint(iadd, port);
Socket sock = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
sock.Bind(ipe);
sock.Listen(32);
sock = sock.Accept();
web(sock);
sock.Shutdown(SocketShutdown.Both);
sock.Close();
}
}

Dec 2 '05 #2
thank you very much

Dec 2 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Gelo Ilzi | last post by:
I'm trying to implement a very simple http server with cgi functionality. The code is simple: import CGIHTTPServer, BaseHTTPServer httpd = BaseHTTPServer.HTTPServer(('',8000),...
10
by: GeekBoy | last post by:
Okay, I have two identical web servers running Windows 2003 web server. I have an ASP.NET application which runs great on one of them. Dedicated IP address, behind our firewall, etc. Everyone's...
2
by: Abel Chan | last post by:
Hi there, I just got an assignment to work on server maintenance. It is a weekly task and we have about 7 production servers running Win2K server. The tasks include but not limited to 1)...
5
by: Logickle | last post by:
Hi, all. I'm working on an application which requires communicating session info between separate web apps running on the same web server. The out of process server method sounded ideal, and...
2
by: Vitali Gontsharuk | last post by:
Hi! I have a problem programming a simple client-server game, which is called pingpong ;-) The final program will first be started as a server (nr. 2) and then as a client. The client then...
0
by: Link | last post by:
hello i want to make simple server client system that send data like that one : http://www.eggheadcafe.com/articles/20020323.asp just no console app well i want to make form with 2...
2
by: Sasquatch | last post by:
I'm having trouble creating a simple login page using the asp:login control. I followed some instructions in a WROX book, "Beginning ASP.NET 2.0," and the instructions are very straight forward,...
1
by: Ted | last post by:
In MS SQL I used the following to create a stored procedure. USE AdventureWorks; GO IF OBJECT_ID ( 'HumanResources.usp_My_Search', 'P' ) IS NOT NULL DROP PROCEDURE HumanResources.usp_My_Search;...
0
DressageRider
by: DressageRider | last post by:
I need your help. Desperately. Someone has to be able to fix the utter balls up I've made after dinking around in Flash for the last four days. I have no prior knowledge of anything other than...
4
by: mattmao | last post by:
Greetings everyone. I am a college student studying in the University of Technology, Sydney. PHP is not in my studying plan, I am taking subjects about J2EE in this semester. However I do...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.