Web|web Service |web The personal collection of the server, only for archiving. Today, the disk is sorted, no place to put. Throw a blog.
Do not understand, don't ask me. Study for yourself.
Webserver.cs
Namespace Cnnbsun.webserver
{
Using System;
Using System.IO;
Using System.Net;
Using System.Net.Sockets;
Using System.Text;
Using System.Threading;
Class MyWebServer
{
Private TcpListener MyListener;
private int port = 8080; Select any idle port
Start and listen to the port
Start a concurrent listening process at the same time
Public MyWebServer ()
{
Try
{
Start and listen to the port
MyListener = new TcpListener (port);
Mylistener.start ();
Console.WriteLine ("Web Server Running ... Press ^c to Stop ... ");
Start a simultaneous listening process ' startlisten '
Thread th = new Thread (new ThreadStart (Startlisten));
Th. Start ();
}
catch (Exception e)
{
Console.WriteLine ("Error while listening to port:" +e.tostring ());
}
}
public void Sendheader (string shttpversion, string smimeheader, int itotbytes, string sstatuscode, ref Socket Mysocket)
{
}
}
public static void Main ()
{
MyWebServer MWS = new MyWebServer ();
}
public void Startlisten ()
{
int istartpos = 0;
String srequest;
String Sdirname;
String Srequestedfile;
String Serrormessage;
String Slocaldir;
Be aware of setting your own virtual directory/////////////////////////////////////
String smywebserverroot = "e:\\mywebserverroot\\"; Set up your virtual directory
//////////////////////////////////////////////////////////////////////////////////////////////////
String Sphysicalfilepath = "";
String sformattedmessage = "";
String sresponse = "";
while (true)
{
Accept New Connection
Socket mysocket = Mylistener.acceptsocket ();
Console.WriteLine ("Socket Type" +mysocket.sockettype);
if (mysocket.connected)
{
Console.WriteLine ("\nclient connected!! \n==================\nclient IP {0}\n ", mysocket.remoteendpoint);
byte[] breceive = new byte[1024];
int i = mysocket.receive (breceive,breceive.length,0);
Convert to String type
String sbuffer = Encoding.ASCII.GetString (breceive);
Process only "get" request type
if (sbuffer.substring (0,3)!= "get")
{
Console.WriteLine ("Process only GET request type ...");
Mysocket.close ();
Return
}
Find locations for "HTTP"
Istartpos = Sbuffer.indexof ("HTTP", 1);
if (srequestedfile.length = 0)
{
Get request File name
Srequestedfile = "index.html";
}
/////////////////////////////////////////////////////////////////////
Get request file type (set to text/html)
/////////////////////////////////////////////////////////////////////
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.