• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

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: 22803
131
Eclipse IDE Spring 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.
 
Quick! Before anybody notices! Cover it up with this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic