通过 DataOutputStream 发送信息会引发异常
假设您在网络游戏中具有以下计时器的代码实现:
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)
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据对您的问题的评论,“来自 Swing 计时器内”表明存在设计缺陷,但异常本身只是一个网络错误,与 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.