Reputation: 73
I have a dll which connects me to a software, let's call it driver1. I have a java program which has to connect to driver1 and query its status. The dll provides me functions for connecting and querying the status.
I wrote a c++ program which I gave the dll as a reference and I am able to use the dll from it. I can connect to driver1 and query its status. Then I created a TcpListener that my Java program can connect as a client. I simply accept the client in c++ and wait for recieving data from Java program.
I send the data from Java side (for example a command representing "connect" method of dll). The command is well recieved from the c++ side and it parses and calls the specified function of dll. However when I want to write the return value of the dll function to the NetworkStream to send the return value to Java side I get an exception saying:
System.IO.IOException: IOException: Unable to write data to the transport connection. An operation was attempted on something that is not a socket.
The c++ side code is the similar to the TcpListener sample from the msdn site:
#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();
}
I get the exception on the line where I call:
stream->Write()
Any ideas about what is happenning?
Thanks in advance...
Upvotes: 0
Views: 2074
Reputation: 2854
I suspect the connection is terminating for some reason. I think if you check the status of the connection using the DOS command NETSTAT -A
you will find the connection is either closed or in a pending state. It is difficult to say which end of the connection (client or server) created the problem.
Also, try debugging the TCPSocket
to check it's state just before sending your data. It could be one end is closing the connection prematurely....
Upvotes: 1