我在代理服务器上建立一个服务器端(用外网地址),想在局域网内通过代理与服务器端见理连接,
用TCP,应该如何建立??#define PROXY_IP "10.178.192.33"
#define PROXY_PORT 1080#define DEST_IP "61.159.174.109"
#define DEST_PORT 3721#define LOCAL_IP "10.178.192.58"
#define LOCAL_PORT 4567。buf[0] = 5; 
buf[1] = 1; //connect
buf[2] = 0; 
buf[3] = 1; 
    
*(unsigned int*)&buf[4] = inet_addr(DEST_IP);//name.sin_addr.s_addr;    
*(unsigned short*)&buf[8] = htons(DEST_PORT);    
send(fd, buf, 10, 0);    
recv(fd, buf, 10, 0);   //fd为与代理的连接 这样代理连接服务器是成功的,我怎么用套接字和服务器关联哪???

解决方案 »

  1.   

    我的程序的一个函数,支持HTTP代理,和SOCKS5,关于SOCKS5参看RFC1928,RFC1929
    void CConnectSocket::OnConnect(int nErrorCode)
    {
    if(CProxyStyle == 0)        //不使用代理
    {
    AfxGetMainWnd()->GetActiveWindow()->SendMessage(WM_USER_CONNECTED,nErrorCode);
    return;
    }
    else if(nErrorCode == 0)
    {
    MSG msg;
    int totalnum;
    int num;
    int round;
    if(CProxyStyle == 1)    //SOCKS5 代理
    {
    char buf[10];
    buf[0] = 5;
    buf[1] = 2;
    buf[2] = 2;
    buf[3] = 0;
    totalnum = 4;
    num = 0;
    round = 0;
    for(;;)
    {
    if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
    {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
    }
    if(m_bTimeOut == TRUE)      //连接已经超时
    {
    if(m_Stoped == FALSE)   //Close()未被调用
    Close();
    return;
    }
    if(m_Stoped == TRUE)        //Close()已被调用
    return;
    round = Send(buf + num, totalnum - num, 0);
    if (round > 0)
    num += round;
    if(num == totalnum)     //发送完毕
    break;
    }
    totalnum = 2;
    num = 0;
    round = 0;
    for(;;)
    {
    if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
    {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
    }
    if(m_bTimeOut == TRUE)
    {
    if(m_Stoped == FALSE)
    Close();
    return;
    }
    if(m_Stoped == TRUE)
    return;
    round = Receive(buf + num,totalnum - num,0);
    if (round > 0)
    num += round;
    if(num == totalnum)    //接收完毕
    break;
    }
    if ( (buf[0] != 5) || (buf[1] == 0xFF) || ((buf[1] != 0)&&(buf[1] != 2)) )
    {
    AfxGetMainWnd()->GetActiveWindow()->SendMessage(WM_USER_CONNECTED,1123);
    return;
    }
    if (buf[1] == 2)      //需要身份验证
    {
    AfxGetMainWnd()->GetActiveWindow()->SendMessage(WM_USER_PROXYMSG,0);
    char* auth = new char[3 + ProxyUserName.GetLength() + ProxyUserPIN.GetLength()];
    if(auth == NULL)
    {
    AfxGetMainWnd()->GetActiveWindow()->SendMessage(WM_USER_CONNECTED,1124);
    return;
    }
    sprintf(auth,"  %s %s", (const char*)ProxyUserName, (const char*)ProxyUserPIN);
    auth[0] = 5;
    auth[1] = ProxyUserName.GetLength();
    auth[2 + ProxyUserName.GetLength()] = ProxyUserPIN.GetLength(); totalnum = 3 + ProxyUserName.GetLength() + ProxyUserPIN.GetLength();
    num = 0;
    round = 0;
    for(;;)
    {
    if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
    {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
    }
    if(m_bTimeOut == TRUE)
    {
    if(m_Stoped == FALSE)
    Close();
    delete [] auth;
    return;
    }
    if(m_Stoped == TRUE)
    {
    delete [] auth;
    return;
    }
    round = Send(auth + num,totalnum - num, 0);
    if (round > 0)
    num += round;
    if(num == totalnum)
    break;
    }
    delete [] auth;
    totalnum = 2;
    num = 0;
    round = 0;
    for(;;)
    {
    if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
    {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
    }
    if(m_bTimeOut == TRUE)
    {
    if(m_Stoped == FALSE)
    Close();
    return;
    }
    if(m_Stoped == TRUE)
    return;
    round = Receive(buf + num,totalnum - num,0);
    if (round > 0)
    num += round;
    if(num == totalnum)
    break;
    }
    if (buf[1] != 0)
    {
    AfxGetMainWnd()->GetActiveWindow()->SendMessage(WM_USER_CONNECTED,1125);
    return;
    }
    }
    memset(buf, 0, 10);    //开始通过代理连接
    buf[0] = 5; 
    buf[1] = 1;
    buf[2] = 0; 
    buf[3] = 1;
    unsigned long ipv4 = inet_addr((const char*)RemoteServer);
    unsigned short por = ntohs(RemotePort);
    if(ipv4 == INADDR_NONE)
    {
    AfxGetMainWnd()->GetActiveWindow()->SendMessage(WM_USER_CONNECTED,1126);
    return;
    }
    memcpy(buf+4, &ipv4, 4);
    memcpy(buf+8, &por, 2);
    AfxGetMainWnd()->GetActiveWindow()->SendMessage(WM_USER_PROXYMSG,1);
    totalnum = 10;
    num = 0;
    round = 0;
    for(;;)
    {
    if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
    {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
    }
    if(m_bTimeOut == TRUE)
    {
    if(m_Stoped == FALSE)
    Close();
    return;
    }
    if(m_Stoped == TRUE)
    return;
    round = Send(buf + num,totalnum - num,0);
    if (round > 0)
    num += round;
    if(num == totalnum)
    break;
    }
    totalnum = 10;
    num = 0;
    round = 0;
    for(;;)
    {
    if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
    {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
    }
    if(m_bTimeOut == TRUE)
    {
    if(m_Stoped == FALSE)
    Close();
    return;
    }
    if(m_Stoped == TRUE)
    return;
    if(m_bTimeOut == FALSE)
    round = Receive(buf + num,totalnum - num,0);
    else
    {
    if(m_Stoped == FALSE)
    Close();
    return;
    }
    if (round > 0)
    num += round;
    if(num == totalnum)
    break;
    }
    if (buf[1] != 0)
    {
    AfxGetMainWnd()->GetActiveWindow()->SendMessage(WM_USER_CONNECTED,1127);
    return;
    }
    AfxGetMainWnd()->GetActiveWindow()->SendMessage(WM_USER_CONNECTED,0);
    }
    else if(CProxyStyle == 2)     //HTTP 代理
    {
    char buf[600];
    sprintf(buf, "%s%s:%d%s","CONNECT ",(const char*)RemoteServer, (const char*)RemotePort," HTTP/1.1\r\nUser-Agent: MyApp/0.1\r\n\r\n");
    totalnum = strlen(buf);
    num = 0;
    round = 0;
    for(;;)
    {
    if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
    {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
    }
    if(m_bTimeOut == TRUE)
    {
    if(m_Stoped == FALSE)
    Close();
    return;
    }
    if(m_Stoped == TRUE)
    return;
    round = Send(buf + num,totalnum - num,0);
    if (round > 0)
    num += round;
    if(num == totalnum)
    break;
    }
    memset(buf, 0, 39);
    totalnum = 39;
    num = 0;
    round = 0;
    for(;;)
    {
    if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
    {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
    }
    if(m_bTimeOut == TRUE)
    {
    if(m_Stoped == FALSE)
    Close();
    return;
    }
    if(m_Stoped == TRUE)
    return;
    if(m_bTimeOut == FALSE)
    round = Receive(buf + num,totalnum - num,0);
    else
    {
    if(m_Stoped == FALSE)
    Close();
    return;
    }
    if (round > 0)
    num += round;
    if(num == totalnum)
    break;
    }
    if((strstr(buf, "Connection established") == NULL)) //连接失败
    {
    AfxGetMainWnd()->GetActiveWindow()->SendMessage(WM_USER_CONNECTED,1123);
    return;
    }
    AfxGetMainWnd()->GetActiveWindow()->SendMessage(WM_USER_CONNECTED,0); //成功
    }
    }
    }