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

首頁 > 學院 > 開發設計 > 正文

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

2019-11-18 19:50:30
字體:
來源:轉載
供稿:網友

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


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 雅安市| 杂多县| 香港 | 泗阳县| 巴塘县| 张家界市| 定边县| 马边| 舞阳县| 长汀县| 娱乐| 浦城县| 永顺县| 武汉市| 隆化县| 大关县| 紫阳县| 玉溪市| 湘潭市| 瑞昌市| 宜春市| 容城县| 阿拉善右旗| 开化县| 鄂温| 茂名市| 亚东县| 平阳县| 长岭县| 皋兰县| 嘉义市| 辽阳市| 张家口市| 施甸县| 班玛县| 汶上县| 平利县| 扶绥县| 财经| 芦溪县| 会同县|