快精灵印艺坊 您身边的文印专家
广州名片 深圳名片 会员卡 贵宾卡 印刷 设计教程
产品展示 在线订购 会员中心 产品模板 设计指南 在线编辑
 首页 名片设计   CorelDRAW   Illustrator   AuotoCAD   Painter   其他软件   Photoshop   Fireworks   Flash  

 » 彩色名片
 » PVC卡
 » 彩色磁性卡
 » 彩页/画册
 » 个性印务
 » 彩色不干胶
 » 明信片
   » 明信片
   » 彩色书签
   » 门挂
 » 其他产品与服务
   » 创业锦囊
   » 办公用品
     » 信封、信纸
     » 便签纸、斜面纸砖
     » 无碳复印纸
   » 海报
   » 大篇幅印刷
     » KT板
     » 海报
     » 横幅

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

/*
豆腐制作 都是精品
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;
}
}
呵呵,一头雾水??我也累了,明天再回答大家的问题吧!


返回类别: 教程
上一教程: .Net开发平台研究(三)
下一教程: About Public Structure----内详

您可以阅读与"用浏览器来接收C# 的程序返回的时间cool!"相关的教程:
· Community中的程序配置
· 检测客户端显示器分辨率、浏览器类型和客户端IP
· 用ASP.Net编写的查询域名的程序
· 用ASP.Net写一个发送ICQ信息的程序
· 用Visual C#打造个性化的IE浏览器
    微笑服务 优质保证 索取样品