Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: problem with closing connection to client socket

  1. #1
    Junior Member
    Join Date
    Dec 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default problem with closing connection to client socket

    Hello
    I am new to java networking domain.
    I am having an issue with my code.My problem is
    I created a server that can accept messages from multiple clients using multithreading.
    I want to close a client when it is not sending any data to the server for 2 minutes. That means when the client is idle for 2 minutes.
    Please help me if there is any way.

    Thanks in advance.

    Sunitha


  2. #2
    Junior Member
    Join Date
    Dec 2010
    Location
    New York
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: problem with closing connection to client socket

    On your server side class when the clients socket is created/connected you can call setSOTimeout(timeout in milliseconds) on the clients socket.
    When you call the read method on that socket's InputStream if no data is read/received with in that SO Timeout then the
    read method for that stream will throw a SocketTimeoutException , but the Socket is not closed.
    If you catch that exception then you can do something such as close that client's connection.
    The Java API reads....
    Enable/disable SO_TIMEOUT with the specified timeout, in milliseconds. With this option set to a non-zero timeout, a read() call on the InputStream associated with this Socket will block for only this amount of time. If the timeout expires, a java.net.SocketTimeoutException is raised, though the Socket is still valid. The option must be enabled prior to entering the blocking operation to have effect. The timeout must be > 0. A timeout of zero is interpreted as an infinite timeout.

Similar Threads

  1. jdbc connection problem
    By sny in forum JDBC & Databases
    Replies: 11
    Last Post: January 6th, 2011, 04:39 AM
  2. Partial messages problem with Java non-blocking socket communication
    By perl0101 in forum What's Wrong With My Code?
    Replies: 14
    Last Post: July 28th, 2010, 03:38 PM
  3. [SOLVED] frame closing programmatically
    By nasi in forum AWT / Java Swing
    Replies: 9
    Last Post: May 10th, 2010, 03:43 AM
  4. Client Socket on Application Server
    By xevimaresma in forum Java Theory & Questions
    Replies: 0
    Last Post: April 12th, 2010, 07:00 AM
  5. InputStream Problem at Client Side
    By pavan in forum Web Frameworks
    Replies: 1
    Last Post: March 26th, 2010, 03:21 AM