我有一個DLL連接到一個軟件,我們稱之爲driver1。 我有一個java程序,它必須連接到driver1並查詢它的狀態。 該DLL爲我提供了連接和查詢狀態的功能。爲什麼在寫入NetworkStream時收到IOException和SocketException?
我寫了一個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();
}
我上線的例外,我稱之爲:
於流>寫()
有關發生什麼的任何想法?
在此先感謝...
什麼java代碼看起來像它的stream.Write後?它關閉了套接字嗎? – Default 2013-04-10 13:39:20
@Default,不,它不會關閉套接字,我可以接收和從Java端發送/從我連接到DLL之前。 – rendar 2013-04-10 14:25:08