Detecting socket disconnections

Hi!
Im using a ServerSocket to enable clients to connect to my
application. The communication over the returned Socket works
fine, but I cannot detect when the client disconnects. The
socket is put on a CLOSE_WAIT state, and according to socket
sources this means that the client is expecting the final last
ack (LAST_ACK) from me. The LAST_ACK is sent if I close() the
socket.
The problem is that I cannot detect disconnection in my app without
actually trying to send some "garbage" data on the socket.
I then get an exception, of course. Is there any clean way
to implement this?
Thanks!
Regards,
Lars Ove Claesson

Hi!
Im using a ServerSocket to enable clients to connect to my
application. The communication over the returned Socket works
fine, but I cannot detect when the client disconnects.
The socket is put on a CLOSE_WAIT state, and according to
socket sources this means that the client is expecting the
final last ack (LAST_ACK) from me. The LAST_ACK is sent if I
close() the socket.Not exactly although the result is the same.
The server is in the CLOSE_WAIT state. That means that the application is supposed to close the socket. Once it does it sends a FIN to the client and then moves to the LAST_ACK state (waiting for the client to ACK the FIN.) The client of course would be waiting for the FIN.
>
The problem is that I cannot detect disconnection in
my app without
actually trying to send some "garbage" data on the socket.
I then get an exception, of course. Is there any clean
way to implement this?As mentioned the blocking read should do this.
Myself I always prefer to explicitly send a "end" message from the client (or server.) That way I handle it explicitly rather that relying on something else.

Similar Messages

  • ServerSocket does not detect a disconnected client

    Hi,
    I have created a serverscoket, which will accept 'n' number of clients and send data to all the connected clients.
    And the server should detect when any client goes down or disconnected from network and give the appropriate message to the use that this particluar client has got disconnected.
    I'm able to detect the disconnected client, when the client application is shut down properly (and the application closes the socket). But while the application is running, when the network card is unplugged, then my server is not able to detect this connection loss, and it continues to send the data to the client without throwing any exception.
    Any help would really be greatly appreciated.
    Thanks,
    Hema

    I Asume you don't mean the ServerSocket but the Socket the ServerSocket accepted, for this Socket you can try using
    acceptedSocket.setKeepAlive (true);This may speed up the disconnection detection depending on the implementation of the keepalive mechanism.
    But it then may be that the connection will be terminated, although the connection is only very slow.
    You have to test out, what is better for your purpose.

  • Detecting client disconnects

    What's the best way to detect a client loss of connection? I've heard
    you can register for weblogic.accounting.disconnect events or used the
    workspace destroyMonitor method. Which is preferred? The events method
    concerns me because I don't believe it will be supported in the future
    once JMS takes a foothold. Is this a valid concern? Are there any new
    ways to do this?
    thanks,
    Edwin

    I would suggest you try catching java.net.SocketException (Connection reset
              by peer: socket write error) in you doGet/doPost method. This is not a sure
              way though ...
              --- Igor Zavialov
              "Dylan Taffe" <[email protected]> wrote in message
              news:3a5393d6$[email protected]..
              > Does anyone know of a way to detect client disconnects with servlets?
              Does
              > the contextEventListener or any other event listener get called when a
              > clients kills its connection with the weblogic server?
              >
              >
              

  • Detecting OCI disconnect

    What is the best way to detect if your OCI database connection has been lost.
    I guess that I could create a thread that runs repeated "select * from dual" or something like that in the background in order to detect a disconnect but I was hoping that there is some better way do this.
    Something more like OCIRegisterCallbackfunc(OCI_ATTR_TYPE_DISCONNECTNOTIFY,CallbackFunc).
    regards
    /N

    But theese functionalities requires that the listener works ok?! If the database machine, for instance, had an power outage and rebooted then I wont get any notification?
    Register for database startup: I assume that you mean a controlled startup of hte instance, not a startup after a reboot of the db-machine. Am I correct?
    Regards
    /I

  • Server to detect client disconnection "Sockets"

    i want to know how it is possible for a server to find out when the client is disconnected at any instance. not only at the time of sending data...
    i am writing a server that should communicate with different clients, and i want it to be able to find out if a client suddenly disconnects or crashes, so how can this be done?!
    is it this way:
    skt.setKeepAlive(true);the problem is that SO_KEEPALIVE checks after 2 hours, which is too much time, how can i change this value????

    So, i want to be able to find out if the client is disconnected without waiting to send or receive data.Use a read timeout.
    how can i change the value of SO_KEEPALIVEYou can't. It is two hours by default and requires privilege to change it, and changing it is system-wide. So you don't want to do that.
    Use a read timeout. Socket.setSoTimeout().

  • Socket disconnection event not being reveived java.nio

    Hi All
    My question is as below
    i am using java.nio package + win 2K + jdk1.4.1_02
    I have a Server accepting socket connection and is configured
    in Nonblocking mode.
    -The server receives (selector) events (accept event)
    when a client requests for a connection .
    -The server is getting read and write events when
    client wants to read and write
    -The server gets a read event when the client disconnects
    My only problem is that the Server is not getting any events
    when the client disconnect due to network break down
    for EX when the network connection breaks the client disconnects
    but the server selector is not getting any events
    I am storing the client Socket objects in a Hash Table and
    if i execute the .isConnected() method it returns true
    even though the the client is down
    The only work around is to try to read and write on the socket
    i get an IO exception and am able to make out that the
    client is disconnected
    But this is not desirable for my client
    as client modification to give any kind of echo is not possible and
    a read /write event will cause problem.
    Can any one tell how to detect client side disconnection
    with out Read and Write on the client socket
    I get an socket Exception when the socket is in Blocking IO and is blocked on read/ write but I am facing this problem in NonBlockingIO
    Regards
    Avinash

    int ct = read.selectNow();
    if (ct > 0)
    Set readyKeys = read.selectedKeys();
    Iterator i = readyKeys.iterator();
    while (i.hasNext())
    SelectionKey key = (SelectionKey) i.next();
    i.remove();
    if (key.isReadable())
    SelectableChannel nextReady = (SelectableChannel) key.channel();
    SocketChannel sc = (SocketChannel) key.channel();
    int rd = sc.read(buffer);
    // Link is dead
    if (rd == -1)
    key.cancel();
    key.attach(null);
    nextReady.close();
    continue;
    // Process your data
    catch (Exception e)
    e.printStackTrace();

  • Detecting socket closure

    Is there a way of telling from a java.net.Socket object if it is no longer connected to another socket? I have tried isBound, isConnected, and isClosed.
    Say my socket has connected, done some processing, and the other end disconnects on me - is there a way of telling at a point in the future if this is the case?isBound, isConnected, and isClosed all seem to return previous state, i.e. isConnected always returns true if the socket has been connected, not if it is currently connected.
    Any ideas?
    D
    p.s. I know I could catch some sort of exception but in my particular scenario I would prefer to avoid using that approach

    I've got the same problem.
    I've developed a java based HTTP client which can access a variety of web servers - most of which I can happily access. For some reason IIS sends a FIN TCP packet in certain situations. I need to detect in my client that the socket has been closed by the server. However trying all 3 methods - always gives the result that it believes the connection is still open. So when I next send a HTTP request to the server (on the socket I believe is still open) I get back a RST TCP packet. The result being that the BufferedReader.readline() method return NULL to my application.
    Any ideas of a fix?
    John

  • Detect client disconnection

    Hi.
    My Java Web Service works with ComPorts so I need to close it in a right way when a client disconnects.
    Is there a way to detect in the Web Service when a client disconnects.
    Thank�s you all!!

    So, i want to be able to find out if the client is disconnected without waiting to send or receive data.Use a read timeout.
    how can i change the value of SO_KEEPALIVEYou can't. It is two hours by default and requires privilege to change it, and changing it is system-wide. So you don't want to do that.
    Use a read timeout. Socket.setSoTimeout().

  • How to handle unexcepted socket disconnection?

    I have created a queue in server, and allow multiple clients to access the queue. Those clients who try to access empty queue are blocked using wait( ) method.
    They worked well, however, when I terminate a waitting client, there is an exception information in server:
    "java.net.SocketException:Connection reset by peer: socket wirte error"
    I want to shut down the waitting thread normally, who can I do this?
    Thanks a lot!

    Thanks a lot.
    When some client trying to get item from an empty
    queue, the queue instance invoke wait( ) method to
    block the client, until other client put something
    into the queue invoking notify( ) method. I wander if
    this is clear enough?
    The problem is, server don't know when some client
    disconnect,Server will know in the next time that it going to read / write to the socket stream
    then you will have to do something about it.
    maybe I can use a loop to test if the thread is alive.I dont think so. Even if the client is disconnected from the server the Client thread in the server will be alive since it is in the run method.
    Make your clients sends some sort of a signel to the server whn they disconnect.
    ex:- "CLIENT_CLOSING"
    once the server receives that the serve should close the client connection

  • Java 1.3.1 and Detecting Socket Closure (A Popular Question, I Know)

    Hi, all. I've been skimming the web looking at various commentary on how it is impossible to detect whether or not a Socket has closed without calling the read of Socket.getInputStream() or the write of Socket.getOutputStream(). I wrote a networking package some months ago that would spawn one Thread for each Socket so I could spare the Threads to block on the I/O, thus following this suggestion. I also understand that Java 1.4 contains various elements of the java.nio package that are supposed to take care of nonblocking I/O. I think, however, that this is entirely possible with Sockets in Java 1.3.1.
    I wrote a program that calls Socket.setSoTimeout(), setting the timeout of the socket to 1 millisecond. I then have one Thread looking at a series of open Sockets. The Thread calls the read method on each of the Sockets' InputStream objects and does one of three things based upon the result:
    If the result is a successful read, the data is handled as is appropriate.
    If the result is a -1, the Socket has been closed and is formally shut down on this side.
    If the result is an InterruptedIOException, the Socket is open but has no available data.
    The Socket is then ignored until the next pass.
    I've tested this idea on an Athalon XP 2100+ and not seen a significant use of processing power.
    Therefore, my question is as follows: what's wrong with this idea? We've looked at it and it seems to be an appropriate solution to the problem. Yet, I've seen little mention of this way to work around blocking Socket I/O in Java 1.3.1. It seems appropriate and certainly cuts back on Thread overhead... but does anyone see a catch?
    Thanks! Best of luck!

    This approach to multiplexing seems to not work under the 1.3.1 release for Windows. I have found I still get a -1 on my input stream read, instead of the InterruptedIOException. It's pretty frustrating.
    The axact same code appears to work fine on Linux . So this may be dependent on the native code layer for the sockets implementation, or we may be encountering a bug in our or Sun's code. I haven't found documentation of the expected behaviour of the SocketInputStream (the actual class returned by Socket.getInputStream) or the socket implementation in general for each platform.
    Note that in general, your approach is not really scalable for the kind of situations the Non-blocking IO package was introduced for. Your approach means that 500 sockets will have a 1/2 second intrinsic delay. You would have to start thread-pooling, etc. You can see why something like an internet audio streaming application would benefit from lower level support in the java libraries.
    Neil

  • RMI detect client disconnect

    Hello, I want to detect when a network disconnect occurs. I implemented a TimerTask that checks every X seconds if the remote object is still bound in the registry, by doing a Naming.lookup(). When I unplug the cable of my computer (to simulate the disconnect), the Naming.lookup() just seems to hang, there is no response... When I plug in the cable again, then I get a response, but this is not the idea... Is there a way to set a timeout or something on this method call?
    thanks

    When I was toying around with chat servers using the RMI Technology, I would have the server keep a vector of RMI Clients (almost like the registry). Then when I went to send a message, I would try to cast the Vector element into the correct type of a client and write the message. If an exception was thrown, I knew the client was not connected any longer and I removed the client from the vector. This approach I have since seen in a lot of documentation on chat servers.
    I haven't tried this but you may try calling the Naming.list () and comparing the values to see if you find a match.
    Hope this helps

  • Server socket disconnection in a client-server setup.

    Hi all,
    is there any way of knowing when server side of the socket connection goes away?
    The scenario is like this, I have a client-server environment with sockets connection. The server can detect when the client socket closes or just goes away but, it is not happening the other way. The client socket remains unaware that the server has gone down and could still send messages across a PrintWriter stream. I guess I am missing something here.
    Any help is appreciated.
    Thanks in advance,
    Cyrus

    Fundamental lesson in Sockets:
    1) A socket is nothing more than an address and port to the programmer.
    2) When you create the socket, data flies then.
    3) If you cannot create the socket, your exception tells you the reason.
    4) If you are open and go to send data, you will can tell by exceptions whether it flew or not.
    5) If you are open and go to read data, you have a time out option which will allow you to go into error recovery mode. For example:
    read_data_and_wait_45_seconds
    if ( exception) then (See if port is still up do error checking) else (received the data)

  • Detect Client Disconnect When using RTMP

    Hello,
    I hava a Java Server and Clients using RTMP. I've read somewhere that I can detect instantly client disconnection when using RTMP. However I cannot find an example code (in Java) that demonstrates this functionality? Can you help me please?

    I am not sure how your client looks, however the lient connect using NetConnection under the hood which spits NetStatusEvents's and you can listen to them ...
    C

  • 36 dukes dollars !!!! : socket disconnects itself on the internet

    Hi all,
    It's not very nice, but I have all these dukes to give to the persons that want to help me. I have a problem I'm working on for weeks, making me mad.
    Here are the dukes I'll distibute (I swear I will if responses that help me, even a little).
    http://forum.java.sun.com/thread.jsp?forum=4&thread=302722
    http://forum.java.sun.com/thread.jsp?forum=31&thread=269487
    http://forum.java.sun.com/thread.jsp?forum=57&thread=291727
    Now, here is my problem :
    I have created a server using the nio package. Each client connects to the server, and the socket isn't closed until the client asks for it. The connection could exist for ages.
    On my local network, this works perfect. But, when I install my server on the internet (on a professionnal computer with fix IP), and that I connect one or more clients, the connections never stay alive more than 30mn. They disconnect by themselves. I have watched the packets received and sent by the client. The client doesn't send or receive any packet (as he is connected to the server), and still doesn't receive or send any packet as the server has already seen that the socket was closed (The server received a -1. Notice that I don't know where this -1 comes from, and that it is what I want to know).
    On my local network, my server is installed on a red-hat 7.3.
    On the internet, my server is installed on a Slackware 7.
    If you have any idea, thank you very much for helping. I'm not shure it is a java problem, but anyway, I post my question here ! If it is a basic network problem, socket configuration on the server, security problem on Linux, or else, help is still accepted and rewarded.
    Thank you.
    Greg.

    Your observation regarding Timeouts and socket connections
    being open (outside your intranet) is correct.
    As another person has pointed out you may extend this time
    limit by setting the SO timeout value - but this needs
    to be done at both client and server sides.
    Do you think it would be reasonable to expect your client
    software users to tinker with their IP param values on
    their server - in order to use your software?
    You might want to look into an alternate approach - like
    opening another socket connection from the client if the
    present connection is already closed. Just so you can
    circumvent the timeout issue.
    George

  • Wierd socket disconnects on __weblogic_admin_rmi_queue

    We are running WLS 7.0 SP2 and I need to upgrade the JVM to 1.4.2 to get around a stability problem. When doing so, saw the following error from the admin server every 5 seconds:
    ####<Oct 11, 2006 11:15:45 AM EDT> <Error> <socket> <spdevapp1> <pmapp1> <ExecuteThread: '5' for queue: '__weblogic_admin_rmi_queue'> <kernel identity> <> <000424> <IOException on socket: weblogic.servlet.internal.MuxableSocketHTTP@ec972 - idle timeout: '30000' ms, socket timeout: '5000' ms, fd: 79
    java.net.SocketException: Connection reset>
    java.net.SocketException: Connection reset
    Start server side stack trace:
    java.net.SocketException: Connection reset
    at java.net.SocketInputStream.read(SocketInputStream.java:168)
    at weblogic.socket.PosixSocketMuxer.readBytesProblem(PosixSocketMuxer.java:889)
    at weblogic.socket.PosixSocketMuxer.deliverGoodNews(PosixSocketMuxer.java:774)
    at weblogic.socket.PosixSocketMuxer.processSockets(PosixSocketMuxer.java:700)
    at weblogic.socket.SocketReaderRequest.execute(SocketReaderRequest.java:23)
    at weblogic.kernel.ExecuteThread.execute(ExecuteThread.java:213)
    at weblogic.kernel.ExecuteThread.run(ExecuteThread.java:189)
    End server side stack trace
    at java.net.SocketInputStream.read(SocketInputStream.java:168)
    at weblogic.socket.PosixSocketMuxer.readBytesProblem(PosixSocketMuxer.java:889)
    at weblogic.socket.PosixSocketMuxer.deliverGoodNews(PosixSocketMuxer.java:774)
    at weblogic.socket.PosixSocketMuxer.processSockets(PosixSocketMuxer.java:700)
    at weblogic.socket.SocketReaderRequest.execute(SocketReaderRequest.java:23)
    at weblogic.kernel.ExecuteThread.execute(ExecuteThread.java:213)
    at weblogic.kernel.ExecuteThread.run(ExecuteThread.java:189)
    ####<Oct 11, 2006 11:15:45 AM EDT> <Error> <HTTP> <spdevapp1> <pmapp1> <ExecuteThread: '5' for queue: '__weblogic_admin_rmi_queue'> <kernel identity> <> <101083> <Connection failure>
    java.net.SocketException: Connection reset
    Start server side stack trace:
    java.net.SocketException: Connection reset
    at java.net.SocketInputStream.read(SocketInputStream.java:168)
    at weblogic.socket.PosixSocketMuxer.readBytesProblem(PosixSocketMuxer.java:889)
    at weblogic.socket.PosixSocketMuxer.deliverGoodNews(PosixSocketMuxer.java:774)
    at weblogic.socket.PosixSocketMuxer.processSockets(PosixSocketMuxer.java:700)
    at weblogic.socket.SocketReaderRequest.execute(SocketReaderRequest.java:23)
    at weblogic.kernel.ExecuteThread.execute(ExecuteThread.java:213)
    at weblogic.kernel.ExecuteThread.run(ExecuteThread.java:189)
    End server side stack trace
    at java.net.SocketInputStream.read(SocketInputStream.java:168)
    at weblogic.socket.PosixSocketMuxer.readBytesProblem(PosixSocketMuxer.java:889)
    at weblogic.socket.PosixSocketMuxer.deliverGoodNews(PosixSocketMuxer.java:774)
    at weblogic.socket.PosixSocketMuxer.processSockets(PosixSocketMuxer.java:700)
    at weblogic.socket.SocketReaderRequest.execute(SocketReaderRequest.java:23)
    at weblogic.kernel.ExecuteThread.execute(ExecuteThread.java:213)
    at weblogic.kernel.ExecuteThread.run(ExecuteThread.java:189)
    but this only came from <pmapp1>, which is a managed server running on the same host. <pmapp2> running on a separate host in the same cluster does not exhibit this problem.
    Also the same errors are not seen if <pmapp1> is started with JDK1.3.1. The following table summarizes the scenarios I tested:
    admin VM: 1.3.1 1.4.2
    pmapp1 VM:
    1.3.1 ok ok
    1.4.2 error error
    All other managed server instances, including <pmapp2>, seems to be impervious to the JVM versions.
    Any thoughts on this?

    Hi,
    Check this link :
    Re: myrealm error when start through node manager

Maybe you are looking for