• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Socket Programing

 
Thamaiyanthi Pitchaimuthu
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • 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
 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to our Sockets forum.
 
Joe Ess
Bartender
Pie
Posts: 9266
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
Pie
Posts: 9266
10
Linux Mac OS X Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks joe,
Am closing and opening the socket whenever exception occurs.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic