在C#网络编程中使用匿名Socks5代理

    我们知道编程的时候使用代理服务器并不是一件爽心悦目的事情,可是很多时候又是迫不得已的emsad.gif。微软在.NET Framework得System.Net名称空间里给我们提供了一个WebProxy类,不过这是一个http代理,这种代理使用起来受限太多,很不方便。如果我们需要访问更多的网络服务,socks代理是一个理想的选择。下面是C#编写的一个匿名socks5代理使用示例:

ContractedBlock.gif ExpandedBlockStart.gif 连接匿名socks5服务器代码 #region 连接匿名socks5服务器代码
InBlock.gif
private bool ConnectProxyServer(string strRemoteHost, int iRemotePort, Socket sProxyServer)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif    
//构造Socks5代理服务器第一连接头(无用户名密码)
InBlock.gif
    byte[] bySock5Send = new Byte[10];
InBlock.gif    bySock5Send[
0= 5;
InBlock.gif    bySock5Send[
1= 1;
InBlock.gif    bySock5Send[
2= 0;
InBlock.gif
InBlock.gif    
//发送Socks5代理第一次连接信息
InBlock.gif
    sProxyServer.Send(bySock5Send, 3, SocketFlags.None);
InBlock.gif
InBlock.gif    
byte[] bySock5Receive = new byte[10];
InBlock.gif    
int iRecCount = sProxyServer.Receive(bySock5Receive, bySock5Receive.Length, SocketFlags.None);
InBlock.gif
InBlock.gif    
if ( iRecCount<2 ) 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        sProxyServer.Close();
InBlock.gif        
throw new Exception("不能获得代理服务器正确响应。");
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
if ( bySock5Receive[0]!=5 || ( bySock5Receive[1]!=0 && bySock5Receive[1]!=2 ) )
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        sProxyServer.Close(); 
InBlock.gif        
throw new Exception("代理服务其返回的响应错误。");
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
if ( bySock5Receive[1]==0 )
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        bySock5Send[
0= 5;
InBlock.gif        bySock5Send[
1= 1;
InBlock.gif        bySock5Send[
2= 0;
InBlock.gif        bySock5Send[
3= 1;
InBlock.gif
InBlock.gif        IPAddress ipAdd 
= Dns.Resolve(strRemoteHost).AddressList[0];
InBlock.gif        
string strIp = ipAdd.ToString();
ExpandedSubBlockStart.gifContractedSubBlock.gif        
string []strAryTemp = strIp.Split(new char[] dot.gif{'.'});
InBlock.gif        bySock5Send[
4= Convert.ToByte(strAryTemp[0]);
InBlock.gif        bySock5Send[
5= Convert.ToByte(strAryTemp[1]);
InBlock.gif        bySock5Send[
6= Convert.ToByte(strAryTemp[2]);
InBlock.gif        bySock5Send[
7= Convert.ToByte(strAryTemp[3]);
InBlock.gif
InBlock.gif        bySock5Send[
8= (byte)(iRemotePort/256);
InBlock.gif        bySock5Send[
9= (byte)(iRemotePort%256);
InBlock.gif
InBlock.gif        sProxyServer.Send(bySock5Send, bySock5Send.Length, SocketFlags.None);
InBlock.gif        iRecCount 
= sProxyServer.Receive(bySock5Receive, bySock5Receive.Length, SocketFlags.None);
InBlock.gif
InBlock.gif        
if ( bySock5Receive[0]!=5 || bySock5Receive[1]!=0 )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            sProxyServer.Close();
InBlock.gif            
throw new Exception("第二次连接Socks5代理返回数据出错。");
ExpandedSubBlockEnd.gif        }

InBlock.gif        return true ;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
if ( bySock5Receive[1]==2 ) 
InBlock.gif            
throw new Exception("代理服务器需要进行身份确认。");
InBlock.gif        
else return false ;
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif
#endregion

   关于socks5代理的详细文档,可以参看RFC 1928

   使用上面的代码需要有一个匿名的socks5代理服务器,然后调用:

None.gif private  Socket GetSocket( string  strIpAdd,  int  iPort)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    IPAddress hostadd 
= Dns.Resolve(strIpAdd).AddressList[0];
InBlock.gif    IPEndPoint EPhost 
= new IPEndPoint(hostadd, iPort);
InBlock.gif    Socket s 
= new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
InBlock.gif    s.Connect(EPhost);
InBlock.gif    
return s ;
ExpandedBlockEnd.gif}

None.gif
None.gifsRemoteHost 
=  GetSocket(proxyHost, proxyProt);
None.gifConnectProxyServer(
"61.135.134.24"80
, sRemoteHost);
None.gif

   如果您需要使用非匿名的socks5代理,把RFC 1928后面关于用户验证的部分实现就可以了,由于我使用的都是匿名的socks5代理,所以没有做这个部分得工作emembarrassed.gif

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值