【问题标题】:My server stops working when reading .accept(), using Sockets我的服务器在使用 Sockets 读取 .accept() 时停止工作
【发布时间】: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
}

【问题讨论】:

    标签: java sockets client


    【解决方案1】:

    ServerSocket#accept 是一个阻塞调用。它侦听端口并在客户端连接时返回Socket。您没有展示太多的服务器逻辑,但似乎您将客户端放在一个数组中,因此您显然希望支持多个客户端。您不会显示您的 Client 类是否启动线程并立即返回。

    您应该有一个服务器循环,它只侦听服务器套接字并在检索到客户端套接字之后创建客户端。即使您在 Client 构造函数中执行此操作(没有代码我无法判断),这也不是一个很好的地方,并且严重阻碍了调试。

    如果您不为您的客户端启动线程,这将解释服务器“停止”(如果“停止”表示“阻塞”而不是“崩溃”)。详细解释见"Writing the Server Side of a Socket" in the Java Tutorial

    我想不出为什么它在通过 Netbeans 启动时表现不同。需要更多的代码上下文。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-09-08
      • 1970-01-01
      • 2013-01-31
      • 2014-01-02
      • 1970-01-01
      • 2015-02-08
      • 1970-01-01
      相关资源
      最近更新 更多