我有一个将我连接到软件的 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();
}
我在我打电话的那一行得到了例外:
流->写()
关于正在发生的事情有什么想法吗?
提前致谢...