For Programmers: Free Programming Magazines  


Home > Archive > Java Help > February 2006 > Sockets problem









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Sockets problem
Matt

2006-02-25, 6:59 pm

Hi all,

I'm developing a server using ServerSocket. For each connection
established with a client, a new thread is created for attending it. The
thread task consists on basically writing data through the socket
continuously. There is no socket reading work in my server.

The point is that I'm not able to detect if the connection with the
client is broken, in order to stop the corresponding thread.

If, for example, the client application is halted suddenly, my server
keeps on writing on the socket although there is nobody listening on the
other side...

I would like to detect the connection failure if the client "dissapears"
for some reason, so that I can stop my thread and act in consecuence.

I've tried with Socket.isConnected, Socket.isClosed and so on...
unsuccessfully.


Here is the code of my server thread:

****************************************
****

public void run()
{
PrintWriter writer = new PrintWriter( new OutputStreamWriter (
m_socket.getOutputStream()));

m_socket.setKeepAlive(false);

while(m_repeat)
{
writer.println(data); writer.flush();
Thread.sleep(500);
if (!m_socket.isConnected()) m_repeat = false;
}
writer.close();
m_socket.close();
}
****************************************
******

Previously I have created a ServerSocket and called accept(), pasing the
new socket to the thread (it is stored in m_socket).

The line using m_socket.isConnected does not work, as I said.

*** Another problem I have is that when I close the connection from the
client by calling Socket.close(), the server keeps on working too! (But
this point is less important for me in this case...)


In summary:

1) How can I detect a connection failure or a client sudden
disconnection from the server?

2) How can I close the connection correctly in the client, so that the
server is notified?



Could anybody help me? Am I doing something wrong?

Thanks in advance,



Matt



Knute Johnson

2006-02-25, 6:59 pm

Matt wrote:
> Hi all,
>
> I'm developing a server using ServerSocket. For each connection
> established with a client, a new thread is created for attending it. The
> thread task consists on basically writing data through the socket
> continuously. There is no socket reading work in my server.
>
> The point is that I'm not able to detect if the connection with the
> client is broken, in order to stop the corresponding thread.
>
> If, for example, the client application is halted suddenly, my server
> keeps on writing on the socket although there is nobody listening on the
> other side...
>
> I would like to detect the connection failure if the client "dissapears"
> for some reason, so that I can stop my thread and act in consecuence.
>
> I've tried with Socket.isConnected, Socket.isClosed and so on...
> unsuccessfully.
>
>
> Here is the code of my server thread:
>
> ****************************************
****
>
> public void run()
> {
> PrintWriter writer = new PrintWriter( new OutputStreamWriter (
> m_socket.getOutputStream()));
>
> m_socket.setKeepAlive(false);
>
> while(m_repeat)
> {
> writer.println(data); writer.flush();
> Thread.sleep(500);
> if (!m_socket.isConnected()) m_repeat = false;
> }
> writer.close();
> m_socket.close();
> }
> ****************************************
******
>
> Previously I have created a ServerSocket and called accept(), pasing the
> new socket to the thread (it is stored in m_socket).
>
> The line using m_socket.isConnected does not work, as I said.
>
> *** Another problem I have is that when I close the connection from the
> client by calling Socket.close(), the server keeps on working too! (But
> this point is less important for me in this case...)
>
>
> In summary:
>
> 1) How can I detect a connection failure or a client sudden
> disconnection from the server?
>
> 2) How can I close the connection correctly in the client, so that the
> server is notified?
>
>
>
> Could anybody help me? Am I doing something wrong?
>
> Thanks in advance,
>
>
>
> Matt
>


Matt:

I think that sooner but probably later your outbound write will fail
because there is no aknowledgement from the client socket. I had a
similar problem in a server/client program I just wrote. The only way I
could think of to get a quicker notification was to create another
thread that tries to read from the socket. When the client end is
closed the read throws an immediate IOException. No data is ever read
because the client doesn't do any writing and the read just blocks until
the socket is closed. When the IOException is thrown I close the
outbound socket and clean up.

--

Knute Johnson
email s/nospam/knute/
Matt

2006-02-25, 6:59 pm

Knute Johnson wrote:
> Matt wrote:
>
>
> Matt:
>
> I think that sooner but probably later your outbound write will fail
> because there is no aknowledgement from the client socket. I had a
> similar problem in a server/client program I just wrote. The only way I
> could think of to get a quicker notification was to create another
> thread that tries to read from the socket. When the client end is
> closed the read throws an immediate IOException. No data is ever read
> because the client doesn't do any writing and the read just blocks until
> the socket is closed. When the IOException is thrown I close the
> outbound socket and clean up.
>


Thank you very much Knute,

It's surprising the fact that no support for connection monitoring is
provided by Socket... but I've tried your solution and works fine :)

I'd like to point out that, as far as i have seen, no IOException is
thrown when the client closes the connection. The readLine method call
which I use is blocking, and it just returns when the client is killed.

Anyway, it works.

Thank you very much for your help.

Matt
Steve Horsley

2006-02-27, 7:09 pm

Knute Johnson wrote:
> Matt wrote:
>
> Matt:
>
> I think that sooner but probably later your outbound write will fail
> because there is no aknowledgement from the client socket. I had a
> similar problem in a server/client program I just wrote. The only way I
> could think of to get a quicker notification was to create another
> thread that tries to read from the socket. When the client end is
> closed the read throws an immediate IOException. No data is ever read
> because the client doesn't do any writing and the read just blocks until
> the socket is closed. When the IOException is thrown I close the
> outbound socket and clean up.
>


I don't think the reading is necessary. Once the TCP stack knows
that the connection is broken, the next write call will fail with
an exception anyway. The code above doesn't see it because
PrintWriter swallows exceptions, IIRC. But an OutputStreamWriter
should raise them. And you should be using an OutputStreamWriter
anyway, so you can specify the character encoding, rather than
just hoping that the platform default encoding will happen to
suit the client.

Steve
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com