Win a copy of Hands On Software Engineering with Python this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

Socket read, MTU and truncation  RSS feed

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi,

I am experiencing an issue when reading from a java socket.  

The socket read is working for most reads until a longer message is received.  The received message seems to be truncated at 1460 characters which seems to fit in with the normal MTU size of 1500 bytes (minus 40 bytes for headers).

The code which is performing the read from the socket is below;


The socket timeout is set to 5 seconds, the response form the downstream service returns in approx 0.1 second.

The only change I thought about making was to switch the read() to readFully().  Unfortunately I can't create the issue on my development machine (as is normal!) and the issue one occurs in one environment which I don't have access too.

Other than that does anyone have any other ideas as to what maybe the issue and/or solution?

Many thanks

 
Saloon Keeper
Posts: 5130
135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This may or may not be related the problem at hand: You're using the InputStreamReader constructor that doesn't take a Charset parameter - so the platform default charset will be using, whatever that is. Are you sure that that is the correct one for the data you're receiving?
 
Rancher
Posts: 3353
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why does the code exit the loop at line 22 instead of waiting for a -1 at line 15?
 
matt andrews
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:Why does the code exit the loop at line 22 instead of waiting for a -1 at line 15?



I was under the impression that if < 4096 bytes had been returned, everything had been read.  This is how it seems to be working in my test environment.

From what you are saying and how the production environment is also working, it seems I could drop this check and just keep reading the socket buffer until -1 is returned (end of stream).  
 
matt andrews
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

matt andrews wrote:

Norm Radder wrote:Why does the code exit the loop at line 22 instead of waiting for a -1 at line 15?



I was under the impression that if < 4096 bytes had been returned, everything had been read.  This is how it seems to be working in my test environment.

From what you are saying and how the production environment is also working, it seems I could drop this check and just keep reading the socket buffer until -1 is returned (end of stream).  




I remember now.  The socket doesn't terminate at the server end, so I can't check for a -1.  The only way the look exits is either a timeout, or knowing its read all the information.  

I've look at the data being returned.  There seems to be a size indicator in a header, so I'm now reading that via a DataInputStream and then reading the creating a byte[] for the length (these messages are all under 8k) and using;



I'm hoping that will solve the issue.

Any comments / observations?
 
Norm Radder
Rancher
Posts: 3353
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I don't have much experience with that.  Your approach sounds reasonable.  Can try it to see what happens?
 
matt andrews
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:Sorry, I don't have much experience with that.  Your approach sounds reasonable.  Can try it to see what happens?



Yep. That fixed it!
 
bacon. tiny ad:
Programmatically Create PDF Using Free Spire.PDF with Java
https://coderanch.com/wiki/703735/Programmatically-Create-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!