Solved

java socket speed

Posted on 2000-04-25
11
860 Views
Last Modified: 2012-05-04
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?
0
Comment
Question by:magosse
  • 4
  • 4
  • 2
  • +1
11 Comments
 
LVL 7

Expert Comment

by:Sasha_Mapa
ID: 2749427
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
 
LVL 16

Expert Comment

by:heyhey_
ID: 2749558
calling native code from Java is much slower that calling Java code from Java code (try Double.longBitsToDouble() for example ...)
0
 
LVL 3

Expert Comment

by:falter
ID: 2749588
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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 2749602
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
 
LVL 7

Expert Comment

by:Sasha_Mapa
ID: 2749887
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
 
LVL 3

Expert Comment

by:falter
ID: 2749979
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
 
LVL 16

Accepted Solution

by:
heyhey_ earned 50 total points
ID: 2750215
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
 
LVL 7

Expert Comment

by:Sasha_Mapa
ID: 2751043
ignore my last comment... I wrote it at 5am in the morning :-)
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 2756085
hmm ... thanks :)
0
 
LVL 7

Expert Comment

by:Sasha_Mapa
ID: 2757390
silly users...
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 2758273
grade C ... will ruin my profile :)
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This video teaches viewers about errors in exception handling.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now