Tutorials to .com


ASP.NET Development Articles


Can't Find What You're Looking For?

Tutorials to .com » Dotnet » Development » Browser to receive C # program to return to the time of the cool!

Browser to receive C # program to return to the time of the cool!

Print View , by: iSee ,Total views: 7 ,Word Count: 515 ,Date: Sat, 22 Aug 2009 Time: 10:06 PM

/ *
Bean products are produced
technical points such as tofu http://www.asp888.net reprint information, please retain the copyright
* /
This morning, I wrote a socket to do with the time server, I was saying that a certain period of time to prepare for months do not need to receive data but the client browser to use the procedure to receive data, it is smooth, one day I will do a good job in the :)
Gossip did not say, look at the procedures. . .

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.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;
)
)
Oh, at a loss? ? I am also tired, tomorrow to answer your question!

Author: tofu ()


Google
 

Rating: Not yet rated

Comments

No comments posted.