C# Socket:通过代理服务器连接到服务器 [英] C# Socket: connect to server through proxy server

查看:128
本文介绍了C# Socket:通过代理服务器连接到服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

VS 2008

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

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.

我得到的错误是:

连接尝试失败,因为连接方在一段时间后没有正确响应,或者建立连接失败,因为连接的主机没有响应xxx.xxx.xx.xx:xx"

"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.

我错过了什么吗?

非常感谢您的任何建议,

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;
}

推荐答案

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

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.

这篇关于C# Socket:通过代理服务器连接到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆