c# ProxyServer 代理服务器 不是很稳定
/**
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa
Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/
// Proxy.cs -- Implements a multi-threaded Web proxy server
//
// Compile this program with the following command line:
// C:>csc Proxy.cs
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.IO;
using System.Threading;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
namespace nsProxyServer
{
public class ProxyServer
{
static public void Main(string[] args)
{
int Port = 3125;
if (args.Length > 0)
{
try
{
Port = Convert.ToInt32(args[0]);
}
catch
{
Console.WriteLine("Please enter a port number.");
return;
}
}
try
{
// Create a listener for the proxy port
TcpListener sockServer = new TcpListener(Port);
sockServer.Start();
while (true)
{
// Accept connections on the proxy port.
Socket socket = sockServer.AcceptSocket();
// When AcceptSocket returns, it means there is a connection. Create
// an instance of the proxy server class and start a thread running.
clsProxyConnection proxy = new clsProxyConnection(socket);
Thread thrd = new Thread(new ThreadStart(proxy.Run));
thrd.Start();
// While the thread is running, the main program thread will loop around
// and listen for the next connection request.
}
}
catch (IOException e)
{
Console.WriteLine(e.Message);
}
}
}
class clsProxyConnection
{
public clsProxyConnection(Socket sockClient)
{
m_sockClient = sockClient;
}
Socket m_sockClient; //, m_sockServer;
Byte[] readBuf = new Byte[1024];
Byte[] buffer = null;
Encoding ASCII = Encoding.ASCII;
private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
{
return true; //总是接受
}
public void Run()
{
string strFromClient = "";
try
{
// Read the incoming text on the socket/
int bytes = ReadMessage(m_sockClient,
readBuf, ref strFromClient);
// If it's empty, it's an error, so just return.
// This will termiate the thread.
if (bytes == 0)
return;
// Get the URL for the connection. The client browser sends a GET command
// followed by a space, then the URL, then and identifer for the HTTP version.
// Extract the URL as the string betweeen the spaces.
int index1 = strFromClient.IndexOf(' ');
int index2 = strFromClient.IndexOf(' ', index1 + 1);
string strClientConnection =
strFromClient.Substring(index1 + 1, index2 - index1);
if ((index1 < 0) || (index2 < 0))
{
throw (new IOException());
}
// Write a messsage that we are connecting.
Console.WriteLine("Connecting to Site " +
strClientConnection);
Console.WriteLine("Connection from " +
m_sockClient.RemoteEndPoint);
// Create a WebRequest object.
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
HttpWebRequest req = (HttpWebRequest)WebRequest.Create
(strClientConnection);
req.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727; InfoPath.2; .NET CLR 3.0.04506; .NET CLR 3.5.21019)";
// req.Headers.Add("UserAgent", "Mozilla/5.0 (Windows NT 6.1; rv:40.0) Gecko/20100101 Firefox/40.0");
// Get the response from the Web site.
WebResponse response = req.GetResponse();
int BytesRead = 0;
Byte[] Buffer = new Byte[32];
int BytesSent = 0;
// Create a response stream object.
Stream ResponseStream = response.GetResponseStream();
// Read the response into a buffer.
BytesRead = ResponseStream.Read(Buffer, 0, 32);
StringBuilder strResponse = new StringBuilder("");
while (BytesRead != 0)
{
// Pass the response back to the client
strResponse.Append(Encoding.ASCII.GetString(Buffer,
0, BytesRead));
m_sockClient.Send(Buffer, BytesRead, 0);
BytesSent += BytesRead;
// Read the next part of the response
BytesRead = ResponseStream.Read(Buffer, 0, 32);
}
}
catch (FileNotFoundException e)
{
SendErrorPage(404, "File Not Found", e.Message);
}
catch (IOException e)
{
SendErrorPage(503, "Service not available", e.Message);
}
catch (Exception e)
{
SendErrorPage(404, "File Not Found", e.Message);
Console.WriteLine(e.StackTrace);
Console.WriteLine(e.Message);
}
finally
{
// Disconnect and close the socket.
if (m_sockClient != null)
{
if (m_sockClient.Connected)
{
m_sockClient.Close();
}
}
}
// Returning from this method will terminate the thread.
}
// Write an error response to the client.
void SendErrorPage(int status, string strReason, string strText)
{
SendMessage(m_sockClient, "HTTP/1.0" + " " +
status + " " + strReason + "\r\n");
SendMessage(m_sockClient, "Content-Type: text/plain" + "\r\n");
SendMessage(m_sockClient, "Proxy-Connection: close" + "\r\n");
SendMessage(m_sockClient, "\r\n");
SendMessage(m_sockClient, status + " " + strReason);
SendMessage(m_sockClient, strText);
}
// Send a string to a socket.
void SendMessage(Socket sock, string strMessage)
{
buffer = new Byte[strMessage.Length + 1];
int len = ASCII.GetBytes(strMessage.ToCharArray(),
0, strMessage.Length, buffer, 0);
try
{
sock.Send(buffer, len, 0);
}
catch (Exception e) {
Console.WriteLine(e.Message);
}
}
// Read a string from a socket.
int ReadMessage(Socket sock, byte[] buf, ref string strMessage)
{
int iBytes = sock.Receive(buf, 1024, 0);
strMessage = Encoding.ASCII.GetString(buf);
return (iBytes);
}
}
}
c# ProxyServer 代理服务器 不是很稳定的更多相关文章
- proxy server 代理服务器
有时候,我觉得自己需要去搞明白.搞清楚一个概念,帮我打通一下自己的知识体系,或者说,尝试联络起来. 1. 简介 突破自身IP限制,访问国外站点. 访问单位或者团体内部资源. 突破中国电信的IP封锁. ...
- 搞JAVA在北京月薪15K的朋友来到厦门却很难找到工作
朋友是搞JAVA开发的,从北京来.来前朋友们都感觉他在厦门应该很快就能找到工作,因为厦门的IT人员很缺. 没想到来厦门大概半个多月了,到现在都还没着落.面试单位每周基本只有两家,而且面试的感觉都说不错 ...
- HttpClient支持使用代理服务器以及身份认证
HttpClient Authentication Doument: http://hc.apache.org/httpclient-3.x/authentication.html HttpClien ...
- 国外稳定的免费PHP空间byethost.com
byethost.com是一个老牌的免费空间商,从2006年起就開始提供免费空间了,其免费服务很稳定(看完下文你就知道有多稳定了). 提供5.5G的免费空间,200G的月流量,能够绑定50个域名,也能 ...
- 跨域问题实践总结!下( [HTML5] postMessage+服务器端(反向代理服务器+CORS Cross-Origin Resource Sharing))
4. [HTML5] postMessage 问题: 对于跨域问题,研究了一下html5的postMessage,写了代码测试了一下,感觉html5新功能就是好用啊.此文仅使用html5的新特性pos ...
- wine安装稳定使用falsh播放器
1安装wine,wine安装使用网上自行查找 2.安装flash播放器.exe 下载附件的falsh播放相关.tar.gz,解压后得到 Flash.ocx (flash10 for windows的插 ...
- idea 启动 springBoot debug很慢,正常启动很快是什么原因
说实话,从我开始使用springboot框架以来,就一直遇到这个问题,我刚把项目从SSM框架转成 spring boot 框架时,就发现有时候启动项目特别慢,有时候特别快,当时觉得特别奇怪,但也一直没 ...
- DIOCP开源项目-DIOCP3的重生和稳定版本发布
DIOCP3的重生 从开始写DIOCP到现在已经有一年多的时间了,最近两个月以来一直有个想法做个 30 * 24 稳定的企业服务端架构,让程序员专注于逻辑实现就好.虽然DIOCP到现在通讯层已经很稳定 ...
- 让Selenium稳定运行的技巧
Selenium简介 Selenium是非常流行的Web自动化测试工具.它具有自动化测试用例制作简单,支持多种浏览器和不同的操作系统等优点. Selenium脚本不稳定的问题 有很多时候Seleniu ...
随机推荐
- Java线程安全性中的对象发布和逸出
发布(Publish)和逸出(Escape)这两个概念倒是第一次听说,不过它在实际当中却十分常见,这和Java并发编程的线程安全性就很大的关系. 什么是发布?简单来说就是提供一个对象的引用给作用域之外 ...
- Samba文件共享服务
Samba起源: 早期网络想要在不同主机之间共享文件大多要用FTP协议来传输,但FTP协议仅能做到传输文件却不能直接修改对方主机的资料数据,这样确实不太方便,于是便出现了NFS开源文件共享程序:NFS ...
- SVN分支/合并原理及最佳实践
转自:http://blog.csdn.net/e3002/article/details/21469437 使用svn几年了,一直对分支和合并敬而远之,一来是因为分支的管理不该我操心,二来即使涉及到 ...
- 2.Java 加解密技术系列之 MD5
Java 加解密技术系列之 MD5 序 背景 正文 结束语 序 上一篇文章中,介绍了最基础的编码方式 — — BASE64,也简单的提了一下编码的原理.这篇文章继续加解密的系列,当然也是介绍比较基础的 ...
- spring service层单元测试
service层测试较简单,目前大多数测试主要是针对public方法进行的.依据测试方法划分,可以分为两种:基于mock的隔离测试和基于dbunit的普通测试. mock隔离测试 配置pom.xml ...
- 解密Lazy<T>
1.Lazy<T>的使用 无意间看到一段代码,在创建对象的时候使用了Lazy,顾名思义Lazy肯定是延迟加载,那么它具体是如何创建对象,什么时候创建对象了? 先看这段示列代码: publi ...
- Java IO流之对象流
对象流 1.1对象流简介 1.2对象流分类 输入流字节流处理流:ObjectInputStream,将序列化以后的字节存储到本地文件 输出流字节流处理流:ObjectOutputStream 1.3序 ...
- Eclipse用法:自动生成get和set方法
方法一 Java的类中,除了常量声明为静态且公有的,一般的对象数据作用域,都是声明为私有的.这样做能保护对象的属性不会被随意改变,调试的时候也会方便很多:在类的公有方法中大一个调用栈就能看到哪里改 ...
- MQ产品比较-ActiveMQ-RocketMQ
几种MQ产品说明: ZeroMQ : 扩展性好,开发比较灵活,采用C语言实现,实际上他只是一个socket库的重新封装,如果我们做为消息队列使用,需要开发大量的代码 RabbitMQ :结合erla ...
- 预约会议sql
CREATE proc sp_MeetingCheck_Test @serialno varchar(max)='', ---- 主档serialno @title ...