简单C#Http/TCP服务器
我试图实现一个简单的http csharp服务器,当用户通过服务器IP连接时,它会将index.html之类的文档发送到浏览器。我现在对如何通过web浏览器发送一个简单的.html文档以便连接的用户可以看到它感到有点困惑 扩展流是将所有信息发送到浏览器的地方简单C#Http/TCP服务器,c#,http,sockets,tcp,C#,Http,Sockets,Tcp,我试图实现一个简单的http csharp服务器,当用户通过服务器IP连接时,它会将index.html之类的文档发送到浏览器。我现在对如何通过web浏览器发送一个简单的.html文档以便连接的用户可以看到它感到有点困惑 扩展流是将所有信息发送到浏览器的地方 using System; using System.Collections.Generic; using System.Text; using System.Net.Sockets; using System.IO; using Syst
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.IO;
using System.Threading;
namespace TCP_Socket
{
class ConnectionThread
{
public ConnectionThread(Socket socketToHandleConnection)
{
connection = socketToHandleConnection;
}
Socket connection = null; //TCP/IP socket to handle the actual connection
NetworkStream connectionStream = null;
BinaryReader inStream = null;
BinaryWriter outStream = null;
String userName = null;
public void run()
{
connectionStream = new NetworkStream(connection);
inStream = new BinaryReader(connectionStream);
outStream = new BinaryWriter(connectionStream);
userName = Environment.UserName;
byte b = 0;
String s = ""; //This will contain all the stuff the browser transmitted,
//but "all in one go".
try
{
while (connectionStream.DataAvailable)
{
b = (byte)inStream.ReadSByte();
Console.Out.Write((char)b);
s += (char)b;
}
String[] items = s.Split();//This will contain all the stuff the browser transmitted,
}
catch (EndOfStreamException eos)
{
Console.Out.WriteLine("Unexpected end of stream.");
Console.Out.WriteLine("Error caused by " + eos.Message);
}
Console.Out.WriteLine("End of stream.");
String stringOut = "HTTP/ 1.1 200 OK\r\n";
outStream.Write(stringOut.ToCharArray());
stringOut = "Content-Type: text/html\r\n";
outStream.Write(stringOut.ToCharArray());
stringOut = "Date: ";
outStream.Write(stringOut.ToCharArray());
stringOut = Convert.ToString(DateTime.Now);
outStream.Write(stringOut.ToCharArray());
stringOut = "\r\n";
outStream.Write(stringOut.ToCharArray());
stringOut = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">\r\n";
outStream.Write(stringOut.ToCharArray());
stringOut = "<html>\r\n";
outStream.Write(stringOut.ToCharArray());
stringOut = "<body>\r\n";
outStream.Write(stringOut.ToCharArray());
stringOut = "Welcome to <strong>" + userName + "'s </strong>primative HTTP server";
outStream.Write(stringOut.ToCharArray());
stringOut = "</body></html>\r\n";
outStream.Write(stringOut.ToCharArray());
inStream.Close();
outStream.Flush();
outStream.Close();
connectionStream.Close();
connection.Close();
Console.Out.WriteLine("Done; Connection closed.");
}
}
}
使用系统;
使用System.Collections.Generic;
使用系统文本;
使用System.Net.Sockets;
使用System.IO;
使用系统线程;
命名空间TCP_套接字
{
类连接读取
{
公共连接读卡器(插座插座与手持连接)
{
连接=插座与连接器连接;
}
Socket connection=null;//处理实际连接的TCP/IP套接字
NetworkStream connectionStream=null;
BinaryReader inStream=null;
BinaryWriter超流=空;
字符串userName=null;
公开募捐
{
connectionStream=新的网络流(连接);
inStream=新的二进制读取器(connectionStream);
扩展流=新的二进制编写器(connectionStream);
用户名=Environment.userName;
字节b=0;
字符串s=”“;//这将包含浏览器传输的所有内容,
//但是“一气呵成”。
尝试
{
while(connectionStream.DataAvailable)
{
b=(字节)inStream.ReadSByte();
Console.Out.Write((char)b);
s+=(char)b;
}
String[]items=s.Split();//这将包含浏览器传输的所有内容,
}
捕获(EndOfStreamException eos)
{
Console.Out.WriteLine(“意外的流结束”);
Console.Out.WriteLine(“由“+eos.Message”引起的错误);
}
Console.Out.WriteLine(“流结束”);
String stringOut=“HTTP/1.1 200正常\r\n”;
outStream.Write(stringOut.ToCharArray());
stringOut=“内容类型:text/html\r\n”;
outStream.Write(stringOut.ToCharArray());
stringOut=“日期:”;
outStream.Write(stringOut.ToCharArray());
stringOut=Convert.ToString(DateTime.Now);
outStream.Write(stringOut.ToCharArray());
stringOut=“\r\n”;
outStream.Write(stringOut.ToCharArray());
stringOut=“\r\n”;
outStream.Write(stringOut.ToCharArray());
stringOut=“\r\n”;
outStream.Write(stringOut.ToCharArray());
stringOut=“\r\n”;
outStream.Write(stringOut.ToCharArray());
stringOut=“欢迎使用”+userName+“'s灵长类HTTP服务器”;
outStream.Write(stringOut.ToCharArray());
stringOut=“\r\n”;
outStream.Write(stringOut.ToCharArray());
流内关闭();
冲水;
exptream.Close();
connectionStream.Close();
connection.Close();
Console.Out.WriteLine(“完成;连接关闭”);
}
}
}
您不需要像.NET 2.0提供的那样使用套接字编写所有HTTP代码。如果它符合您的要求,请尝试一下。搜索后,我找到了一些其他解决方案:
其他类似的嵌入式服务器包括: