• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Slow TCP/IP in Java

SendTrash

Platinum Member
Hello,

I am creating an application in Java 1.4 and I seem to be having trouble on latency.

What is the common latency that I should be getting if I am just sending Strings over a 100 LAN Ethernet?

I wrote a little test program that sent out 3 types of Strings to a simple server that spit back the strings to my client.

Here are the results:

Avg times
Short Strings (1 word) = 21 ms
Medium Strings (sentence) = 74ms
Long Strings (sample SOAP Message) = 97ms

To me these times are way too large. Does anyone know what my problem my be? Or is java really that slow in TCP? I will be making a test for UDP soon.

PS- for timing I used a DLL that provided must better results that Java's own System call to get the System time, so the times are pretty accurate
 
Client Code

Server Code

The client is messy because I am looping it around the string outputs so that I can get an average value.

Do you still have any previous networking code? Oh... and here is a jar file with the Timing code in it.. if you want to run the code you need to unpack the jar and move the dll into your java path and WinTimer.java into a default package with the client.java

Jar File for Timer code

Thank you...
 
Update:

When I ran the code from my command line instead of my IDE (eclipse) I get slighly better times:

Short - 3ms
Medium - 47ms
Long - 79 ms

The short string was 7 bytes, medium was 385 bytes and the long string was 571 bytes.

Correct me if I am wrong, but in a 100 LAN, there is ideally 12500 bytes per ms bandwidth.

In my program, I am sending Strings at a rate of 571bytes/79ms = 7.22 bytes/ms... that is less than 1 percent network utilization!!!!

Even if my network was congested (which it is not), these times are unacceptable.
 
I don't have much experience with JAVA, but I will help to the best of my knowledge 🙂

(1) The very first thing I noticed is that you are using Buffered readers/writers. I think that this is what is causing your slowdown.
(2) For speed, you might consider writing RAW bytes instead of "Objects"


LMK if you need any clarification.
 
Originally posted by: singh
I don't have much experience with JAVA, but I will help to the best of my knowledge 🙂

(1) The very first thing I noticed is that you are using Buffered readers/writers. I think that this is what is causing your slowdown.
(2) For speed, you might consider writing RAW bytes instead of "Objects"


LMK if you need any clarification.

Hmm.. what should I use other than Buffered streams? Are you saying I should deal with Input/Output Streams and then recast them as chars? I can try that...
 
Originally posted by: SendTrash
Originally posted by: singh
I don't have much experience with JAVA, but I will help to the best of my knowledge 🙂

(1) The very first thing I noticed is that you are using Buffered readers/writers. I think that this is what is causing your slowdown.
(2) For speed, you might consider writing RAW bytes instead of "Objects"


LMK if you need any clarification.

Hmm.. what should I use other than Buffered streams? Are you saying I should deal with Input/Output Streams and then recast them as chars? I can try that...

I'm just saying that try to remove the *Buffered* stream so that the data is sent/received ASAP. Are you only going to be sending Text Data (Strings)?
 
1. In the server remove the System.out.println() between the read and the write.
2. Do not create a new reader and writer every time through the loop. Create them before the loop and use them in the loop.
 
I decided to try out the code. I changed the WinTimer to just use System.currentTimeMillis(). With the original code I was getting 1, 1 and 2. With my changes I got 0, 0, 0. So unless there is a timer with finer granularity that is about the best I can do.
 
wow... I forgot I had System.out calls in the server. After removing them it became much much faster

Size of short message: 7
Size of medium message: 385
Size of long message: 11420
Average Times:
short messages: 2 ms
medium messages: 1 ms
long messages: 6 ms

*** now, setting calling sock.setTcpNoDelay(true) on both client and server...

Size of short message: 7
Size of medium message: 385
Size of long message: 11420
Average Times:
short messages: 1 ms
medium messages: 1 ms
long messages: 2 ms
 
Back
Top