当前位置:首页 > 网络编程 > WEB编程 > ASP.net > 用浏览器来接收C# 的程序返回的时间cool! 

用浏览器来接收C# 的程序返回的时间cool! 

点击次数:19 次 发布日期:2008-11-27 00:14:00 作者:源代码网
源代码网推荐 /*
源代码网推荐豆腐制作 都是精品
源代码网推荐http://www.asp888.net 豆腐技术站
源代码网推荐如转载 请保留版权信息
源代码网推荐*/
源代码网推荐今天早上 我写了一篇 用socket 做的 时间服务器,当时我说准备用一段时间作个不需要客户端接收数据
源代码网推荐而是用 浏览器 接收数据的程序,很顺利,一天的时间 我就做好了:)
源代码网推荐闲话不说,先看程序。。。

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;
源代码网推荐}
源代码网推荐}
源代码网推荐呵呵,一头雾水??我也累了,明天再回答大家的问题吧!

作者:豆腐()


源代码网推荐
源代码网供稿.

网友评论 (0)
会员中心
网络编程
本站推荐
网络编程之精华