Use a browser to receive the cool time returned by the C # program!

Source: Internet
Author: User

/*
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!


Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.