Socket Client = new Socket("ip", port);
private ObjectOutputStream out;
out = new ObjectOutputStream(Client.getOutputStream());//line3
out.flush();


我有一个套接字程序。当我有一个客户端时没有问题。但是当客户端超过一秒钟,而后来的客户端时,该程序在第3行上是waiting它没有错误和异常。什么问题?

        TCP = new Thread(new Runnable() {

            @Override
            public void run() {
           if ("TCP".equals(Protocol)) {
            try {
                Thread.sleep(10);
            } catch (InterruptedException ex) {
                Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
            }

            RunServer();

        }
            }
        });
        TCPWait = new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    WaitForConnection();
                } catch (IOException ex) {
                    Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
        TCPWait.start();
        TCP.start();
    public void RunServer() {
        try {

            while (true) {
                {
                    accessLock.lock();
                    if(CanContinue == false)
                        condition.await();
                    try {
//                        WaitForConnection();
                        GetStreams();
                        ProcessConnection();
                    accessLock.unlock();

                    } catch (Exception e) {
                        JOptionPane.showMessageDialog(rootPane, "ارتباط قطع شد" + e.getMessage());


                    }
                }
            }

        } catch (Exception e) {
            JOptionPane.showMessageDialog(rootPane, e.getMessage());
        }
    public void WaitForConnection() throws IOException {
        jTextArea1.append("لطفا تا برقرای ارتباط صبر کنید.../n ");
        accessLock.lock();
        connection = server.accept();
       CanContinue = true;
       condition.signal();
       accessLock.unlock();

        jTextArea1.append("ارتباط از " + connection.getInetAddress().getHostName() + "\n");

    }

    public void GetStreams() {
        try {
            Socket s = new Socket();
            s = connection;
            out = new ObjectOutputStream(s.getOutputStream());
            out.flush();

            in = new ObjectInputStream(s.getInputStream());
            jTextArea1.append("در حال برقرای ارتباط...\n");
        } catch (Exception e) {
            JOptionPane.showMessageDialog(rootPane, "مشکل در برقرای ارتباط!\n");

        }

    }

    public void ProcessConnection() {
        SendData("ارتباط برقرا شد\n");
        try {
            jTextArea1.append((String) in.readObject() + "\n");

        } catch (Exception e) {
        }
        String message = "";

        do {
            try {

                jTextArea1.append((String) in.readObject() + "\n");
            } catch (Exception e) {
            }
        } while (message != "Exit");


    }


WaitForConnection()是线程中的侦听器,而runserver()发送和接收另一个线程中的消息。

最佳答案

据我所知,您在此线程中仅等待一次连接:

    TCPWait = new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                WaitForConnection();
            } catch (IOException ex) {
                Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    });


而不是在主循环中等待多个连接:

        while (true) {
            {
                accessLock.lock();
                if(CanContinue == false)
                    condition.await();
                try {
        //            WaitForConnection();
                    GetStreams();
                    ProcessConnection();
                accessLock.unlock();

                } catch (Exception e) {
                    JOptionPane.showMessageDialog(rootPane, "ارتباط قطع شد" + e.getMessage());


                }
            }
        }


因此,一个客户端连接,然后其余客户端卡住,因为您再也不会调用accept。您应该取消对WaitForConnection();的调用的注释

关于java - ObjectOutputStream是否需要服务器响应?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9421416/

10-09 04:46