Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/133.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# TCPClient错误:";系统无效操作例外“;_C#_Tcpclient - Fatal编程技术网

C# TCPClient错误:";系统无效操作例外“;

C# TCPClient错误:";系统无效操作例外“;,c#,tcpclient,C#,Tcpclient,我一直在创建一个TCPClient,它应该连接到服务器,但出现以下错误: System.InvalidOperationException:'不允许在上执行该操作 未连接的插座。” 这是我的密码: Public String IPAddress = "192.168.100.xxx" Public Int32 Port = 23; public TcpClient client = new TcpClient(); public void Connect() { client.Conn

我一直在创建一个TCPClient,它应该连接到服务器,但出现以下错误:

System.InvalidOperationException:'不允许在上执行该操作 未连接的插座。”

这是我的密码:

Public String IPAddress = "192.168.100.xxx"
Public Int32 Port = 23;
public TcpClient client = new TcpClient();

public void Connect() {
    client.Connect(IPAddress, Port);
    // send first message request to server
    Byte[] msg_data = System.Text.Encoding.ASCII.GetBytes("Hello Server);

    // uses the GetStream public method to return the NetworkStream
    NetworkStream netStream = _client.GetStream();

    // write message to the network
    netStream.Write(msg_data, 0, msg_data.Length);

    // buffer to store the response bytes
    msg_data = new Byte[256];

    // read the first batch of response byte from arduino server
    Int32 bytes = netStream.Read(msg_data, 0, msg_data.Length);
    received_msg = System.Text.Encoding.ASCII.GetString(msg_data, 0, bytes);

    netStream.Close();

}

public void Send() {
    // message data byes to be sent to server
    Byte[] msg_data = System.Text.Encoding.ASCII.GetBytes(_sendMessage);

    // uses the GetStream public method to return the NetworkStream

    // ** Error Here: System.InvalidOperationException: 'The operation is not allowed on non-connected sockets.'
    NetworkStream netStream = client.GetStream(); 

    // write message to the network
    netStream.Write(msg_data, 0, msg_data.Length);

    // buffer to store the response bytes
    msg_data = new Byte[256];

    // read the first batch of response byte from arduino server
    Int32 bytes = netStream.Read(msg_data, 0, msg_data.Length);
    received_msg = System.Text.Encoding.ASCII.GetString(msg_data, 0, bytes);

    netStream.Close(); // close Stream
}
我在创建
NetworkStream netStream=client.GetStream()的新实例时遇到错误。我一直在努力寻找导致错误的原因,我认为它以某种方式关闭了上面的连接

所有内容都在一个类中,必须在软件中的任何位置调用。

client.GetStream()的实现方式如下:

return new NetworkStream(this.Client, true);
而true表示如果流被释放/关闭,它也会关闭/断开套接字/客户端的连接。你应该可以通过直接打电话来避免这种情况

var netStream = new NetworkStream(client.Client, false);
更好的办法是:

NetworkStream netStream = client.GetStream();
…
netSteam.Dlose();
要确保流始终关闭,即使写入错误:

using (var netStream = new NetworkStream(client.Client, false))
{
  …
}

您在连接时似乎正在关闭网络流--这会影响客户端的状态吗?我不这么认为,即使在流弹出此错误后我没有关闭流:/@Bas是的,它正在关闭导致错误的流x)您说错误发生在
Send()
方法中,但您的代码没有显示对该方法的任何调用。您需要提供一个可靠地再现问题的好方法。也就是说,错误消息非常清楚:您试图对未连接的
TcpClient
对象调用
GetStream()
,即从未连接或已关闭。不要这样做。确保流始终处于接近使用状态的方法非常好。谢谢