Sending information over DataOutputStream throws e

2019-09-19 14:05发布

Consider you have the following code implementation of a timer in a net game:

public void DefineTimer()
    {
            Action updateClockAction = new AbstractAction() {
                public void actionPerformed(ActionEvent e){

                    //System.out.println(javax.swing.SwingUtilities.isEventDispatchThread());

                     JPanelMainGame.this.jLabelSeconds.setFont(new java.awt.Font("Lucida Handwriting", 1, 36));
                     JPanelMainGame.this.jLabelSeconds.setForeground(Color.red);
                     JPanelMainGame.this.jLabelSeconds.setText(Integer.toString(JPanelMainGame.this.m_TimerTotalSeconds));

                    if( JPanelMainGame.this.m_TimerTotalSeconds >0)
                    {
                         JPanelMainGame.this.m_TimerTotalSeconds--;
                    }
                    else if ( JPanelMainGame.this.m_TimerTotalSeconds == 0)
                    {
                       // System.out.println(javax.swing.SwingUtilities.isEventDispatchThread());
                        JPanelMainGame.this.m_Timer.stop();
                        JPanelMainGame.this.jLabelSeconds.setText("0");
                        JPanelMainGame.this.jButtonFinish.setVisible(false);
                        System.out.println("after JbuttonFinish set visble false");
                        System.out.println("!m_WasGameDecisived: "+!m_WasGameDecisived);
                       // System.out.println(javax.swing.SwingUtilities.isEventDispatchThread());
                        JPanelGameApplet gameApplet = (JPanelGameApplet) getTopLevelAncestor();
                        //Checking whether time ended for both players and no solution was recieved

                        if(gameApplet.GetJPanelChooseGame().GetGameType() == eGameType.Net)
                        {

                            gameApplet.GetClinetThread().UpdateServerOfTimeEnded();
                            System.out.println("After Update");
                            if (!m_WasGameDecisived)
                            {
                                // System.out.println(javax.swing.SwingUtilities.isEventDispatchThread());
                                System.out.println("Tie - No one had a solution in the given time");


                                gameApplet.GetClinetThread().SendRequestToClosePlayerThreadAndRemoveItFromPlayersOnServer();
                                System.out.println("After SendRequestToClosePlayerThread");
                                gameApplet.GetClinetThread().CloseSocket();
                                System.out.println("After CloseSocket");
                                Menu.BrowseTo(PanelMenuNumber.k_ChooseGame, JPanelMainGame.this.getParent());
                                //askUserForAnotherRoundLeaveTableOrExitProgram();//////////////////////////////////////////////To implement
                            }
                        }
                        else if(gameApplet.GetJPanelChooseGame().GetGameType() == eGameType.Single)
                        {
                            JPanelMainGame.this.showPopUpSelectionBar();

                        }
                    }
                }
            };
            m_Timer = new Timer(1000, updateClockAction);
    }  

I have added these functions (which write to the same DataOutputStream:

gameApplet.GetClinetThread().UpdateServerOfTimeEnded();

gameApplet.GetClinetThread().SendRequestToClosePlayerThreadAndRemoveItFromPlayersOnServer();

After adding them I get inconsistent exceptions: Sometimes I get exception in the client because one of the functions, or both or not having any exception at all.
Please help me figure out the problem
Example for exceptions I get from time to time:

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:851)
        at GUI.JPanelMainGame$2.actionPerformed(JPanelMainGame.java:313)
        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:825)
        at GUI.JPanelMainGame$2.actionPerformed(JPanelMainGame.java:327)
        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)

1条回答
Anthone
2楼-- · 2019-09-19 14:28

'From within a Swing timer' indicates a design flaw as per the comments to your question, but the exception itself is simply a network error and has nothing to do with Swing at all. There is a Microsoft Knowledge Base article about it, which you should read, but in brief this exception with this error text indicates that the local TCP stack has given up writing to that connection due to prior network send problems.

查看更多
登录 后发表回答