• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Having trouble closing socket properly

 
Ranch Hand
Posts: 273
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Need help closing the server application properly. I want to keep running the server while (!done). It runs it just won't process more than one request from the client. What should I change in order to perform more than one request for the client.

 
Ranch Hand
Posts: 262
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm relatively new to Socket programming but it looks like you're closing the socket inside the third if statement. Doesn't look like a good idea to me. Is it a logic error or something?
 
Charles Sexton
Ranch Hand
Posts: 273
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Heena Agarwal wrote:I'm relatively new to Socket programming but it looks like you're closing the socket inside the third if statement. Doesn't look like a good idea to me. Is it a logic error or something?



the socket closes right before the end of while(!done)
 
Heena Agarwal
Ranch Hand
Posts: 262
4
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Charles Sexton wrote:

Heena Agarwal wrote:I'm relatively new to Socket programming but it looks like you're closing the socket inside the third if statement. Doesn't look like a good idea to me. Is it a logic error or something?



the socket closes right before the end of while(!done)



Charles Sexton wrote:


Somehow every time I look at your code, it looks to me that you are closing the socket inside the third if loop. But it's possible that I'm not seeing that closing brace somewhere..

Anyway, I'll pass ..
 
Charles Sexton
Ranch Hand
Posts: 273
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Heena Agarwal wrote:

Charles Sexton wrote:

Heena Agarwal wrote:I'm relatively new to Socket programming but it looks like you're closing the socket inside the third if statement. Doesn't look like a good idea to me. Is it a logic error or something?



the socket closes right before the end of while(!done)



Charles Sexton wrote:


Somehow every time I look at your code, it looks to me that you are closing the socket inside the third if loop. But it's possible that I'm not seeing that closing brace somewhere..

Anyway, I'll pass ..



You are right....I apologize.....
 
Charles Sexton
Ranch Hand
Posts: 273
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

This is the error I receive when trying to perform more than one request for the client.....I moved the socket.close(); outside the third if statement but still receive the same error.....I know it has to do something with the while(true) or while(!done) loops.....

java.net.SocketException: Software caused connection abort: recv failed
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(SocketInputStream.java:152)
at java.net.SocketInputStream.read(SocketInputStream.java:122)
at java.net.SocketInputStream.read(SocketInputStream.java:210)
at java.io.DataInputStream.readUnsignedShort(DataInputStream.java:337)
at java.io.DataInputStream.readUTF(DataInputStream.java:589)
at java.io.DataInputStream.readUTF(DataInputStream.java:564)
at client_sexton_it351.StreamSocket.receiveMessage(StreamSocket.java:68)
at client_sexton_it351.Client_Sexton_IT351.displayCustomer(Client_Sexton_IT351.java:40)
at client_sexton_it351.ClientGUI.customerDisplayButtonActionPerformed(ClientGUI.java:243)
at client_sexton_it351.ClientGUI.access$200(ClientGUI.java:21)
at client_sexton_it351.ClientGUI$3.actionPerformed(ClientGUI.java:85)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3320)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
BUILD SUCCESSFUL (total time: 11 seconds)
 
Heena Agarwal
Ranch Hand
Posts: 262
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Charles Sexton wrote:This is the error I receive when trying to perform more than one request for the client.....I moved the socket.close(); outside the third if statement but still receive the same error.....I know it has to do something with the while(true) or while(!done) loops.....

java.net.SocketException: Software caused connection abort: recv failed
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(SocketInputStream.java:152)
at java.net.SocketInputStream.read(SocketInputStream.java:122)



Moving socket.close() out of the if loop alone will not solve your problem. That stack trace shows that the connection is closed and you're still trying to receive messages.

So the socket has closed because you are closing it. And what determines that all the requests are processed, the done flag. How are you setting the done flag? It's a local variable I see. So there is no way, an outside entity can set it. Right?

You'd want to set it only after all the requests have completed. Also that is when you'd want to close the connection too. Would that be right?

If so, is your code doing that?
 
Charles Sexton
Ranch Hand
Posts: 273
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Heena Agarwal wrote:

Charles Sexton wrote:This is the error I receive when trying to perform more than one request for the client.....I moved the socket.close(); outside the third if statement but still receive the same error.....I know it has to do something with the while(true) or while(!done) loops.....

java.net.SocketException: Software caused connection abort: recv failed
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(SocketInputStream.java:152)
at java.net.SocketInputStream.read(SocketInputStream.java:122)



Moving socket.close() out of the if loop alone will not solve your problem. That stack trace shows that the connection is closed and you're still trying to receive messages.

So the socket has closed because you are closing it. And what determines that all the requests are processed, the done flag. How are you setting the done flag? It's a local variable I see. So there is no way, an outside entity can set it. Right?

You'd want to set it only after all the requests have completed. Also that is when you'd want to close the connection too. Would that be right?

If so, is your code doing that?



I need the connection to close when all requests are done so you are right. The done Boolean variable is a local variable so their is no way for an outside entity to modify this variable unless it was through the I/O stream correct? If that was the case then when the user closes the client set done = to true.....?
 
Heena Agarwal
Ranch Hand
Posts: 262
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Charles Sexton wrote:

I need the connection to close when all requests are done so you are right. The done Boolean variable is a local variable so their is no way for an outside entity to modify this variable unless it was through the I/O stream correct? If that was the case then when the user closes the client set done = to true.....?



Yes. But how do you know all requests are processed and now is the time to close the connection. You need to know that to put the socket.close() at the right place and to ensure that the done flag is set at the right time.
Since it's a local variable, you must have the logic within your method.

I could not understand the following part--

The done Boolean variable is a local variable so their is no way for an outside entity to modify this variable unless it was through the I/O stream correct?



If that was the case then when the user closes the client set done = to true.....?


But done is a local variable. Does your client have any means of setting it?

I'm just saying this by looking at your code. I have no idea of how it is supposed to function. You're the best person for answering that question.

 
Charles Sexton
Ranch Hand
Posts: 273
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Heena Agarwal wrote:

Charles Sexton wrote:

I need the connection to close when all requests are done so you are right. The done Boolean variable is a local variable so their is no way for an outside entity to modify this variable unless it was through the I/O stream correct? If that was the case then when the user closes the client set done = to true.....?



Yes. But how do you know all requests are processed and now is the time to close the connection. You need to know that to put the socket.close() at the right place and to ensure that the done flag is set at the right time.
Since it's a local variable, you must have the logic within your method.

I could not understand the following part--

The done Boolean variable is a local variable so their is no way for an outside entity to modify this variable unless it was through the I/O stream correct?



If that was the case then when the user closes the client set done = to true.....?


But done is a local variable. Does your client have any means of setting it?

I'm just saying this by looking at your code. I have no idea of how it is supposed to function. You're the best person for answering that question.



I want the connection to remain open until the GUI is closed. The user closes the GUI by pushing the close button. This would allow the user to make more than one request through the client.
 
Heena Agarwal
Ranch Hand
Posts: 262
4
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Charles Sexton wrote:

I want the connection to remain open until the GUI is closed. The user closes the GUI by pushing the close button. This would allow the user to make more than one request through the client.



Then I guess you will have to design it to work accordingly. I can't see your code so the best I can do is speculate. You have a local done flag in your server code. I don't know if the user should send a certain specific message that should denote that the user is done sending requests to the server and then the server should act accordingly. It is your design. You must know it.

Also you are creating your data socket in the same thread in which your server is accepting client connections. You'd probably want to have a separate thread per client or you'd probably want to use a thread pool. Even if it is a practice assignment, you'd probably want to have the processing in a different thread?

You might also need to handle the race conditions( if applicable ), but you would know about them.

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic