java 中的 Websocket 程序没有响应

Websoket program in java is not responding

我在 java 中练习 WebSocket 并编写了两个简单的程序,其中一个是一个服务器,它开始侦听端口 9090 并从客户端接收一个字符串作为输入,然后将字符串变成大写 returns 返回给客户端。

程序没有错误,但由于某种原因,无法运行。我找不到问题所在。

首次亮相后,即使双方成功连接,服务器似乎也没有收到输入字符串。当用户输入 his/her 字符串时,客户端将其发送到服务器,但服务器未接收到它,因此,两个程序都进入了想要的阶段,应用程序将不会响应。

你能帮我解决这个问题吗? 提前致谢

服务器端代码:

    import java.io.*;
    import java.net.ServerSocket;
    import java.net.Socket;
    
    public class Main {
    
        public static void main(String[] args) throws IOException {
        // Start listening at port 9090
            ServerSocket s = new ServerSocket(9090);
            System.out.println("Started: " + s);
            Socket socket = s.accept();
            System.out.println("conecction accepted by " + socket);
    
            try {
                // Buffer reader to get input and save it as "in"
                BufferedReader in = new BufferedReader(
                  new InputStreamReader( 
                         socket.getInputStream() 
                        )
                    );

              // write the output as "out"
                PrintWriter out = new PrintWriter(new BufferedWriter(
                        new OutputStreamWriter(socket.getOutputStream()
                          )
                ));

                // A Loop for waiting to receive the input from the client
                while (true) {
                    System.out.println("Watting for inpute line ...");
                    String line = in.readLine();

                    // print the input string to console to make sure it recive the input
                    System.out.println("inputed Line: " + line);

                    // sent back upper case string to client 
                    out.println(line.toUpperCase());
                    out.flush();
                }
            }finally {
                System.out.println("closing...");
                socket.close();
            }
        }
    }

客户端代码:

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) throws IOException {
        
    // get the Address for localhost 
        InetAddress addr = InetAddress.getByName(null);
        
     //Connect to the server on pprt 9090   
        Socket s = new Socket(addr,9090);
        
     // read and write to server using buffer reader and printwriter
        try{
            BufferedReader in = new BufferedReader(
                    new InputStreamReader(
                            s.getInputStream()
                    )
            );
            PrintWriter out = new PrintWriter(
                    new BufferedWriter(
                            new OutputStreamWriter(s.getOutputStream())
                    )
            );
       //Get the Ipute string from user 
            Scanner input = new Scanner(System.in);
            while (true){
                System.out.println("Enter your text:");
                String line = input.nextLine();
                if(line.equals("quit"))
                    break;
                
        // Sent the input string to the server using 'out'          
                out.println(line);
        // Recive the upper case string from server        
                String response = in.readLine();
                System.out.println("Echo:... " + response);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            System.out.println("closing...");
            s.close();
        }
    }
}

服务器端正在等待从客户端读取一行,如果你在客户端 flush PrintWriter 它应该可以工作:

// Sent the input string to the server using 'out'          
out.println(line);
out.flush();