使用连接 jsch 从服务器向客户端写入多个输出?

问题描述 投票:0回答:0

我正在尝试使用 JSCH 连接从客户端向服务器发送多条消息。 第一条消息被发送得很大。但是,对于第二条消息/第三条消息,我得到 EOF 异常

连接正常,我可以发送第一条消息。 奴隶的例子

connection.out.writeObject( " message number " + 0 + " " + InetAddress.getLocalHost());
connection.out.flush();

被主人收到了

但是如果我想做类似的事情

while (i<=5)
{
   connection.out.writeObject(i + "  " + InetAddress.getLocalHost());
   connection.out.flush();
   i++;
}

这些是 OnlineTicTacToe 类的构造函数

public OnlineTicTacToe() throws IOException, InterruptedException, ClassNotFoundException {
        // receive an ssh2 connection from a user-local master server.
        Connection connection = new Connection();
    
       
        // list to collect the available button numbers from the remaining buttons
        // available
        // ArrayList<Integer> list = new ArrayList<>();
        // for debugging, always good to write debugging messages to the local file
        // don't use System.out that is a connection back to the client.
        PrintWriter logs = new PrintWriter(new FileOutputStream("logss.txt"));
        logs.println("Autoplay: got started.");
        logs.flush();
        myMark = "X"; // auto player is always the 2nd.
        yourMark = "O";
        //myTurn[0] = true;
        logs.println("Starting the counterpart code....");
        logs.flush();
           
        // auto play implementation : the person is playing consecutive turns 
        // i.e 0 -> 1 -> 2
                while ( true)
                {
                    if(myTurn[0]) {
                        
                        connection.in.readObject();
                          continue;
                    }
                    else{
                        logs.println("remote user typed" + i);
                        connection.out.writeObject(i);
                        connection.out.flush();
                        i++;
                     
                        myTurn[0] = !myTurn[0];
                    }
                
                
         }
    }
    
    public OnlineTicTacToe(String hostname) throws IOException  {
        final int JschPort = 22; // Jsch IP port
        // isAuto = 1;
//        jschCounter = 1; // to keep track of jsch connection in actionPerformed
        // Read username, password, and a remote host from keyboard
        Scanner keyboard = new Scanner(System.in);
        String username = null;
        String password = null;
        // The JSCH establishment process is pretty much the same as Lab3.
        // IMPLEMENT BY YOURSELF
       try {
        //    read the user name from the console
           System.out.print("User: ");
           username = keyboard.nextLine();
        //    read the password from the console
           Console console = System.console();
           password = new String(console.readPassword("Password: "));
       } catch (Exception e) {
           e.printStackTrace();
           System.exit(-1);
       }
       
        // A command to launch remotely:
        // java -cp ./jsch-0.1.54.jar:. JSpace.Server
        String cur_dir = System.getProperty("user.dir");
        String command = "java -cp " + cur_dir + "/jsch-0.1.54.jar:" + cur_dir + " OnlineTicTacToe";
        // establish an ssh2 connection to ip and run
        // Server there.
        Connection connection = new Connection(username, password, hostname, command);
        // the main body of the master server
        input = connection.in;
        output = connection.out;
        // set up a window
        makeWindow(true); // I'm a former
        // // start my counterpart thread
        // myTurn[0] = false; 
        Counterpart counterpart = new Counterpart();
        counterpart.start();

    }

 private class Counterpart extends Thread implements Serializable {

        @Override
        public void run() {
            while (true) {
                synchronized (myTurn) {
                    try {
                         if (myTurn[0]) {
                            myTurn.wait();
                            
                        } 
                            System.out.println("Running counterpart");
                            System.out.println("data available ---"+input.available());
                            boolean marked = false; 
                            // while  ( input.available() <= 0 )  myTurn.wait();
                              
                                Object object = input.readObject();
                                int  buttonClickedOpp = (Integer) object;

                            System.out.println("Opponent played button " + buttonClickedOpp);
                            marked = markButton(buttonClickedOpp, yourMark);

                            if (marked) {
                                boolean won = checkForWin(yourMark);

                                if (won) {
                                    showWon(yourMark);
                                }
                                if (won) {
                                    replayAgain();
                                }
                                myTurn[0] = !myTurn[0];
                                myTurn.notifyAll();
                                // System.out.println("Opponent's turn over.");
                            }
                        

                    } catch (IOException | InterruptedException | ClassNotFoundException  e) {
                        System.err.println(e);
                    }
                }
            }
        }

Counterpart 线程处理在客户端读取远程服务器写入的消息的情况。

万一我有一条消息,它工作正常但是,对于多条消息,它给出了文件结束异常。

java connection jsch
© www.soinside.com 2019 - 2024. All rights reserved.