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 ...
随机推荐
- UVa1025 (DAG上的dp)
这是紫书上的第一个dp哈. 1.状态定义:dp[i][j]---->到时刻i的时候(出发的时候时刻为0,约定时间为时刻time),从j号车站开往N号车站,在车站等待的最少的时间. 2.这个人当前 ...
- LeetCode Weekly Contest
链接:https://leetcode.com/contest/leetcode-weekly-contest-33/ A.Longest Harmonious Subsequence 思路:hash ...
- SQLiteDatabase的使用
新建DBHeler.JAVA package com.hixin.db; import java.util.ArrayList; import java.util.HashMap; import co ...
- mysql数据库小常识
什么是数据库? 计算机处理和存储的一切信息都是数据. 计算机系统中一种用于存储数据的程序. 一种:计算机系统中有很多种能够存取数据的程序. 他们各有特长和长处,有自己的适用范围. 存取:能够保存数据避 ...
- pycharm5工具免费分享及安装教程
好东西,就要分享,最近在捣鼓Python,所以就找个pycharm5工具,感觉挺好用的. 废话不多说了,所见即所得: 百度云盘分享:http://pan.baidu.com/s/1sk9k4Nj 密码 ...
- kairosdb + cassandra Setup
安装cassandra 下载 cassandra cassandra download mirror wget http://mirror.bit.edu.cn/apache/cassandra/2. ...
- Handling Class Imbalance with R and Caret - An Introduction
When faced with classification tasks in the real world, it can be challenging to deal with an outcom ...
- 深入tornado中的http1connection
前言 tornado中http1connection文件的作用极其重要,他实现了http1.x协议. 本模块基于gen模块和iostream模块实现异步的处理请求或者响应. 阅读本文需要一些基础的ht ...
- web前端面试集锦(自己搜集的,如有错误请不吝赐教)
css 1 浏览器兼容性 CSS hack(针对IE6-,IE7,IE8,IE9以及其他浏览器) ‘ * ’ : 所有的IE浏览器都能识别 说明:在标准模式中 “-″减号是IE6专有的hack “\9 ...
- centos nginx-1.10.3 安装
wget http://nginx.org/download/nginx-1.13.1.tar.gz nginx 依赖 pcre 库,要先安装pcre,因为nginx 要在rewrite 要解析正则表 ...