简体   繁体   中英

C# Socket: connect to server through proxy server

VS 2008

I am using the code below to detect if the client can connect to our SIP server. This was working fine. However, the client has changed there network and now my application has to connect to the SIP server from behind a proxy server.

The error I get is the:

"A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond xxx.xxx.xx.xx:xx"

This code was working ok, until I have to connect from behind a proxy server.

I could not see any properties there I can add the proxy address to the socket.

Am I missing something?

Many thanks for any suggestions,

public bool IsSIPServerAvailable()
{
    bool isAvailable = true;
    Socket sock = new Socket(AddressFamily.InterNetwork,
                             SocketType.Stream,
                             ProtocolType.Tcp);

    try
    {
        sock.Connect("xxx.xxx.xx.xx", xx);
    }
    catch (SocketException ex)
    {
        Console.WriteLine(ex.Message);
        isAvailable = false;
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
        isAvailable = false;
    }
    finally
    {
        if (sock.Connected)
        {
            sock.Close();
        }
    }

    return isAvailable;
}

See my answer here , maybe it will be helpful. The general idea is to first establish connection to a proxy, and then use HTTP CONNECT command to open another TCP connection to a given host and port.

This is very similar to this question . I don't think it is possible to create a socket connection through a proxy server. You would need some kind of a protocol and more over, usually administrators set up proxy servers to refuse connections from ports other than standard HTTP, HTTPS.

One solution would be to use tunneling over HTTP.

The whole idea of a proxy is to change the network topology without changing the applications. Thus there is no need to change the app, but a change is required in the proxy configuration/setup.

To check the proxy config independently of your application, you can use Telnet Host Port and see if you get the same timeout/error message or if you get disconnected after entering a few characters.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM