通过DataOutputStream发送信息会引发异常 [英] Sending information over DataOutputStream throws exception

查看:81
本文介绍了通过DataOutputStream发送信息会引发异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑到您在网络游戏中具有以下计时器的代码实现:

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

我添加了这些功能(它们写入相同的DataOutputStream:

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)

推荐答案

来自Swing计时器中的计时器"表明对您的问题的注释存在设计缺陷,但异常本身只是网络错误,与无关与摇摆.有一篇有关它的Microsoft知识库文章,您应该阅读,但简而言之,此异常带有此错误文本表示由于先前的网络发送问题,本地TCP堆栈已放弃对该连接的写入

'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.

这篇关于通过DataOutputStream发送信息会引发异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆