java socket speed

Are TCP/IP sockets, with client/server written in Java, slower than in other languages like C++ ?
If it is slower what things can be done to increase file transfer speeds?
magosseAsked:
Who is Participating?
 
heyhey_Connect With a Mentor Commented:
so back to teh question:
>> Are TCP/IP sockets, with client/server written in Java, slower than in other languages like C++ ?

yes. and you don't have the ful SOCKET functionality (in Java you work on pretty high level)

>> If it is slower what things can be done to increase file transfer speeds?

not much. if you really need a good speed, why don't you use C++ (which is platform independent enough)

re calling native code: yes - the slowest part is 'calling' native code. if you can reduce this kind of traffic (JavaVM -> native code and back) your app will work much better (note that - after all - almost all the standard API at the end calls some platform / native code :)
0
 
Sasha_MapaCommented:
Using a BufferedReader/BufferedWriter will increase speed a LOT.
I'm sure they would be slower, but not much slower, because they are after all implemented in native code...
0
 
heyhey_Commented:
calling native code from Java is much slower that calling Java code from Java code (try Double.longBitsToDouble() for example ...)
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
falterCommented:
But there is no way around the native code.
The only things you can do is avoid
DataOutput/InputStreams they will break down all calls to a single byte read or write which at least calls a native function read one byte ore write one byte to the socket.
So BufferedReader/Writer as Sasha_Mapa mentioned will be a good choice.

If this is not enough you can write your one SocketImpl with your one code or you
write only a replacement for net.dll (windows), libnet.so (Unix) ...
But this will not be a trivial task.
0
 
Jim CakalicSenior Developer/ArchitectCommented:
Yes, buffering can make a BIG difference. According to performance material published in the "Client Server Programming with Java/Corba" book, the difference between writing to a socket using a DataOutputStream and writing to a socket using a BufferedOutputStream decorated by a DataOutputStream was two orders of magnitude.

The timings published for a simple ping application were 300.5ms per message when using just DataOutputStream and 2.1ms per message when using BufferedOutputStream in conjunction with DataOutputStream.

Choosing the right buffer size is important. The default constructor of BufferedOutputStream creates a 512-byte buffer. You will likely want to create a stream with a buffer large enough to contain some unit of the message that you want to send -- optimally the entire message. Remember too, to flush -- something every good mother teaches ;-)

Best regards,
Jim Cakalic
0
 
Sasha_MapaCommented:
heyhey, what I meant is that it can't be faster under any conditions because both use (the same?) native code... Thanks for the info though, didn't know it was much longer to call native code... So does that mean I should leave my implementation of antialiased Graphics in Java and not move it to C and call it from Java?
0
 
falterCommented:
Depends on the number of calls you made to the native code.
Is it one method doing all the stuff and give you some result back, you will only recognize it if you will call this methods realy often.

Same effect as using Bufferd streams in socket programming, reducing the number of native calls, means reducing the overhead you got through a JNI call.
0
 
Sasha_MapaCommented:
ignore my last comment... I wrote it at 5am in the morning :-)
0
 
heyhey_Commented:
hmm ... thanks :)
0
 
Sasha_MapaCommented:
silly users...
0
 
heyhey_Commented:
grade C ... will ruin my profile :)
0
All Courses

From novice to tech pro — start learning today.