【发布时间】:2012-03-01 23:50:32
【问题描述】:
请原谅我的写作错误... 我正在使用 NetBeans 运行自制服务器和客户端,一切正常。正如我之前所说,我在客户端上使用“Socket”,在我的 sv 上使用“ServerSocket”。我还在服务器中使用 JDBC Mysql。 当我在它们的可分发文件夹中生成两个 java 文件并使用它们时,问题就开始了。客户端向服务器发送信息(它以 .getOutputStream() 和 .getInputStream() 开头,然后是 .flush() ),但服务器没有收到任何消息。我试着看看它停在哪里,它在
clients[i] = new Client (server.accept(), i);
当我尝试从 NetBeans 执行我的服务器并从我的桌面执行客户端时,发生了疯狂的事情……它有效!所以肯定是服务器的问题。我也在使用一个开放的 UDP 端口,我正在寻找 192.168.0.10 上的服务器 IP(这是我的计算机,在 LAN 中)。
希望有人能帮助我,在此先感谢!
我在这里粘贴我的代码,很抱歉有些变量是西班牙语:
public ServidorMultiCliente() { super("服务器多客户端"); 初始化组件();
try{ servidor = new ServerSocket( 500, maxNumberClients); }catch(IOException excepcion){ excepcion.printStackTrace(); System.exit(1); } serverWriteBox.append("Server iniciated, waiting for connections..."); }
我从服务器运行这些:
公共无效 ejecutar(){ clientsAcceptor.start(); 消息监听器.start(); }
clientsAcceptor 在哪里:
私有类 aceptadorClientes 扩展线程{
public void run(){ for( int i = 1; i < maxNumberClients; i++ ){ try{ clientes[i] = new Cliente (servidor.accept(), i); // **Here it stops** // It never reaches here... (without using NetBeans) clientes[i].start(); clientes[i].aceptado = true; }catch(IOException excepcion){ excepcion.printStackTrace(); } }
这就是我在不同线程中接受客户的方式。我用 messageListener 做同样的事情,它是每个新客户的新线程。它在一个循环中,总是在听。在这里我粘贴我的可执行客户端,它与我在 ServidorMultiCliente 中使用的 Cliente 类不同:
公共客户(){ }
public Cliente(String host){ this.host = host; this.executeConnection(); } public void executeConnection(){ int connect = 0; try { cliente = new Socket(InetAddress.getByName(host), 500); conectar = 1; } catch (IOException ex) { conectar = 0; this.ejecutarConexion(); } if(conectar == 1){ obtainFlows(); } }private void 获得Flows(){
try{ output= new ObjectOutputStream( cliente.getOutputStream()); output.flush(); // Here I should be "connected" input = new ObjectInputStream(cliente.getInputStream()); } catch(IOException ex){ this.initiateDisconnection(); } sendFlows("I'm connected!"); new messageListener().start(); // This is a thread }
【问题讨论】: