Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Socket Programing

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

Am working in the Socket Programing, My application continuously reads the data packet from socket,
sometimes read method is giving 0 value. As per my knowledge the read method of the InputStream will throw IOException if the socket is closed and if data is not available it will give zero.My issue is how to check the socket connection status whether is alive or not and check whether the data is available.

If i get the solution that will be the great help for me..

Thanks..
Thamaiyanthi.P
 
Sheriff
Posts: 22649
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving to our Sockets forum.
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thamaiyanthi Pitchaimuthu wrote:
. . .sometimes read method is giving 0 value.



Do you mean that read returns 0 bytes? That is expected.
The API Documentation indicates that invoking read on a closed socket will throw an exception.
 
Thamaiyanthi Pitchaimuthu
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Joe,
I had a look into the API i saw the below point also.

*If there are no bytes buffered on the socket, or all buffered bytes have been consumed by read, then all subsequent calls to read will throw an IOException.

in my application i need to send some signaling message to the serversocket to send the data or ack. that time am checking the for exception (IOException) if that occurs am closing an reconnecting the connection. I dont know whether clocsing and reconnecting is good solution. if connection it meaning less to close the connection which is already closed. please give me some suggestion

I have come across so many blogs and forums but all little bit confused.

Thanks..
Thamaiyanthi.
 
Joe Ess
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thamaiyanthi Pitchaimuthu wrote:Hi Joe,
I dont know whether clocsing and reconnecting is good solution. if connection it meaning less to close the connection which is already closed. please give me some suggestion



My gut feeling is that invoking close() on a closed socket will throw an exception. Of course, the socket closing probably isn't the only reason a socket read would throw an exception, so you should probably try to close it in any event.
 
Thamaiyanthi Pitchaimuthu
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks joe,
Am closing and opening the socket whenever exception occurs.
 
I can't renounce my name. It's on all my stationery! And hinted in this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic