国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > 綜合 > 正文

用瀏覽器來接收C# 的程序返回的時間

2024-07-21 02:27:32
字體:
供稿:網(wǎng)友

今天早上 我寫了一篇 用socket 做的 時間服務(wù)器,當(dāng)時我說準(zhǔn)備用一段時間作個不需要客戶端接收數(shù)據(jù)
而是用 瀏覽器 接收數(shù)據(jù)的程序,很順利,一天的時間 我就做好了:)
閑話不說,先看程序。。。

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;
}
}

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 庆阳市| 罗田县| 东至县| 五大连池市| 灵宝市| 孙吴县| 南城县| 云安县| 荆门市| 德保县| 罗江县| 霍林郭勒市| 海宁市| 抚远县| 垣曲县| 南宫市| 定边县| 芜湖市| 合作市| 蒙阴县| 云南省| 合山市| 秭归县| 铁岭县| 胶州市| 西和县| 甘洛县| 稻城县| 石门县| 蕉岭县| 永昌县| 高州市| 邵阳县| 虎林市| 平定县| 雷山县| 延安市| 通海县| 万源市| 湘乡市| 廉江市|