http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 7117|回复: 4

[基类库] Socket做代理服务器为什么响应很慢

[复制链接]
发表于 2014-6-26 15:35:03 | 显示全部楼层 |阅读模式
1金钱
无论资源多小,下面这个循环都是要执行15秒左右。
while(rBytes>0){        rBytes=IPsocket.Receive(RecvBytes,RecvBytes.Length,0);    strRetPage = strRetPage + System.Text.Encoding.Default.GetString(RecvBytes, 0, rBytes);   }
-------------------------------------------------------------------


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Text.RegularExpressions;
namespace WebProxy
{

         class Proxy
    {
             static void Main(string[] args)
        {
const  int  port=9999;
TcpListener tcplistener = new TcpListener(System.Net.IPAddress.Parse("127.0.0.1"), 9999);
Console.WriteLine("侦听端口号:"+port.ToString());
tcplistener.Start();
//侦听端口号
while (true)
{
Socket  socket=tcplistener.AcceptSocket();
//并获取传送和接收数据的Socket实例
Proxy  proxy=new Proxy(socket);
//Proxy类实例化
Thread  thread=new Thread (new ThreadStart(proxy.Run));
//创建线程
thread.Start();
//启动线程
}





        }
       public Proxy(Socket socket) {
          // TODO 在此处添加构造函数逻辑
           this.clientSocket=socket; }

       Socket clientSocket;
       Byte[] read = new byte[1024];
       //定义一个空间,存储来自客户端请求数据包
       Byte[] Buffer = null;
       //设定编码
       Byte[] RecvBytes = new Byte[4096];


    public void Run()
{
string clientmessage="";
//存放来自客户端的HTTP请求字符串
string URL="";
//存放解析出地址请求信息
int  bytes=ReadMessage(read,ref clientSocket, ref  clientmessage);
if(bytes == 0)
{
  return ;
}
int index1 = clientmessage.IndexOf(' ');
int index2 = clientmessage.IndexOf(' ', index1 + 1);
if ((index1 == -1) || (index2 == -1))
{
    throw new IOException();
}
string part1 = clientmessage.Substring(index1 + 1, index2 - index1);
int index3 = part1.IndexOf('/', index1 + 8);
int index4 = part1.IndexOf(' ', index1 + 8);
int index5 = index4 - index3;
URL = part1.Substring(index1 + 4, (part1.Length - index5) - 8);

//  URL = Regex.Match(URL, @"(?<=://)[a-zA-Z\.0-9]+(?=\/)").Value.ToString();
  Console.WriteLine("远程主机名:" + URL);

try{
   IPHostEntry IPHost =Dns.GetHostEntry(URL);
Console.WriteLine("远程主机名:"+IPHost.HostName);
string[] aliases=IPHost.Aliases;
IPAddress[] address=IPHost.AddressList;


Console.WriteLine("Web服务器IP地址:"+address[0]);
//解析出要访问的服务器地址

//解析出要访问的服务器地址
//IPEndPoint

   ;
   IPEndPoint ipe = new IPEndPoint(address[0], 80);//用指定的端口和
  Socket   IPsocket=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);



//创建连接Web服务端的Socke对象
  IPsocket.Connect(ipe);

//Socket连接WEB服务器
if(IPsocket.Connected)
Console.WriteLine("Socket正确连接!");
String GET =clientmessage;
Byte[] ByteGet = System.Text.Encoding.Default.GetBytes(GET);
IPsocket.Send(ByteGet,ByteGet.Length,0);
//代理访问软件对服务器端传送HTTP请求命令
Int32  rBytes=IPsocket.Receive(RecvBytes,RecvBytes.Length,0);
//代理访问软件接收来自WEB服务器端的反馈信息
Console.WriteLine("接收字节数:"+rBytes.ToString());
String  strRetPage=null;
strRetPage = strRetPage + System.Text.Encoding.Default.GetString(RecvBytes, 0, rBytes);
while(rBytes>0)
{
        rBytes=IPsocket.Receive(RecvBytes,RecvBytes.Length,0);
    strRetPage = strRetPage + System.Text.Encoding.Default.GetString(RecvBytes, 0, rBytes);

}
IPsocket.Shutdown(SocketShutdown.Both);
IPsocket.Close();
SendMessage(clientSocket,strRetPage);
//代理服务软件往客户端传送收到的信息
}
catch(Exception exc2)
{
    Console.WriteLine(exc2.ToString());

  //  throw exc2 ;
}

}


//接收客户端的HTTP请求数据
private  int  ReadMessage(byte[] ByteArray,ref  Socket s, ref String clientmessage)
{
int bytes=s.Receive(ByteArray,1024,0);
string  messagefromclient=Encoding.ASCII.GetString(ByteArray);
clientmessage=(String)messagefromclient;
Console.WriteLine("发出的信息:" + clientmessage);
return bytes;
}

//传送从WEB服务器反馈的数据到客户端
private  void SendMessage(Socket s,string  message)
{
Buffer =new Byte[message.Length];
int length = System.Text.Encoding.Unicode.GetBytes(message, 0, message.Length, Buffer, 0);
Console.WriteLine("传送字节数:"+length.ToString());
Console.WriteLine("收到的信息:"+Buffer.ToString());
//s.Send(Buffer,length,0);
byte[] bytes1 = Encoding.GetEncoding("GB2312").GetBytes(message);
s.Send(bytes1);
}
    }
}




1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
发表于 2014-6-26 15:52:32 | 显示全部楼层
慢会不会是网速或者是代理慢
回复

使用道具 举报

 楼主| 发表于 2014-6-26 16:11:14 | 显示全部楼层
站长苏飞 发表于 2014-6-26 15:52
慢会不会是网速或者是代理慢

这个代理慢,用Fiddler是很快的,人感觉不出来。
回复

使用道具 举报

发表于 2014-6-26 22:02:55 | 显示全部楼层
1.建议对Main函数、Proxy构造函数、Run函数下断点,调试慢在哪里
2.对可能出现拖慢速度的地方加StopWatch看看,检查慢的原因
回复

使用道具 举报

发表于 2015-2-1 20:00:57 | 显示全部楼层
异步传送数据,你的是同步的
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-4-29 21:55

© 2014-2021

快速回复 返回顶部 返回列表