我写了个转发器,在IE中设置代理地址为我的转发器地址,转发器把IE的请求数据转发给代理服务器,把代理服务器返回的数据转发给IE.
当在IE中打开www.yahoo.com,IE中已经完全显示网页时突然刷的一下又没了,显示“该页无法显示”。请问这是怎么回事?
我估计是要返回状态码之类的东西给IE,我不知道是什么

解决方案 »

  1.   

    你的转发是要实现http proxy?
    参看http 的rfc,如果是成功,你需要返回success http200 ok 字样,如果错误,返回http404,...
      

  2.   

    #include "stdafx.h"
    #include "Proxy.h"
    #include <winsock2.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <afxmt.h>
    //#include <afx.h>#ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif/////////////////////////////////////////////////////////////////////////////
    // The one and only application object#define PROXYPORT    5060    //Proxy Listen Port
    #define BUFSIZE   10240      //Buffer size
    CWinApp theApp;using namespace std;UINT ProxyToServer(LPVOID pParam);
    UINT UserToProxyThread(void *pParam);struct SocketPair{
    SOCKET  user_proxy;      //socket : local machine to proxy server
    SOCKET  proxy_server;    //socket : proxy sever to remote server
    BOOL    IsUser_ProxyClosed; // status of local machine to proxy server
    BOOL    IsProxy_ServerClosed; // status of proxy server to remote server
    HANDLE  User_SvrOK;    // status of setup connection between proxy server and remote server
    CCriticalSection cs;
    };SOCKET    gListen_Socket;   
    //CRITICAL_SECTION csMemDebug2;VOID DebugPrint(LPCSTR lpFormat, ...) // 打印调试信息
    {
    static CStdioFile fDebugFile("log.txt", CFile::typeText | CFile::modeReadWrite | CFile::modeCreate);
    va_list Marker;
    char szBuf[10500];
    CString szRecord;

    va_start(Marker, lpFormat);
    vsprintf(szBuf, lpFormat, Marker);
    va_end(Marker);

    szRecord.Format("%s\n", szBuf);
    fDebugFile.WriteString((LPCSTR)szRecord);
    fDebugFile.Flush();
    }
    int StartServer()
    {
         WSADATA wsaData;
     sockaddr_in local;
     SOCKET listen_socket; if(::WSAStartup(0x202,&wsaData)!=0)
    {printf("\nError in Startup session.\n");WSACleanup();return -1;}; local.sin_family=AF_INET;
    local.sin_addr.s_addr=INADDR_ANY;
    local.sin_port=htons(PROXYPORT); listen_socket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
    if(listen_socket==INVALID_SOCKET)
    {printf("\nError in New a Socket.");WSACleanup();return -2;}
    if(::bind(listen_socket,(sockaddr *)&local,sizeof(local))!=0)
    {printf("\n Errorcode %d in Binding socket.",WSAGetLastError());WSACleanup();return -3; };
    if(::listen(listen_socket,5)!=0)
    {printf("\n Error in Listen."); WSACleanup(); return -4;}
    gListen_Socket=listen_socket;  AfxBeginThread(UserToProxyThread,NULL);   //Start accept function
    return 1;
    }int CloseServer()
    {
    closesocket(gListen_Socket);
    WSACleanup();
    return 1;
    }
    // Setup chanel and read data from local , send data to remote
    UINT UserToProxyThread(void *pParam)
    {
     char Buffer[BUFSIZE];
    //  char log[15000];
     int  Len;
     sockaddr_in from;
     SOCKET msg_socket;
     int fromlen,retval;
     SocketPair SPair;

     CWinThread *pChildThread;
         fromlen=sizeof(from);
     msg_socket=accept(gListen_Socket,(struct sockaddr*)&from,&fromlen);
       AfxBeginThread(UserToProxyThread,pParam); //Start another thread to listen.
     if( msg_socket==INVALID_SOCKET)
    { printf( "\nError  in accept "); return -5;}
     
    //recieve the first line of client  SPair.IsUser_ProxyClosed=FALSE;
     SPair.IsProxy_ServerClosed=TRUE;
     SPair.user_proxy=msg_socket;  retval=recv(SPair.user_proxy,Buffer,sizeof(Buffer),0);
     
     if(retval==SOCKET_ERROR)
    { printf("Errorcode=%d Recv from client,ThreadID=%d",WSAGetLastError(),GetCurrentThreadId()); 
          if(SPair.IsUser_ProxyClosed==FALSE)
    {closesocket(SPair.user_proxy);
     SPair.IsUser_ProxyClosed=TRUE;
    }
    }
     if(retval==0)
    {printf("\nClient Close connection,ThreadID=%d",GetCurrentThreadId());
         if(SPair.IsUser_ProxyClosed==FALSE)
    {closesocket(SPair.user_proxy);
     SPair.IsUser_ProxyClosed=TRUE;
    }
    }
     Len=retval;
    #ifdef _DEBUG
    if (Len==-1) Buffer[0]=0;
    else Buffer[Len]=0;
    SPair.cs.Lock();
     DebugPrint("\n Received %d bytes,data[\n%s\n]from client\n",retval,Buffer);
     SPair.cs.Unlock();
     
     
    #endif
     //
     SPair.IsUser_ProxyClosed=FALSE;
     SPair.IsProxy_ServerClosed=TRUE;
     SPair.user_proxy=msg_socket;
     SPair.User_SvrOK=CreateEvent(NULL,TRUE,FALSE,NULL);  pChildThread=AfxBeginThread(ProxyToServer,(LPVOID)&SPair);
     ::WaitForSingleObject(SPair.User_SvrOK,60000);  //Wait for connection between proxy and remote server
     ::CloseHandle(SPair.User_SvrOK);  while(SPair.IsProxy_ServerClosed ==FALSE && SPair.IsUser_ProxyClosed==FALSE)
     {
       retval=send(SPair.proxy_server,Buffer,Len,0);
       if(retval==SOCKET_ERROR)
    { SPair.cs.Lock();
       DebugPrint("\nErrorcode=%d, Send to server,ThreadID=%d\n",WSAGetLastError(),GetCurrentThreadId());
         SPair.cs.Unlock();
     
     
         if(SPair.IsProxy_ServerClosed==FALSE)
    {
    closesocket(SPair.proxy_server);

    SPair.IsProxy_ServerClosed=TRUE;

    }
      continue;
    }
        retval=recv(SPair.user_proxy,Buffer,sizeof(Buffer),0);
     
    if(retval==SOCKET_ERROR)
    {
    SPair.cs.Lock();
    DebugPrint("\nErrorcode=%d, Recv from client,ThreadID=%d\n",WSAGetLastError(),GetCurrentThreadId());
    SPair.cs.Unlock();
          if(SPair.IsUser_ProxyClosed==FALSE)
    {closesocket(SPair.user_proxy);
     SPair.IsUser_ProxyClosed=TRUE;
    }
          continue;
    }
    if(retval==0)
    {SPair.cs.Lock();
    DebugPrint("\nClient Close connection,ThreadID=%d",GetCurrentThreadId());
     SPair.cs.Unlock();
         if(SPair.IsUser_ProxyClosed==FALSE)
    {closesocket(SPair.user_proxy);
     SPair.IsUser_ProxyClosed=TRUE;
    }
     break;
    }
    Len=retval;
    #ifdef _DEBUG
    if (Len==-1) Buffer[0]=0;
    else Buffer[Len]=0;
    SPair.cs.Lock();
     DebugPrint("\n Received %d bytes,data[\n%s\n]from client\n",retval,Buffer);
     SPair.cs.Unlock();
    #endif

      } //End While     if(SPair.IsProxy_ServerClosed==FALSE)
    {
    closesocket(SPair.proxy_server);
    SPair.IsProxy_ServerClosed=TRUE;
    }
     if(SPair.IsUser_ProxyClosed==FALSE)
    {closesocket(SPair.user_proxy);
     SPair.IsUser_ProxyClosed=TRUE;
    }
    ::WaitForSingleObject(pChildThread->m_hThread,INFINITE);  //Should check the reture value
    return 0;
    }
      

  3.   

    // Read data from remote and send data to local
    UINT ProxyToServer(LPVOID pParam)
    {
        SocketPair * pPar=(SocketPair*)pParam;
    char Buffer[BUFSIZE];
    // char log[15000];
    int retval,Len;
    struct sockaddr_in server;
    SOCKET  conn_socket; server.sin_family=AF_INET;
    //代理服务器的地址和端口
    server.sin_port=htons(80);
    server.sin_addr.s_addr=inet_addr("202.113.49.22"); conn_socket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP); /* Open a socket */
    if (conn_socket <0 ) {
    fprintf(stderr,"Client: Error Opening socket: Error %d\n",
    WSAGetLastError());
    pPar->IsProxy_ServerClosed=TRUE;
    ::SetEvent(pPar->User_SvrOK);
    return -1;
    }
    if (connect(conn_socket,(struct sockaddr*)&server,sizeof(server))
    == SOCKET_ERROR) {
    fprintf(stderr,"connect() failed: %d\n",WSAGetLastError());
    pPar->IsProxy_ServerClosed=TRUE;
    ::SetEvent(pPar->User_SvrOK);
    return -1;
    }
    pPar->proxy_server=conn_socket;
    pPar->IsProxy_ServerClosed=FALSE;
        ::SetEvent(pPar->User_SvrOK);
    // cook up a string to send
    while(!pPar->IsProxy_ServerClosed &&!pPar->IsUser_ProxyClosed)
    {
    retval = recv(conn_socket,Buffer,sizeof (Buffer),0 );
    if (retval == SOCKET_ERROR ) {
    pPar->cs.Lock();
     DebugPrint("\nErrorcode=%d, Recv from server,ThreadID=%d\n",WSAGetLastError(),GetCurrentThreadId());
     pPar->cs.Unlock();
    //closesocket(conn_socket);
    pPar->IsProxy_ServerClosed=TRUE;
    break;
    }
    Len=retval;
    if (retval == 0) {           //对方已关闭
    pPar->cs.Lock();
    DebugPrint("\nServer Close connection,ThreadID=%d",GetCurrentThreadId());
     pPar->cs.Unlock();
    //closesocket(conn_socket);
    pPar->IsProxy_ServerClosed=TRUE;
    break;
    } retval = send(pPar->user_proxy,Buffer,Len,0);
    if (retval == SOCKET_ERROR) {
    pPar->cs.Lock();
    DebugPrint("\nErrorcode=%d, Send to client,ThreadID=%d\n",WSAGetLastError(),GetCurrentThreadId());
     pPar->cs.Unlock();
    //closesocket(pPar->pPair->user_proxy);
    pPar->IsUser_ProxyClosed=TRUE;
    break;
    }
    #ifdef _DEBUG
    if (Len==-1) Buffer[0]=0;
    else Buffer[Len]=0;
    pPar->cs.Lock();
    DebugPrint("Received %d bytes, data [%s] from server\n",retval,Buffer);
     pPar->cs.Unlock();
    #endif
    }
    if(pPar->IsProxy_ServerClosed==FALSE)
    {
    closesocket(pPar->proxy_server);
    pPar->IsProxy_ServerClosed=TRUE;
    }
     if(pPar->IsUser_ProxyClosed==FALSE)
    {closesocket(pPar->user_proxy);
     pPar->IsUser_ProxyClosed=TRUE;
    }
     return 1;
    }int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
    {
    int nRetCode = 0; // initialize MFC and print and error on failure
    if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
    {
    // TODO: change error code to suit your needs
    cerr << _T("Fatal Error: MFC initialization failed") << endl;
    nRetCode = 1;
    }
    else
    {
    // TODO: code your application's behavior here.
    StartServer();
    while(1)
    if(getchar()=='q') break;
    CloseServer();
    } return nRetCode;
    }