【问题标题】:Why am I getting IOException and SocketException while writing to NetworkStream?为什么在写入 NetworkStream 时出现 IOException 和 SocketException?
【发布时间】:2013-04-02 09:10:47
【问题描述】:

我有一个将我连接到软件的 dll,我们称之为 driver1。 我有一个必须连接到 driver1 并查询其状态的 java 程序。 该dll为我提供了连接和查询状态的功能。

我编写了一个 c++ 程序,我将 dll 作为参考,我可以从中使用 dll。我可以连接到 driver1 并查询它的状态。 然后我创建了一个 TcpListener,我的 Java 程序可以作为客户端连接它。 我只是接受 C++ 中的客户端并等待从 Java 程序接收数据。

我从 Java 端发送数据(例如表示 dll 的“连接”方法的命令)。 该命令从 c++ 端很好地接收,它解析并调用 dll 的指定函数。但是,当我想将 dll 函数的返回值写入 NetworkStream 以将返回值发送到 Java 端时,我得到一个异常说:

System.IO.IOException:IOException:无法将数据写入传输连接。尝试对非套接字的操作进行操作。

c++ 端代码类似于来自 msdn 站点的 TcpListener 示例:

#using <System.dll>

using namespace System;
using namespace System::IO;
using namespace System::Net;
using namespace System::Net::Sockets;
using namespace System::Text;
using namespace System::Threading;
void main()
{
    try
    {

        // Set the TcpListener on port 1124.
        Int32 port = 1124;
        IPAddress^ localAddr = IPAddress::Parse( "127.0.0.1" );

        // TcpListener* server = new TcpListener(port);
        TcpListener^ server = gcnew TcpListener( localAddr,port );

        // Start listening for client requests.
        server->Start();

        // Buffer for reading data
        array<Byte>^bytes = gcnew array<Byte>(256);
        String^ data = nullptr;

        Console::Write( "Waiting for a connection... " );

        // Perform a blocking call to accept requests.
        TcpClient^ client = server->AcceptTcpClient();
        Console::WriteLine( "Connected!" );
        data = nullptr;

        while(true) 
        {
            // Get a stream Object* for reading and writing
            NetworkStream^ stream = client->GetStream();
            Int32 i;

            // Loop to receive all the data sent by the client.
            while ( i = stream->Read( bytes, 0, bytes->Length ) )
            {

                // Translate data bytes to a ASCII String*.
                data = Text::Encoding::ASCII->GetString( bytes, 0, i );
                Console::WriteLine( "Received: {0}", data );

                // Process the data sent by the client.
                /*
                 *
                 * Here I parse the data, understand what method of the dll to call.
                 * Call it and make a String^ from the return value.
                 *
                 *
                 */
                array<Byte>^msg = Text::Encoding::ASCII->GetBytes( returnValue );

                // Send back a response.
                stream->Write( msg, 0, msg->Length );
                Console::WriteLine( "Sent: {0}", data );
            }
        }

        // Shutdown and end connection
        client->Close();
     }
     catch ( SocketException^ e ) 
     {
         Console::WriteLine( "SocketException: {0}", e );
     }

     Console::WriteLine( "\nHit enter to continue..." );
     Console::Read();
}

我在调用的那一行遇到异常:

流->Write()

对正在发生的事情有什么想法吗?

提前谢谢...

【问题讨论】:

  • java代码在stream.Write之后是什么样子的?它会关闭套接字吗?
  • @Default,不,它不会关闭套接字,在我连接到 dll 之前,我可以在 Java 端接收和发送数据。

标签: .net c++-cli socket.io ioexception system.net.sockets


【解决方案1】:

我怀疑连接由于某种原因而终止。我认为如果您使用 DOS 命令NETSTAT -A 检查连接状态,您会发现连接已关闭或处于挂起状态。很难说是连接的哪一端(客户端或服务器)造成了问题。

另外,在发送数据之前尝试调试TCPSocket 以检查其状态。可能是一端过早关闭连接....

【讨论】:

  • 感谢@justderb,我检查了与netstat -a 的连接,发现在c++ 端从java 接收“connect”命令并连接dll 后,127.0.0.1:1124 的旧连接已失效,并且c++ 端和 dll 127.0.0.1:7600 之间的新连接已打开。当我在 dll 连接后以调试模式检查客户端套接字时,我看到“可用”属性显示 SocketException。在它是 0 之前。
  • 顺便说一下,在我调用 dll 的 connect 函数之前,我可以将任何内容写入流并发送给客户端。
  • 好吧,我不知道你的 dll 是什么或它在做什么,所以你需要自己弄清楚。但是,我很高兴您发现了问题!
  • 谢谢。我也不知道 dll 里面是什么 :) 好的,我会尝试直接使用 java 中的 dll...
猜你喜欢
  • 2017-12-07
  • 2013-12-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-23
  • 2011-09-10
  • 1970-01-01
相关资源
最近更新 更多