【问题标题】:Trying to close an active TCP Connection, but getting error尝试关闭活动的 TCP 连接,但出现错误
【发布时间】:2014-02-13 13:18:25
【问题描述】:

我有一个使用 ASP.net c#、javascript 和 jQuery 的 Web 应用程序。

我试图在我的 PAGE_UNLOAD 事件中关闭某些活动的 TCP 连接。

这是我的代码 -

IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties();
TcpConnectionInformation[] connections = properties.GetActiveTcpConnections();

IPAddress ipAddress = Dns.Resolve(camIP).AddressList[0];
IPEndPoint remEndPoint = new IPEndPoint(ipAddress, 80);

foreach (TcpConnectionInformation c in connections)
{
    if (c.RemoteEndPoint.ToString() == remEndPoint.ToString())
    {
        Socket mySocket = new Socket(AddressFamily.InterNetwork,
            SocketType.Stream, ProtocolType.Tcp);

        try
        {
            mySocket.Bind(c.LocalEndPoint);
            mySocket.Shutdown(SocketShutdown.Both);
        }
        catch (Exception ex)
        {

        }
        finally
        {
            mySocket.Close();
        }
    }
}

但是,当我尝试绑定时,我收到以下错误 - “尝试以访问权限禁止的方式访问套接字”

通过设置img控件的src创建TCP连接-

CameraIframe.Src = @"http://" + CameraHiddenFieldUsername.Value + ":" +
    CameraHiddenFieldPassword.Value + "@" + camIP + @"/axis-cgi/mjpg/video.cgi?fps=" +
    CameraHiddenFieldfps.Value + "&compression=" + CameraHiddenFieldcompression.Value +
    "&resolution=320x240&text=0&date=0&clock=0&eek=" + DateTime.Now.ToBinary();

我是套接字编程的新手,因此我们将不胜感激。

【问题讨论】:

    标签: c# asp.net sockets tcp error-handling


    【解决方案1】:

    套接字很可能被某个进程占用。使用 netstat -o 查找哪个。

    【讨论】:

    • 有没有办法强制关闭socket?套接字有一个 PID 并且状态已建立。
    【解决方案2】:

    关闭或“删除”时这不是问题。 绑定时出现问题。请相应地修正您的标题。您正在尝试绑定已绑定的套接字,或尝试将其绑定到本地端口

    你正在做的事情开始变得没有意义。如果它们是您自己的连接,只需关闭它们。 您不需要为此创建新的套接字。

    当你创建一个新的套接字时,你不需要绑定它,除非你打算用它来监听它,并且在没有事先连接它或随后关闭它的情况下关闭它也没有意义。

    【讨论】:

    • 如何关闭它们?
    猜你喜欢
    • 2021-03-26
    • 1970-01-01
    • 2012-10-04
    • 1970-01-01
    • 2017-05-15
    • 2016-08-06
    • 1970-01-01
    • 2021-06-29
    • 1970-01-01
    相关资源
    最近更新 更多