通过DataOutputStream发送信息会引发异常

时间:2011-10-09 21:01:48

标签: java multithreading swing sockets timer

考虑一下网络游戏中有一个计时器的以下代码实现:

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);
    }  

我添加了这些函数(写入相同的DataOutputStream:

gameApplet.GetClinetThread().UpdateServerOfTimeEnded();

gameApplet.GetClinetThread().SendRequestToClosePlayerThreadAndRemoveItFromPlayersOnServer();

添加后我得到不一致的例外: 有时候我会在客户端因为其中一个功能,或者两者兼而有之,或者根本没有任何例外 请帮我弄清楚问题 我不时得到的例外情况的例子:

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 个答案:

答案 0 :(得分:3)

'来自Swing计时器'表示根据您的问题的评论设计缺陷,但异常本身只是一个网络错误,与Swing完全无关。有关于它的Microsoft知识库文章,您应该阅读,但简而言之,此错误带有此错误文本表示由于先前的网络发送问题,本地TCP堆栈已放弃写入该连接