Proxy C++/CLI HTTP代理问题

Proxy C++/CLI HTTP代理问题,proxy,c++-cli,Proxy,C++ Cli,我正在努力制作一个小型HTTP代理服务器,我可以用它将所有通信保存到一个文件中。鉴于我在这方面没有任何经验,我使用了codeproject.com中的一个类和一些相关的代码来启动它。它是用旧的CLI语法编写的,所以我对它进行了转换。我不能让它工作,所以我添加了更多的代码使它工作线程等,现在它的工作排序。基本上,它从我刚刚配置的Mozilla Firefox客户端接收到一些东西,通过这个代理路由它的连接,然后路由到google.com。在它将Firefox的数据发送到google之后,收到一个响应

我正在努力制作一个小型HTTP代理服务器,我可以用它将所有通信保存到一个文件中。鉴于我在这方面没有任何经验,我使用了codeproject.com中的一个类和一些相关的代码来启动它。它是用旧的CLI语法编写的,所以我对它进行了转换。我不能让它工作,所以我添加了更多的代码使它工作线程等,现在它的工作排序。基本上,它从我刚刚配置的Mozilla Firefox客户端接收到一些东西,通过这个代理路由它的连接,然后路由到google.com。在它将Firefox的数据发送到google之后,收到一个响应,并将其发送到Firefox。这很好,但是代理无法从Firefox接收任何数据。它只是在Sleep50部分循环。不管怎样,代码如下:

ProxyTest.cpp:

CHTTPProxy.h,来自,经过大量修改:

#using <mscorlib.dll>
#using <SYSTEM.DLL>
using namespace System;
using System::Net::Sockets::TcpClient;
using System::String;
using System::Exception;
using System::Net::Sockets::NetworkStream;
#include <stdio.h>

ref class CHttpProxy 
{ 
public: 
    CHttpProxy(System::String ^ szHost, int port); 
    System::String ^ m_host; 
    int m_port; 
    void SendToServer(array<unsigned char> ^ Packet, System::Net::Sockets::NetworkStream ^ sendstr); 
};

CHttpProxy::CHttpProxy(System::String ^ szHost, int port)
{
    m_host = gcnew System::String(szHost);
    m_port = port;
}

void CHttpProxy::SendToServer(array<unsigned char> ^ Packet, System::Net::Sockets::NetworkStream ^ sendstr)
{
    TcpClient ^ tcpclnt = gcnew TcpClient();

    try
    {
        tcpclnt->Connect(m_host,m_port); 
    }
    catch (Exception ^ e )
    {
        Console::WriteLine(e->ToString());
        return;
    }

    // Send it
    if ( tcpclnt )
    {
        NetworkStream ^ networkStream;

        networkStream = tcpclnt->GetStream();

        int size = Packet->Length;        
        networkStream->Write(Packet, 0, size);
        array<unsigned char> ^ bytes = gcnew array<unsigned char>(tcpclnt->ReceiveBufferSize);
        char * bytess = new char[tcpclnt->ReceiveBufferSize];
        Sleep(500); //Wait for responce 
        do {
            unsigned int k = networkStream->Read(bytes, 0, (int)tcpclnt->ReceiveBufferSize); //Read from google

            for(unsigned int i=0; i<k; i++) {
                bytess[i] = bytes[i];
                if (bytess[i] == 0) bytess[i] = ' '; //Dont terminate the string
                if (bytess[i] < 8) bytess[i] = ' '; //Somethings making the computer beep, and its not 7?!?!
            };
            Console::WriteLine("\n\nAbove packet sent to google. Google Packet Received:\n"+gcnew System::String(bytess)); 

            sendstr->Write(bytes,0,k); //Send it to mozilla

            Console::WriteLine("\n\nAbove packet sent to client..."); 
            //Sleep(1000);

        } while(networkStream->DataAvailable);

        delete [] bytess;
    }
    return;
}

任何帮助都将不胜感激,我已经试了好几个小时了。很抱歉nobugz的缩进,现在已经修好了,只有你一个人,这不是学校的作业:而且,这是3年前写的,所以我几乎不认为这有什么关系,即使这是一个很好的观点你有没有解决过这个问题?@Per Lundberg不幸没有,对不起…@PerLundberg评论:P@n00b现在不必吝啬:P@Per伦德伯格:只有你,这不是学校作业:而且,这是3年前写的,所以我认为即使这是一个好的观点也没什么关系!:你有没有解决过这个问题?@Per Lundberg不幸没有,对不起…@PerLundberg评论:P@n00b现在不必吝啬了:P
#using <mscorlib.dll>
#using <SYSTEM.DLL>
using namespace System;
using System::Net::Sockets::TcpClient;
using System::String;
using System::Exception;
using System::Net::Sockets::NetworkStream;
#include <stdio.h>

ref class CHttpProxy 
{ 
public: 
    CHttpProxy(System::String ^ szHost, int port); 
    System::String ^ m_host; 
    int m_port; 
    void SendToServer(array<unsigned char> ^ Packet, System::Net::Sockets::NetworkStream ^ sendstr); 
};

CHttpProxy::CHttpProxy(System::String ^ szHost, int port)
{
    m_host = gcnew System::String(szHost);
    m_port = port;
}

void CHttpProxy::SendToServer(array<unsigned char> ^ Packet, System::Net::Sockets::NetworkStream ^ sendstr)
{
    TcpClient ^ tcpclnt = gcnew TcpClient();

    try
    {
        tcpclnt->Connect(m_host,m_port); 
    }
    catch (Exception ^ e )
    {
        Console::WriteLine(e->ToString());
        return;
    }

    // Send it
    if ( tcpclnt )
    {
        NetworkStream ^ networkStream;

        networkStream = tcpclnt->GetStream();

        int size = Packet->Length;        
        networkStream->Write(Packet, 0, size);
        array<unsigned char> ^ bytes = gcnew array<unsigned char>(tcpclnt->ReceiveBufferSize);
        char * bytess = new char[tcpclnt->ReceiveBufferSize];
        Sleep(500); //Wait for responce 
        do {
            unsigned int k = networkStream->Read(bytes, 0, (int)tcpclnt->ReceiveBufferSize); //Read from google

            for(unsigned int i=0; i<k; i++) {
                bytess[i] = bytes[i];
                if (bytess[i] == 0) bytess[i] = ' '; //Dont terminate the string
                if (bytess[i] < 8) bytess[i] = ' '; //Somethings making the computer beep, and its not 7?!?!
            };
            Console::WriteLine("\n\nAbove packet sent to google. Google Packet Received:\n"+gcnew System::String(bytess)); 

            sendstr->Write(bytes,0,k); //Send it to mozilla

            Console::WriteLine("\n\nAbove packet sent to client..."); 
            //Sleep(1000);

        } while(networkStream->DataAvailable);

        delete [] bytess;
    }
    return;
}