The time returned by the program receiving C # from the browser

Source: Internet
Author: User
Program | browser

This morning I wrote a time server with sockets, when I said to be ready for a while to do not need the client to receive data
But using the browser to receive data program, very smooth, one day time I do it:
Gossip does not say, first look at the program ...

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 OK");
Sw. WriteLine ("server:microsoft-iis/5.0");
Sw. WriteLine ("Date:mon, Nov 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. ToShortDateString () + "" + 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;
}
}



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.