今天早上 我写了一篇 用socket 做的 时间服务器,当时我说准备用一段时间作个不需要客户端接收数据EG6Linux联盟
而是用 浏览器 接收数据的程序,很顺利,一天的时间 我就做好了:)EG6Linux联盟
闲话不说,先看程序。。。
using System;EG6Linux联盟
using System.Collections;EG6Linux联盟
using System.IO;EG6Linux联盟
using System.Net;EG6Linux联盟
using System.Net.Sockets;EG6Linux联盟
using System.Threading;
class HttpProcessor {
private Socket s;EG6Linux联盟
private BufferedStream bs;EG6Linux联盟
private StreamReader sr;EG6Linux联盟
private StreamWriter sw;EG6Linux联盟
private String method;EG6Linux联盟
private String url;EG6Linux联盟
private String protocol;EG6Linux联盟
private Hashtable hashTable;
public HttpProcessor(Socket s) {EG6Linux联盟
this.s = s;EG6Linux联盟
hashTable = new Hashtable();EG6Linux联盟
}
public void process() {EG6Linux联盟
NetworkStream ns = new NetworkStream(s, FileAccess.ReadWrite);EG6Linux联盟
bs = new BufferedStream(ns);EG6Linux联盟
sr = new StreamReader(bs);EG6Linux联盟
sw = new StreamWriter(bs);EG6Linux联盟
writeURL();EG6Linux联盟
s.Shutdown(SocketShutdown.SdBoth);EG6Linux联盟
ns.Close();EG6Linux联盟
}EG6Linux联盟
public void writeURL() {EG6Linux联盟
try {EG6Linux联盟
writeSuccess();EG6Linux联盟
} catch(FileNotFoundException) {EG6Linux联盟
writeFailure();EG6Linux联盟
sw.WriteLine("File not found: " + url);EG6Linux联盟
}EG6Linux联盟
sw.Flush();EG6Linux联盟
}
public void writeSuccess() {EG6Linux联盟
sw.WriteLine("HTTP/1.1 200 OK");EG6Linux联盟
sw.WriteLine("Server: Microsoft-IIS/5.0");EG6Linux联盟
sw.WriteLine("Date: Mon, 27 Nov 2000 08:19:43 GMT");EG6Linux联盟
sw.WriteLine("Content-Length: 6");EG6Linux联盟
sw.WriteLine("Content-Type: text/html");EG6Linux联盟
sw.WriteLine("");
String strDateLine;EG6Linux联盟
DateTime now;EG6Linux联盟
now = DateTime.Now;EG6Linux联盟
strDateLine = now.ToShortDateString() + " " + now.ToLongTimeString();EG6Linux联盟
sw.WriteLine(strDateLine);EG6Linux联盟
}
public void writeFailure() {EG6Linux联盟
sw.WriteLine("HTTP/1.0 404 File not found");EG6Linux联盟
sw.WriteLine("Connection: close");EG6Linux联盟
sw.WriteLine();EG6Linux联盟
}EG6Linux联盟
}
public class HttpServer {EG6Linux联盟
public HttpServer() : this(81) {EG6Linux联盟
}
public HttpServer(int port) {EG6Linux联盟
this.port = port;EG6Linux联盟
}EG6Linux联盟
public void listen() {EG6Linux联盟
Socket listener = new Socket(0, SocketType.SockStream, ProtocolType.ProtTCP);EG6Linux联盟
IPAddress ipaddress = new IPAddress("169.254.0.244");EG6Linux联盟
IPEndPoint endpoint = new IPEndPoint(ipaddress, port);EG6Linux联盟
listener.Bind(endpoint);EG6Linux联盟
listener.Blocking = true;EG6Linux联盟
listener.Listen(-1);EG6Linux联盟
Console.WriteLine("Press Ctrl+c to Quit...");EG6Linux联盟
while(true) {EG6Linux联盟
Socket s = listener.Accept();EG6Linux联盟
HttpProcessor processor = new HttpProcessor(s);EG6Linux联盟
Thread thread = new Thread(new ThreadStart(processor.process));EG6Linux联盟
thread.Start();EG6Linux联盟
}EG6Linux联盟
}EG6Linux联盟
public static int Main(String[] args) {EG6Linux联盟
HttpServer httpServer;EG6Linux联盟
if(args.GetLength(0) > 0) {EG6Linux联盟
httpServer = new HttpServer(args[0].ToUInt16());EG6Linux联盟
} else {EG6Linux联盟
httpServer = new HttpServer();EG6Linux联盟
}EG6Linux联盟
Thread thread = new Thread(new ThreadStart(httpServer.listen));EG6Linux联盟
thread.Start();EG6Linux联盟
return 0;EG6Linux联盟
}EG6Linux联盟
}
Linux联盟收集整理 ,转贴请标明原始链接,如有任何疑问欢迎来本站Linux论坛讨论