linux社区爱心援助Linux认证系列教程业界动态站务新闻公司招聘建议留言网址大全LPI专题CISCO专题
设为首页
加入收藏
管理团队
JSP  
JAVA  
PERL  
 您的位置:首页 > 开发语言 > c/c++/c# >
栏目导栏
  php
  JSP
  ASP
  asp.net
  JAVA
  c/c++/c#
  perl
  JavaScript
  Basic
  Delphi
资料搜索
热门文章
·Windows/Linux下配置Eclipse+C
·c语言static与extern的用法
·VC++(Ctime日期函数)应用
·typedef struct和struct的区别
·C/C++对文件操作
·C#发送Email邮件方法总结
·struct的初始化,拷贝及指针成
·C常用算法程序实例-线性代数方
·C语言入门实例 switch使用
·c++二叉树实现源代码
·对初学者的建议:如何学习C语言
·C++ Builder 处理控件中的文本
·常用的BCB & Delphi 函数
·一道C#面试题目引发的思考
·C++ Builder 使用Canvas对象的
最新文章
·使用C/C++扩展Python
·C语言入门实例 switch使用
·在C#里实现DATAGRID的打印预览
·如何使用 Visual C# .NET 创建
·C#发送Email邮件方法总结
·一道C#面试题目引发的思考
·c语言艺术清屏
·c语言static与extern的用法
·大数的阶乘算法
·C#编码好习惯
·构造函数,复制构造函数
·typedef struct和struct的区别
·struct的初始化,拷贝及指针成
·超强的指针学习笔记推荐
·STL中erase方法对链表类容器的
Google
 
浏览器来接收C# 的程序返回的时间
[ 作者:  加入时间:2007-10-09 11:58:15  来自:Linux联盟收集整理 ]

今天早上 我写了一篇 用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论坛讨论

评论】【加入收藏夹】【 】【打印】【关闭
※ 相关链接
 ·IE浏览器右下脚弹出的广告特效代码  (2007-10-29 13:52:47)
 ·IE浏览器中一个值得关注的JS问题  (2007-10-29 13:49:46)
 ·JS来实现浏览器菜单的命令  (2007-10-29 13:46:38)
 ·JSP获取客户端的浏览器和操作系统信息  (2007-10-25 14:04:15)
 ·利用Java实现网页浏览器  (2007-10-25 13:33:52)
 ·asp如何向浏览器发送内容  (2007-10-17 15:14:58)
 ·Firefox 浏览器的DOM原型扩展  (2007-10-15 14:22:11)
 ·浏览器状态栏javascript特效编程实例  (2007-10-15 12:53:13)
 ·编程技巧:php控制用户的浏览器  (2007-10-11 12:26:42)
 ·Javascript实例:利用Javascript基于浏览器类型的重定向  (2007-10-09 12:14:32)