Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 出现测试Tcp连接错误的线程_C#_Sockets_Testing_Tcpclient - Fatal编程技术网

C# 出现测试Tcp连接错误的线程

C# 出现测试Tcp连接错误的线程,c#,sockets,testing,tcpclient,C#,Sockets,Testing,Tcpclient,我尝试测试套接字连接。它显示错误 无法访问已释放的对象。 对象名称:“System.Net.Sockets.TcpClient” at连接(ip,端口) 这是从这里开始的线程之间的竞争条件: System.Threading.Thread t = new System.Threading.Thread(() => { client.Connect(ip,port ); }); t.Start(); 最后一块是: finally

我尝试测试套接字连接。它显示错误

无法访问已释放的对象。 对象名称:“System.Net.Sockets.TcpClient”

at连接(ip,端口)


这是从这里开始的线程之间的竞争条件:

    System.Threading.Thread t = new System.Threading.Thread(() =>
    {

        client.Connect(ip,port );

    });
    t.Start();
最后一块是:

    finally
    {
        client.Close();
    }
在本例中,主线程在连接完成之前到达finally块

对象创建和清理实际上应该在同一个线程上,所以尝试类似的方法,除非您真的需要闭包

System.Threading.Thread t = new System.Threading.Thread(() =>
{
    using(TcpClient client = new TcpClient())
    {
        client.Connect(ip, port);
        //followup code here
    }

});
t.Start();

这是从这里开始的线程之间的竞争条件:

    System.Threading.Thread t = new System.Threading.Thread(() =>
    {

        client.Connect(ip,port );

    });
    t.Start();
最后一块是:

    finally
    {
        client.Close();
    }
在本例中,主线程在连接完成之前到达finally块

对象创建和清理实际上应该在同一个线程上,所以尝试类似的方法,除非您真的需要闭包

System.Threading.Thread t = new System.Threading.Thread(() =>
{
    using(TcpClient client = new TcpClient())
    {
        client.Connect(ip, port);
        //followup code here
    }

});
t.Start();

根据您试图实现的目标,您可以修改代码以

System.Threading.Thread t = new System.Threading.Thread(() =>
                {
                    using (System.Net.Sockets.TcpClient client = new TcpClient())
                    {
                        client.Connect("127.0.0.1", 80);
                        // Communicate with the Server. 
                    }   // The client object would be disposed here
                });
                t.Start();

请记住,此代码将在每次线程运行时创建
TcpClient
的新实例。我假设,在建立连接后,您会很快与服务器通信,然后可以在对象完成后安全地处置该对象。

根据您试图实现的目标,您可以修改代码以

System.Threading.Thread t = new System.Threading.Thread(() =>
                {
                    using (System.Net.Sockets.TcpClient client = new TcpClient())
                    {
                        client.Connect("127.0.0.1", 80);
                        // Communicate with the Server. 
                    }   // The client object would be disposed here
                });
                t.Start();

请记住,此代码将在每次线程运行时创建
TcpClient
的新实例。我假设,在建立连接后,您将很快与服务器通信,然后可以安全地处理对象,一旦完成。

我在客户端遇到另一个问题。连接(ip,端口)显示“由于目标计算机主动拒绝,无法建立连接”我在客户端遇到另一个问题。连接(ip,端口)显示“由于目标计算机主动拒绝,无法建立连接”