Hello,
I am working in the communication between two programs, one in C++ and the other in Java, connected using standard sockets. Both programs run under UNIX (Ubuntu) within the same machine and share information for a period of time in regular basis. At some point of the execution and always at the same point it gets stuck since the program in C++ sends the information and the one in Java does not get all the info so they block since the first expect to receive that and the second is not sending anything because did not receive the information in the firs place.
The odd thing is one you execute the program in Java under Windows. Then it works fine, the program ends correctly without any blocing.
I think is an issue with the Java application but why the difference between running the under Ubuntu or Windows? Do the socket behave differently? Is some parameter different from the JVM in Ubuntu and Windows?
Thank you very much in advance!
Julen.
EDIT:
This the code in the Java side that reads the buffer:
if (task.equals("receiving")){
try {
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = socket.getOutputStream();
// receive messages
char[] length = new char[5];
while (!socket.isClosed()&&(!socket.isInputShutdown())){
in.read(length,0,5);
// this way of reading the length implies that only one command
// at a time can be received and interpreted, so far the iCS does not
// concatenate more commands in one transmission
int commandLength = length[4];
System.err.println("Speed Advice --> command received with length "+ commandLength);
char[] command = new char[commandLength - 1];
in.read(command,0,commandLength - 1);
/*if (cow){
System.err.println("Speed Advice --> Last byte received for X-pos is "+(int)command[commandLength-1]);
}*/
readCommand(command);
}
System.err.println("Speed Advice --> Socket was externally closed.");
in.close();
closeConnection();
}catch (Exception e) {
e.printStackTrace();
}
}
And this C++ sending information:
void
Socket::
send( std::vector<unsigned char> b)
throw( SocketException )
{
if( socket_ < 0 ) return;
size_t numbytes = b.size();
unsigned char *const buf = new unsigned char[numbytes];
for(size_t i = 0; i < numbytes; ++i)
{
buf[i] = b[i];
}
if (verbose_)
{
cerr << "Send " << numbytes << " bytes via tcpip::Socket: [";
for(size_t i = 0; i < numbytes; ++i)
{
buf[i] = b[i];
cerr << " " << (int)b[i] << " ";
}
cerr << "]" << endl;
}
unsigned char const *buf_ptr = buf;
while( numbytes > 0 )
{
#ifdef WIN32
int n = ::send( socket_, (const char*)buf_ptr, static_cast<int>(numbytes), 0 );
#else
int n = ::send( socket_, buf_ptr, numbytes, 0 );
#endif
if( n<0 )
{
// BailOnSocketError definitely throws an exception so clear up heap
delete[] buf;
BailOnSocketError( "send failed" );
}
numbytes -= n;
buf_ptr += n;
}
delete[] buf;
}