/*
Tofu is made of excellent products
Http://www.asp888.net bean curd technology station
Retain the copyright information if reprinted.
*/
This morning I wrote an article about the time server using socket. At that time, I said that I was going to use it for a period of time without the client to receive data.
Instead, I used a browser to receive data. It went very well and I finished it in one day :)
If you don't talk about it, read the program first...
Using System;
Using System. Collections;
Using System. IO;
Using System. Net;
Using System. Net. Sockets;
Using System. Threading;
Class HttpProcessor {
Private Socket s;
Private BufferedStream bs;
Private StreamReader sr;
Private StreamWriter sw;
Private String method;
Private String url;
Private String protocol;
Private Hashtable hashTable;
Public HttpProcessor (Socket s ){
This. s = s;
HashTable = new Hashtable ();
}
Public void process (){
NetworkStream ns = new NetworkStream (s, FileAccess. ReadWrite );
Bs = new BufferedStream (ns );
Sr = new StreamReader (bs );
Sw = new StreamWriter (bs );
WriteURL ();
S. Shutdown (SocketShutdown. SdBoth );
Ns. Close ();
}
Public void writeURL (){
Try {
WriteSuccess ();
} Catch (FileNotFoundException ){
WriteFailure ();
Sw. WriteLine ("File not found:" + url );
}
Sw. Flush ();
}
Public void writeSuccess (){
Sw. WriteLine ("HTTP/1.1 200 OK ");
Sw. WriteLine ("Server: Microsoft-IIS/5.0 ");
Sw. WriteLine ("Date: Mon, 27 Nov 2000 08:19:43 GMT ");
Sw. WriteLine ("Content-Length: 6 ");
Sw. WriteLine ("Content-Type: text/html ");
Sw. WriteLine ("");
String strDateLine;
DateTime now;
Now = DateTime. Now;
StrDateLine = now. tow.datestring () + "" + now. ToLongTimeString ();
Sw. WriteLine (strDateLine );
}
Public void writeFailure (){
Sw. WriteLine ("HTTP/1.0 404 File not found ");
Sw. WriteLine ("Connection: close ");
Sw. WriteLine ();
}
}
Public class HttpServer {
Public HttpServer (): this (81 ){
}
Public HttpServer (int port ){
This. port = port;
}
Public void listen (){
Socket listener = new Socket (0, SocketType. SockStream, ProtocolType. ProtTCP );
IPAddress ipaddress = new IPAddress ("169.254.0.244 ");
IPEndPoint endpoint = new IPEndPoint (ipaddress, port );
Listener. Bind (endpoint );
Listener. Blocking = true;
Listener. Listen (-1 );
Console. WriteLine ("Press Ctrl + c to Quit ...");
While (true ){
Socket s = listener. Accept ();
HttpProcessor processor = new HttpProcessor (s );
Thread thread = new Thread (new ThreadStart (processor. process ));
Thread. Start ();
}
}
Public static int Main (String [] args ){
HttpServer httpServer;
If (args. GetLength (0)> 0 ){
HttpServer = new HttpServer (args [0]. ToUInt16 ());
} Else {
HttpServer = new HttpServer ();
}
Thread thread = new Thread (new ThreadStart (httpServer. listen ));
Thread. Start ();
Return 0;
}
}
Why ?? I am also tired. I will answer your questions tomorrow!