新的tcpclient()。connected抛出null引用 [英] New tcpclient().connected throws null reference

查看:175
本文介绍了新的tcpclient()。connected抛出null引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,



我写了一个定制的延迟检查器。有一个客户端和服务器。客户端是控制台应用程序,服务是服务。我被限制为.Net 3.5。



所以这一切都在本地工作正常,但在客户端主服务器上它会抛出一个错误:

Hey,

I have written a bespoke "latency checker". There is a client and server. Client is Console App and service is a Service. I am restricted to .Net 3.5.

So this all works locally fine, but on the clients main server it throws an error:

while (!client.Connected && retry-- > 0)

空引用异常
$ t $ b at TcpClient.Get_Connected(指向上面一行)



我不明白。我只是实例化了它。它可能是最多的。



这里是这个方法的代码。

Null ref exception
at TcpClient.Get_Connected (points to the line above)

I don't get it. I only just instantiated it. The most it might be is closed.

here is the code for this method.

private int GetPort(long taskLength, bool isReport = false)
{
    IPEndPoint endPoint = new IPEndPoint(_address, _port);
    TcpClient client = new TcpClient();
    int port = 0;


    int retry = retries;

    while (!client.Connected && retry-- > 0)
    {
        try
        {
            client.Connect(endPoint);

            using (var stream = client.GetStream())
            using (StreamReader reader = new StreamReader(stream))
            using (StreamWriter writer = new StreamWriter(stream))
            {
                writer.AutoFlush = true;
                if (isReport)
                {
                    writer.WriteLine($"report:{taskLength}");
                }
                else
                {
                    writer.WriteLine($"port request:{taskLength}");
                }
                string portString = reader.ReadLine();
                if (!string.IsNullOrEmpty(portString) && int.TryParse(portString, out port))
                    return port;
                throw new BadResponseException("Port format incorrect");
            }
        }
        catch (BadResponseException)
        {
            //retry
        }
        catch (SocketException)
        {
            //retry
        }
        finally
        {
            client.Close();
        }
    }
    return port;
}

第一次致电: taskLength = 1 isReport = false





任何可能造成这种情况的想法都会有所帮助^ _ ^



谢谢



我的尝试:



我在本地测试了客户端和服务器,服务器在本地运行。这是我将客户端放在另一台机器上之前的第一个测试。



服务器有1个端口保持打开状态,以便客户端发出请求。它打开一个端口异步,将其设置为接收特定数量的字节并回复说明端口号是什么。



我只有一个客户端运行atm这是单线程。

For the first call: taskLength = 1 and isReport = false.


Any ideas on what could've caused this would help ^_^

Thanks

What I have tried:

I tested the client and server locally and the server is running both locally to it. This is the first test before I put the client on another machine.

The Server has 1 port held open for requests from clients. It opens a port async, sets it to receive a specific number of bytes and replies back saying what the port number is.

I only have 1 client running atm which is single threaded.

推荐答案

report:{taskLength} );
}
else
{
writer.WriteLine(
"report:{taskLength}"); } else { writer.WriteLine(


port request:{taskLength});
}
string portString = reader.ReadLine();
if (!string.IsNullOrEmpty(portString)&&& int .TryParse(portString , out port))
return port;
throw new BadResponseException( 端口格式不正确);
}
}
catch (BadResponseException)
{
// 重试
}
catch (SocketException)
{
// retry
}
finally
{
client.Close();
}
}
返回端口;
}
"port request:{taskLength}"); } string portString = reader.ReadLine(); if (!string.IsNullOrEmpty(portString) && int.TryParse(portString, out port)) return port; throw new BadResponseException("Port format incorrect"); } } catch (BadResponseException) { //retry } catch (SocketException) { //retry } finally { client.Close(); } } return port; }

第一次致电: taskLength = 1 isReport = false





任何可能造成这种情况的想法都会有所帮助^ _ ^



谢谢



我的尝试:



我在本地测试了客户端和服务器,服务器在本地运行。这是我将客户端放在另一台机器上之前的第一个测试。



服务器有1个端口保持打开状态,以便客户端发出请求。它打开一个端口异步,将其设置为接收特定数量的字节并回复说明端口号是什么。



我只有一个客户端运行atm这是单线程。

For the first call: taskLength = 1 and isReport = false.


Any ideas on what could've caused this would help ^_^

Thanks

What I have tried:

I tested the client and server locally and the server is running both locally to it. This is the first test before I put the client on another machine.

The Server has 1 port held open for requests from clients. It opens a port async, sets it to receive a specific number of bytes and replies back saying what the port number is.

I only have 1 client running atm which is single threaded.


我猜它只会在多次重试时发生。然后您的 TcpClient 可能不再有效,因为 Close()致电:

I guess that it only occurs when there are multiple retries. Then your TcpClient might be no longer valid due to the Close() call:

Close方法将实例标记为已处置并请求关联的Socket关闭TCP连接。

The Close method marks the instance as disposed and requests that the associated Socket close the TCP connection.

注意已处理。



无需执行已连接完全检查,因为在关闭后代码中总是 false 。但是如果 Close()调用使对象无效,那么您可能会在下一个 Connect()打电话。唯一的解决方案是使用新实例(可选地限于 Connect()调用不会抛出异常的情况;当抛出异常时,没有需要关闭)。

Note the "disposed".

There is no need to perform the Connected check at all because it would be always false in your code after closing. But if the Close() call makes the object invalid, you might then get the same error at the next Connect() call. The only solution would be then using a new instance (optionally limited to the case that the Connect() call does not throw an exception; when that throws an exception, there is no need to close).


这篇关于新的tcpclient()。connected抛出null引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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