首 页 | 精品电影 | 音乐天堂 | 在线游戏 | Flash MTV | 三湘书屋 | 幽默笑话 | 三湘图库 | 美女写真 | IT知识库 | QQ贴图 | 加入书签

网页制作网络编程图形图象操作系统冲浪宝典软件教学网络安全认证考试通信技术电子商务业内动态书籍教程原码

最近更新 文章分类 多媒体类 精品软件

本站搜索:
您的位置:三湘时空 -> IT知识库 -> 文章分类 -> C#应用 -> 用浏览器来接收C# 的程序返回的时间  
用浏览器来接收C# 的程序返回的时间


文章类别:C#应用 来源: 作者: 发表日期:2005-10-4 字体:[ ]

小游戏 | 在线影院 | 幽默笑话 | 源码下载 | Flash MTV | 音乐试听 | 书屋 | 美女写真

今天早上 我写了一篇 用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;
}
}

上一篇:ASP.Net中自定义Http处理及应用之HttpHandler篇 下一篇:C#操作技巧的数据类型之间的转换
本栏目热门文章
·C# Socket编程 2006-3-14
·[C#]解决读写包含汉字的txt文件时乱码的问题 2005-10-4
·c#操作word表格 2005-10-4
·在C#.net中将查询数据导入EXCEL表输出 2006-3-24
·使用C#在进度条中显示复制文件的进度 2005-11-13
·C#.Net网络程序开发-Socket篇 2006-3-14
·C#中使用SendMessage 2005-10-4
·利用C#进行AutoCAD的二次开发(一) 2005-10-4
·利用C#进行AutoCAD的二次开发(二) 2005-10-4
·用Visual C#获得计算机名称和IP地址 2005-10-4
新近更新文章
·C#中计算两个时间的差 2006-6-20
·基于Remoting透明代理的AOP组件 2006-6-20
·用CDO.Message打造邮件发送程序 2006-6-15
·C#箴言之用属性来访问类的私有成员 2006-6-15
·C#中父窗口和子窗口之间实现控件互操作 2006-6-15
·C# 中启动进程的三种方法 2006-6-12
·讲述c#中的类型转换 2006-6-9
·如何把html中的相对路径变成绝对路径 2006-6-9
·C#简单命名指南 2006-6-6
·在Excel中将连续多列相同数据项合并 2006-6-4
首 页 | 软件发布 | 广告联系 | 下载帮助 | 意见反馈 | 网站地图
  CopyRight? 2002-2004 WWW.SXSKY.NET? All Rights Reserved
三湘时空 站长QQ:82675303 Email: