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 5 of 5

Thread: server/client application fails when client closes

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default server/client application fails when client closes

    hi all,

    I made an application wich is a server that accepts many clients, when one of those clients wants to close the connection, sends an specified string to the server and this closes the connection. That works perfectly, the problem is when the client closes the connection without sending the string, then the server knows nothing about this and keeps the usless socket open for ever.
    I can't find in the literature how the server is notified when some client closes unexpectedly the connection. Any help will be just great.

    Thaks, billykid.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: server/client application fails when client closes

    One way would be to have a task on a timer to test each socket's state every so often. No idea what to test. You'd have to experiment.

  3. #3
    Junior Member
    Join Date
    Aug 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: server/client application fails when client closes

    Thanks for your reply Norm. I actually use a dedicated thread to control the socket connected to every client. I use a loop where I read the socket waiting for a data input and that loop is finished when the socket is closed for another thread of my program (checking the "Socket.isClosed()" method) or there is an error in the reading of the socket.
    I thought checking the socket state in the loop, the problem is none propertie of the socket changes when the client closes unexpectedly so I can't know when that happens.
    I tried setting a time out for the connection using the "Socket.setSoTimeout()" method and it works ok, but i don't want that, I want the connection to be open until the client decide to close it even if it keeps inactive for a long time.

    Any idea?
    Thans again, Billykid

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: server/client application fails when client closes

    Write some test code to see what you can detect about the state of the connection after the client closes it.

  5. #5
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: server/client application fails when client closes

    Well a while ago Inwas working on a server+client application and ran into that problem.. What I eventually ended up doing was have the client ping the server if nothing else was going on (if the text field hadn't changed and nothing was sent recently) by using an int that was designated for that purpose. however it seems Norm has a better idea and I would be quite interested to find out!

Similar Threads

  1. Basic client/server application
    By TheRedFox in forum Java Networking
    Replies: 1
    Last Post: November 25th, 2011, 09:40 AM
  2. Server-Client Chat application using UDP
    By weakprogrammer in forum Java Networking
    Replies: 3
    Last Post: July 1st, 2011, 02:35 PM
  3. Threads in multi client-server application
    By KingOfClubs in forum Threads
    Replies: 1
    Last Post: June 11th, 2011, 12:10 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. Problems with client/server application
    By lori in forum AWT / Java Swing
    Replies: 3
    Last Post: January 15th, 2010, 01:12 PM