(1)usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Ne
(1)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace WebServer
{
class Program
{
static void Main(string[] args)
{
//获取本机ip的地址
IPAddress localaddress = IPAddress.Loopback;
IPEndPoint endpoint = new IPEndPoint(localaddress, 49155);
// 创建Socket对象,使用IPv4地址,数据通信类型为数据流,传输控制协议TCP协议.
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//socket绑定到断点上
socket.Bind(endpoint);
//设置链接队列的长度
socket.Listen(10);
while (true)
{
Console.WriteLine("wait an connect....");
Socket clientsockets = socket.Accept();
Console.WriteLine("Client adress is:{0}", clientsockets.RemoteEndPoint);
byte[] buffer = new byte[2048];
int receivelength = clientsockets.Receive(buffer, 2048, SocketFlags.None);
string requeststring = Encoding.UTF8.GetString(buffer, 0, receivelength);
//在服务器上输出请求
Console.WriteLine(requeststring);
// 服务器端做出相应内容
// 响应的状态行
string statusLine = "HTTP/1.1 200 OK\r\n";
byte[] respOnseStatusLineBytes= Encoding.UTF8.GetBytes(statusLine);
string respOnseBody= "
liu peng
";
string respOnseHeader=
string.Format(
"Content-Type: text/html; charset=UTf-8\r\nContent-Length: {0}\r\n", responseBody.Length);
byte[] respOnseHeaderBytes= Encoding.UTF8.GetBytes(responseHeader);
byte[] respOnseBodyBytes= Encoding.UTF8.GetBytes(responseBody);
// 向客户端发送状态行
clientsockets.Send(responseStatusLineBytes);
// 向客户端发送回应头信息
clientsockets.Send(responseHeaderBytes);
// 发送头部和内容的空行
clientsockets.Send(new byte[] { 13, 10 });
// 想客户端发送主体部分
clientsockets.Send(responseBodyBytes);
// 断开连接
clientsockets.Close();
Console.ReadKey();
break;
}
// 关闭服务器
socket.Close();
}
}
}
(2)
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace TcpWebserver
{
class Program
{
static void Main(string[] args)
{
// 获得本机的Ip地址,即127.0.0.1
IPAddress localaddress = IPAddress.Loopback;
// 创建可以访问的断点,49155表示端口号,如果这里设置为0,表示使用一个由系统分配的空闲的端口号
IPEndPoint endpoint = new IPEndPoint(localaddress, 49155);
// 创建Tcp 监听器
TcpListener tcpListener = new TcpListener(endpoint);
// 启动监听
tcpListener.Start();
Console.WriteLine("Wait an connect Request...");
while (true)
{
// 等待客户连接
TcpClient client = tcpListener.AcceptTcpClient();
if (client.COnnected== true)
{
// 输出已经建立连接
Console.WriteLine("Created connection");
}
// 获得一个网络流对象
// 该网络流对象封装了Socket的输入和输出操作
// 此时通过对网络流对象进行写入来返回响应消息
// 通过对网络流对象进行读取来获得请求消息
NetworkStream netstream = client.GetStream();
// 把客户端的请求数据读入保存到一个数组中
byte[] buffer = new byte[2048];
int receivelength = netstream.Read(buffer, 0, 2048);
string requeststring = Encoding.UTF8.GetString(buffer, 0, receivelength);
// 在服务器端输出请求的消息
Console.WriteLine(requeststring);
// 服务器端做出相应内容
// 响应的状态行
string statusLine = "HTTP/1.1 200 OK\r\n";
byte[] respOnseStatusLineBytes= Encoding.UTF8.GetBytes(statusLine);
string respOnseBody= "
Welcome you
";
string respOnseHeader=
string.Format(
"Content-Type: text/html; charset=UTf-8\r\nContent-Length: {0}\r\n", responseBody.Length);
byte[] respOnseHeaderBytes= Encoding.UTF8.GetBytes(responseHeader);
byte[] respOnseBodyBytes= Encoding.UTF8.GetBytes(responseBody);
// 写入状态行信息
netstream.Write(responseStatusLineBytes, 0, responseStatusLineBytes.Length);
// 写入回应的头部
netstream.Write(responseHeaderBytes, 0, responseHeaderBytes.Length);
// 写入回应头部和内容之间的空行
netstream.Write(new byte[] { 13, 10 }, 0, 2);
// 写入回应的内容
netstream.Write(responseBodyBytes, 0, responseBodyBytes.Length);
// 关闭与客户端的连接
client.Close();
Console.ReadKey();
break;
}
// 关闭服务器
tcpListener.Stop();
}
}
}
启动exe,然后在网页输入地址http://localhost:49155/ 可以看到效果