aspose file tools*
The moose likes Sockets and Internet Protocols and the fly likes Socket Programing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Socket Programing" Watch "Socket Programing" New topic
Author

Socket Programing

Thamaiyanthi Pitchaimuthu
Greenhorn

Joined: Aug 20, 2010
Posts: 14
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

Joined: Oct 27, 2005
Posts: 19722
    
  20

Moving to our Sockets forum.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8927
    
    9

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.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Thamaiyanthi Pitchaimuthu
Greenhorn

Joined: Aug 20, 2010
Posts: 14
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

Joined: Oct 29, 2001
Posts: 8927
    
    9

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

Joined: Aug 20, 2010
Posts: 14
Thanks joe,
Am closing and opening the socket whenever exception occurs.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Socket Programing