Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / February 2006

Tip: Looking for answers? Try searching our database.

Sockets problem

Thread view: 
Matt - 25 Feb 2006 16:07 GMT
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 - 25 Feb 2006 17:50 GMT
> Hi all,
>
[quoted text clipped - 60 lines]
>
> 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.

Signature

Knute Johnson
email s/nospam/knute/

Matt - 25 Feb 2006 18:43 GMT
>> Hi all,
>>
[quoted text clipped - 73 lines]
> 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 - 27 Feb 2006 22:44 GMT
>> Hi all,
>>
[quoted text clipped - 73 lines]
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.