Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用“时,程序正在运行”;踏入;在调试模式下,但在运行时不是。(插座)_Java_Sockets - Fatal编程技术网

Java 使用“时,程序正在运行”;踏入;在调试模式下,但在运行时不是。(插座)

Java 使用“时,程序正在运行”;踏入;在调试模式下,但在运行时不是。(插座),java,sockets,Java,Sockets,我有三个类:服务器、客户端和Cap。我正在尝试通过套接字将Cap从服务器发送到客户端并返回。服务器实现runnable,在run方法中有一个无限while循环,如下所示: try { out.writeObject(new Cap()); out.flush(); Cap cap = (Cap) in.readObject(); boolean bol = false; if(cap.getValue()==2) { bol = true; } out.writeBoolean(b

我有三个类:服务器、客户端和Cap。我正在尝试通过套接字将Cap从服务器发送到客户端并返回。服务器实现runnable,在run方法中有一个无限while循环,如下所示:

try {
out.writeObject(new Cap());
out.flush();

Cap cap = (Cap) in.readObject();

boolean bol = false;

if(cap.getValue()==2) {

 bol = true;
 }

out.writeBoolean(bol);
out.flush();
} catch (IOException ex) {
                JOptionPane.showMessageDialog(null, "Something is wrong");
                break;
} catch (ClassNotFoundException ex) {JOptionPane.showMessageDialog(null, "Something is wrong");}

}
try {

     Cap cap = (Cap) in.readObject();

     textBox.setText(cap.toString());

     while(!JButtonHasBeendPressed) {

      }

      out.writeObject(cap);
      out.flush();
      bol = in.readBoolean();

      if(bol) {

       //Do something
      }
      else {
       //Do something else
       }
       JButtonHasBeendPressed = false;

   } catch (IOException ex) {
       //Do something
   } catch (ClassNotFoundException ex) {
      //Do something
   }
 }
在run方法中,但在while循环之前,我执行了以下操作:

out = new ObjectOutputStream(socket.getOutputStream());
in = new ObjectInputStream(socket.getInputStream())
out = new ObjectOutputStream(socket.getOutputStream());
in = new ObjectInputStream(socket.getInputStream());
在客户端类中,我还有一个while循环,如下所示:

try {
out.writeObject(new Cap());
out.flush();

Cap cap = (Cap) in.readObject();

boolean bol = false;

if(cap.getValue()==2) {

 bol = true;
 }

out.writeBoolean(bol);
out.flush();
} catch (IOException ex) {
                JOptionPane.showMessageDialog(null, "Something is wrong");
                break;
} catch (ClassNotFoundException ex) {JOptionPane.showMessageDialog(null, "Something is wrong");}

}
try {

     Cap cap = (Cap) in.readObject();

     textBox.setText(cap.toString());

     while(!JButtonHasBeendPressed) {

      }

      out.writeObject(cap);
      out.flush();
      bol = in.readBoolean();

      if(bol) {

       //Do something
      }
      else {
       //Do something else
       }
       JButtonHasBeendPressed = false;

   } catch (IOException ex) {
       //Do something
   } catch (ClassNotFoundException ex) {
      //Do something
   }
 }
在while循环之前,我做了以下操作:

out = new ObjectOutputStream(socket.getOutputStream());
in = new ObjectInputStream(socket.getInputStream())
out = new ObjectOutputStream(socket.getOutputStream());
in = new ObjectInputStream(socket.getInputStream());
当我调试客户机程序时,它完全按照我想要的方式工作——程序进入“if(bol)”中。当我运行程序时,服务器可以将对象发送到客户机,但之后出现了问题。正如您在代码中所看到的,我在客户机类中有一个JButton,即设置JButtonHasBeendPressed=true,但当我按下它时不会发生任何事情。我没有收到任何错误消息,实际上什么也没有发生

当我调试服务器和客户机时,out.writebolean(bol)发生IOException;在服务器类中


Hank

服务器是否连接到客户端?是的,示例中的服务器实际上是客户端连接到实际服务器时创建的ClientCommunicate。它是通过accept()-method连接的。问题似乎在于等待按钮按下的while循环。基本上outputstream对象正在流上写入。是否要在客户端控制台上写入?