I’m designing a net game so I have a client with one listening thread to things that are coming from server, also the server has a listening thread which is listening to messages from the client.
In the one of the Client panels I do the following:
sending some message to server and after two lines of code from the same place do it again with other function.
The result is a run of code that sometimes works well and sometimes throw an exception.
The exception is being thrown at the line of one of the sending functions on the client panel.
Note: if I place Thread.sleep(1000); between the two sends than no exception is thrown,but as you know it’s a bad solution..
* The two functions that sends info to the one and only server thread are:
ClientCommunicationThread.UpdateServerOfTimeEnded
ClientCommunicationThread.SendRequestToClosePlayerThreadAndRemoveItFromPlayersOnServer
How can I solve this problem
Thanks.
This is the stacktrace:
java.net.SocketException: Software caused connection abort: socket write error
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92)
at java.net.SocketOutputStream.write(SocketOutputStream.java:115)
at java.io.DataOutputStream.writeInt(DataOutputStream.java:181)
at GUI.ClientCommunicationThread.UpdateServerOfTimeEnded(ClientCommunicationThread.java:850)
at GUI.JPanelMainGame$2.actionPerformed(JPanelMainGame.java:312)
at javax.swing.Timer.fireActionPerformed(Timer.java:271)
at javax.swing.Timer$DoPostEvent.run(Timer.java:201)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
java.net.SocketException: Software caused connection abort: socket write error
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92)
at java.net.SocketOutputStream.write(SocketOutputStream.java:115)
at java.io.DataOutputStream.writeInt(DataOutputStream.java:180)
at GUI.ClientCommunicationThread.SendRequestToClosePlayerThreadAndRemoveItFromPlayersOnServer(ClientCommunicationThread.java:824)
at GUI.JPanelMainGame$2.actionPerformed(JPanelMainGame.java:325)
at javax.swing.Timer.fireActionPerformed(Timer.java:271)
at javax.swing.Timer$DoPostEvent.run(Timer.java:201)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
Assumption : This happens because you are using same socket object for both functions, And both functions wants to write something to socket output stream. I hope I am right on this. Based on this my go would be using synchronisation. Below link will guide you through some of the way to have locks on objects so that two different thread cannot access same object together. You create implicit lock and than release when you done with it. 🙂 And of course you can create two different threads to calls two different functions. ( More safer…)
Link : Intrinsic Locks and Synchronization
Hope this will help you to solve.