AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Sockets problem

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help
View previous topic :: View next topic  
Author Message
Matt
Guest





PostPosted: Sat Feb 25, 2006 4:12 pm    Post subject: Sockets problem Reply with quote



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
Back to top
Knute Johnson
Guest





PostPosted: Sat Feb 25, 2006 6:12 pm    Post subject: Re: Sockets problem Reply with quote



Matt wrote:
Quote:
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/
Back to top
Matt
Guest





PostPosted: Sat Feb 25, 2006 7:12 pm    Post subject: Re: Sockets problem Reply with quote



Knute Johnson wrote:
Quote:
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.


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
Back to top
Steve Horsley
Guest





PostPosted: Mon Feb 27, 2006 11:12 pm    Post subject: Re: Sockets problem Reply with quote

Knute Johnson wrote:
Quote:
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.


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
Back to top
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.