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;
}
}