java - 套接字问题 - readline 无法正常工作

标签 java sockets

我尝试使用套接字编写客户端和服务器连接的代码。问题是我的客户端无法从服务器读取响应(它卡在读取线上)。

这是一些代码。

服务器:

    try {
        // Create the server socket.
        portNumber = Integer.parseInt(myParam.get("socket.portNumber"));
        System.out.println(portNumber);
        mainSocket = new ServerSocket(portNumber);

    } catch (IOException ioe) {
        System.out.println("Error Message : "+ioe.getMessage());
    }

    while(true)
    {     
        try
        {
            // Accept connections
            Socket clientSocket = mainSocket.accept();
            SocketServerThread st = new SocketServerThread (clientSocket);
            st.start();
        }
        catch(IOException ioe)
        {
            System.out.println("Error message :"+ioe.getMessage());
        }
    }

主题:

public void run() {

    BufferedReader in = null;
    PrintWriter out = null;
    String clientResponse = null;

    try {
        in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        out = new PrintWriter(new OutputStreamWriter(clientSocket.getOutputStream()));

        //Read The Message
        String clientRequest = in.readLine();
        System.out.println("Message recieved : " + clientRequest);

        //Process the message

        // Send response
        out.println(clientResponse+"\n");
        out.flush();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        // Clean up
        try {
            in.close();
            out.close();
            clientSocket.close();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }

客户端:

    try {
        // Create the server socket.
        simSocket = new Socket("192.168.52.27", portNumber);
    } catch (IOException ioe) {
        System.out.println("Error Message : " + ioe.getMessage());
    }
    BufferedReader in = null;
    PrintWriter out = null;
    try {
        in = new BufferedReader(new InputStreamReader(simSocket.getInputStream()));
        out = new PrintWriter(new OutputStreamWriter(simSocket.getOutputStream()));

            out.write("My message");
            out.flush();

            do{
            response = in.readLine(); //This is where the code hang
            }while (response.length()<= 0);

            System.out.print(response);

    } catch (IOException ioe) {
        System.out.println("Error message :" + ioe.getMessage());
    } finally {
        try {
            in.close();
            out.close();
            simSocket.close();
        } catch (IOException ioException) {
            ioException.printStackTrace();
        }
    }

你们能告诉我这是什么问题吗?非常感谢您的帮助

最佳答案

好的,我已经弄明白了。挂起的不是客户端,而是服务器。它尝试从客户端读取一行文本,但客户端不发送行分隔符:

    out.write("My message");
    out.flush();

在这里用 println() 替换 write()。

关于java - 套接字问题 - readline 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4457975/

相关文章:

java - 为什么我的水印在java中没有显示在中心?

java - 在 GLSurfaceView.Renderer 之外创建 gl 纹理?

java - Wicket:使(启用 CDI 的)页面可序列化时要记住什么?

flash - 闪光灯无法通过开放式 socket 连接,存在安全问题

java - 将对象添加到 JList

java - 更改 AutoCompleteTextView 的值

基于fork()的c服务器练习

c# - 套接字是否在硬盘上保存任何数据?

c++ - 在 Qt 中通过 TCP 传输大文件

php - fgets()超时