选择显示字体大小

用浏览器来接收c# 的程序返回的时间

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


 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

Java   Asp   PHP   .Net   XML   C/C++   CGI   VB   Jsp   J2ee   J2se   J2me   EJB   Servlet   Tomcat   Resin   Struts   Weblogic   Eclipse   ANT   GUI   JMS   Web servise   IDEA   Webphere   Hibernate   Spring   Jboss   Applet   Swing   Socket   Javamail   Perl   Ajax   P2P   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons