C# Socket:通过代理服务器连接到服务器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/818921/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 02:25:31  来源:igfitidea点击:

C# Socket: connect to server through proxy server

c#networkingproxysockets

提问by ant2009

VS 2008

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.

我使用下面的代码来检测客户端是否可以连接到我们的 SIP 服务器。这工作正常。但是,客户端已经更改了那里的网络,现在我的应用程序必须从代理服务器后面连接到 SIP 服务器。

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"

“连接尝试失败,因为连接方在一段时间后没有正确响应,或建立连接失败,因为连接的主机未能响应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;
}

采纳答案by Haspemulator

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.

在此处查看我的答案,也许会有所帮助。大体思路是先建立与代理的连接,然后使用 HTTP CONNECT 命令打开另一个到给定主机和端口的 TCP 连接。

回答by kgiannakakis

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.

这与这个问题非常相似。我认为不可能通过代理服务器创建套接字连接。您将需要某种协议以及更多,通常管理员设置代理服务器以拒绝来自标准 HTTP、HTTPS 以外的端口的连接。

One solution would be to use tunneling over HTTP.

一种解决方案是通过 HTTP 使用隧道。

回答by weismat

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 Portand see if you get the same timeout/error message or if you get disconnected after entering a few characters.

要独立于您的应用程序检查代理配置,您可以使用Telnet Host Port并查看是否收到相同的超时/错误消息,或者是否在输入几个字符后断开连接。