Problem when connecting Java App with C++ App using Windows Socket

Problem when connecting Java App with C++ App using Windows Socket

Post by S2V2aW4gTF » Fri, 29 May 2009 02:33:01


I encounter such a strange problem:
We have a VC++ Application, which needs to communicate with another Java
application using socket (in VC++ we use Windows socket indeed). The Java App
acts as the server and VC++ App as the client. The two works well when
implement them in the same computer. However, if they are implemented in
different computers, whether in a LAN or in a WAN (acrossing routers),
problem encounters: the Java App often reported it detects socket error and
close the socket (and the connection) which the VC++ client makes, then the
VC++ client will try to reconnect the Java App again, thus result in such a
situation, the connection is made, then about 20-30 seconds later, the
connection is closed, then reconnect,...
We found that often the Java App detected socket error when it tried to
write data to the client. if there was no data to write to client, Java won't
detect the socket error. Of course, the link between the two app is OK.
We don't know why, the Java App is developed by another company, and we
don't have much experience in Java so we couldn't give much advice to them.
However, in our point, we think socket programming model is universal
acrossing different platforms and different programming languages. Maybe we
are wrong? If we want to communicate Java App using Windows socket, we need
to do something special ?

Thanks

Kevin Liu
 
 
 

Problem when connecting Java App with C++ App using Windows Socket

Post by Remy Lebea » Fri, 29 May 2009 03:00:35


It is. So it is likely a bug in the Java app's handling of its socket, or a
bug in Java itself.


If everything is done right, you shouldn't need to. For instance, I have a
Java app that communicates with a Windows app via sockets, and it works
fine. However, in my case, the Java app is the client and the Windows app
is the server. But it should not make a difference.

--
Remy Lebeau (TeamB)

 
 
 

Problem when connecting Java App with C++ App using Windows Socket

Post by S2V2aW4gTF » Fri, 29 May 2009 03:14:01

To make things more complicated, we tested the 2 Apps in several WAN
environment, in some environments, they work well together (meaning Java App
does not detect socket error), in other environments, they don't.
Moreover, we used to suspect that the Java App may not use buffer control:
when the socket write-buffer is full, the Java App doesn't wait but report
write error. However, this may not be true because in those environments they
don't work well, only very small size of data the Java App need to send to
VC++ client.

BTW, for the VC++ client, we use asychronous mode to connect to the Java App
(using connect() method and WSAEnumNetworkEvents() method) because we want
one instance of the VC++ client to connect multiple instances of Java Apps.
But, after successful connection, we switch the socket to synchornous mode,
and use dedicated worker threads to communicate with Java App on that socket,
one thread for receiving data from Java App using block-mode recv() method,
one thread for sending data to Java App. For the moment, we don't know
whether the Java App uses synchronous mode or asynchronous mode socket, and
we have no knowledge (or experience) for that whether the socket client and
socket server must use the same synchronizing mode or not.

Thanks

Kevin
 
 
 

Problem when connecting Java App with C++ App using Windows Socket

Post by Remy Lebea » Fri, 29 May 2009 06:12:32


What is the actual socket error that is being reported?


Like I said earlier, the Java app is probably not managing its sockets
correctly to begin with.


Does not matter.


You don't need to use asynchronous mode in order to do that.


Since you are using threads anyway, each thread could handle its own
connect() to a particular server in synchronous mode.


If you continue using the sockets in asynchronous mode, then you do not need
separating reading/writing threads. A single thread can handle both I/O
simultaneously. The fewer threads you need to use, the more connections you
will be able to support, and you will be using fewer resources overall.


It would not really matter either way.


They do not.

--
Remy Lebeau (TeamB)
 
 
 

Problem when connecting Java App with C++ App using Windows Socket

Post by Luke Alcat » Fri, 29 May 2009 07:39:45

The request I would make of the Java app vendor is to provide detailed
diagnostics about the socket error. I/O member functions of socket classes
throw exceptions when errors occur and the getMessage() method of the
exception object may provide useful information.

Also, when you run the Java app on a different machine is it a Windows
environment? If not is it X86 based? There is a possibility that messages
won't be understood if the network endpoints have different architectures
and scaler data is embedded in the messages such as ints without proper use
if htonl and ntohl.

I mix win32 and Java based network endpoints often with no problem.

Finally, if you are familiar with network protocol analyzers such as
WireShark, I would suggest that you examine network messages passed between
client and server to determine whether content, timing, etc. is different in
the single host vs. dual host scenario.

HM