using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Net.Sockets;
using System.Net;
using System.IO;
using System.Security.Cryptography;
using System.Text.RegularExpressions;namespace JK
{
public partial class Form1 : Form
{
private HttpServer server = new HttpServer();
public Form1()
{
InitializeComponent(); server.Start();
lblZhuangTai.Text = "状态:正在监控..."; } private void btnTingZhi_Click(object sender, EventArgs e)
{
server.Stop();
lblZhuangTai.Text = "状态:停止监控";
} private void btnKaiShiJianKong_Click(object sender, EventArgs e)
{
server.Start();
lblZhuangTai.Text = "状态:正在监控...";
} public class HttpServer
{
TcpListener _listener = null;
ManualResetEvent _waitEvent = null;
Thread t;
public void Start()
{
_waitEvent = new ManualResetEvent(false);
//开始侦听所有IP的9887端口
_listener = new TcpListener(IPAddress.Any, 9887);
_listener.Start(); _waitEvent.Reset(); //开始处理客户端连接请求
t = new Thread(new ThreadStart(ThreadDeamon));
t.IsBackground = true;
t.Start();
} private void ThreadDeamon()
{
while (!_waitEvent.WaitOne(1000, false))
{
//如果有挂起的请求
if (_listener.Pending())
{
//接收一个请求
TcpClient client = _listener.AcceptTcpClient();
//得到客户端IP地址
string ip = ((IPEndPoint)client.Client.RemoteEndPoint).Address.ToString();
//Console.WriteLine(String.Format("来自: {0}", ip));
//获得数据
NetworkStream ns = client.GetStream();
MemoryStream ms = new MemoryStream();
byte[] buffer = new byte[1024];
int read = 0; while (ns.DataAvailable)
{
read = ns.Read(buffer, 0, buffer.Length);
if (read > 0)
{
ms.Write(buffer, 0, read); //如果网络流中有可用的数据那么就写如到内存留
}
else
{
break;
}
} //输出客户端请求的数据到控制台
byte[] data = ms.ToArray();
string recivedString = Encoding.UTF8.GetString(data); //Console.WriteLine();
//Console.WriteLine(recivedString);
//Console.WriteLine(); //解析参数,即Http里的QueryString
ms.Seek(0, SeekOrigin.Begin); StreamReader sr = new StreamReader(ms);
string headerline = sr.ReadLine(); sr.Close(); //获取网址?号后面的字符传
int start = headerline.IndexOf("?") + 1;
int length = headerline.IndexOf("HTTP/1.1") - start;
string[] args = headerline.Substring(start, length).Trim().Split(new char[] { '&' }); Dictionary<string, string> queryString = new Dictionary<string, string>();
foreach (string arg in args)
{
string[] p = arg.Split(new char[] { '*' });
if (p.Length == 2 && !queryString.ContainsKey(p[0]))
queryString.Add(p[0], p[1]);
}
if (!queryString.ContainsKey("signstr"))
return;
string strRet = "false";
//根据参数执行不同的任务。
//省略 //执行任务完成后,返回执行结果。注意:需要遵守Html协议 string ret = string.Format(RESPONSE, DateTime.Now.ToUniversalTime(), strRet.Length, strRet);
byte[] sendData = Encoding.UTF8.GetBytes(ret);
ns.Write(sendData, 0, sendData.Length);
//ns.Close();
client.Close();
//Console.WriteLine(String.Format("client {0} disconnected.", ip));
}
}
} const string RESPONSE = @"HTTP/1.1 200 OK
Date: {0}
Server: IYOND-SERVER
Content-Type: text/html; charset=UTF-8
Content-Length: {1}
Connection: close{2}
"; public void Stop()
{
_waitEvent.Set();
_listener.Stop();
t.Interrupt();
//System.Diagnostics.Process.GetCurrentProcess().Kill();
} }
}
}帮我看看有没有什么问题``谢谢``
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Net.Sockets;
using System.Net;
using System.IO;
using System.Security.Cryptography;
using System.Text.RegularExpressions;namespace JK
{
public partial class Form1 : Form
{
private HttpServer server = new HttpServer();
public Form1()
{
InitializeComponent(); server.Start();
lblZhuangTai.Text = "状态:正在监控..."; } private void btnTingZhi_Click(object sender, EventArgs e)
{
server.Stop();
lblZhuangTai.Text = "状态:停止监控";
} private void btnKaiShiJianKong_Click(object sender, EventArgs e)
{
server.Start();
lblZhuangTai.Text = "状态:正在监控...";
} public class HttpServer
{
TcpListener _listener = null;
ManualResetEvent _waitEvent = null;
Thread t;
public void Start()
{
_waitEvent = new ManualResetEvent(false);
//开始侦听所有IP的9887端口
_listener = new TcpListener(IPAddress.Any, 9887);
_listener.Start(); _waitEvent.Reset(); //开始处理客户端连接请求
t = new Thread(new ThreadStart(ThreadDeamon));
t.IsBackground = true;
t.Start();
} private void ThreadDeamon()
{
while (!_waitEvent.WaitOne(1000, false))
{
//如果有挂起的请求
if (_listener.Pending())
{
//接收一个请求
TcpClient client = _listener.AcceptTcpClient();
//得到客户端IP地址
string ip = ((IPEndPoint)client.Client.RemoteEndPoint).Address.ToString();
//Console.WriteLine(String.Format("来自: {0}", ip));
//获得数据
NetworkStream ns = client.GetStream();
MemoryStream ms = new MemoryStream();
byte[] buffer = new byte[1024];
int read = 0; while (ns.DataAvailable)
{
read = ns.Read(buffer, 0, buffer.Length);
if (read > 0)
{
ms.Write(buffer, 0, read); //如果网络流中有可用的数据那么就写如到内存留
}
else
{
break;
}
} //输出客户端请求的数据到控制台
byte[] data = ms.ToArray();
string recivedString = Encoding.UTF8.GetString(data); //Console.WriteLine();
//Console.WriteLine(recivedString);
//Console.WriteLine(); //解析参数,即Http里的QueryString
ms.Seek(0, SeekOrigin.Begin); StreamReader sr = new StreamReader(ms);
string headerline = sr.ReadLine(); sr.Close(); //获取网址?号后面的字符传
int start = headerline.IndexOf("?") + 1;
int length = headerline.IndexOf("HTTP/1.1") - start;
string[] args = headerline.Substring(start, length).Trim().Split(new char[] { '&' }); Dictionary<string, string> queryString = new Dictionary<string, string>();
foreach (string arg in args)
{
string[] p = arg.Split(new char[] { '*' });
if (p.Length == 2 && !queryString.ContainsKey(p[0]))
queryString.Add(p[0], p[1]);
}
if (!queryString.ContainsKey("signstr"))
return;
string strRet = "false";
//根据参数执行不同的任务。
//省略 //执行任务完成后,返回执行结果。注意:需要遵守Html协议 string ret = string.Format(RESPONSE, DateTime.Now.ToUniversalTime(), strRet.Length, strRet);
byte[] sendData = Encoding.UTF8.GetBytes(ret);
ns.Write(sendData, 0, sendData.Length);
//ns.Close();
client.Close();
//Console.WriteLine(String.Format("client {0} disconnected.", ip));
}
}
} const string RESPONSE = @"HTTP/1.1 200 OK
Date: {0}
Server: IYOND-SERVER
Content-Type: text/html; charset=UTF-8
Content-Length: {1}
Connection: close{2}
"; public void Stop()
{
_waitEvent.Set();
_listener.Stop();
t.Interrupt();
//System.Diagnostics.Process.GetCurrentProcess().Kill();
} }
}
}帮我看看有没有什么问题``谢谢``
解决方案 »
- 深度内存整理问题,求代码!
- 迅雷软件运行时,桌面右下角时常有广告弹出,请问迅雷是怎样实现广告的更新的,我看他的广告不同时段的广告不同!
- PropertyInfo.setvalue 的问题。代码如下,在线等很急
- 怎樣在Report services 中繪制柏拉圖?
- 连接数据库问题
- 请教,当鼠标指向一个可复制的汉字串(比如文本文件中的串,或网页上的串)时,这个汉字串如何赋给一个程序中的字符串变量。
- .net开发、调用DCOM的问题
- 2014关棍节福利呀。蛋炒饭。。。。
- 在xp下安装rc3英文版有错误,请帮我解决
- C# 多个自定义控件 怎么互不影响
- 请教:C#项目中隐藏显示窗体的问题
- 使用oledbadapter.update将dataGrid的数据,更新到access中
有人知道我的是怎么回事吗```