Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
582 views
in Technique[技术] by (71.8m points)

java - Socket problem - readline won't work properly

I try to code a client and server connection using socket. The problem is my client can't read the response from the server (it hangs on the readline).

Here is some of the code.

Server:

    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());
        }
    }

The Thread:

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+"
");
        out.flush();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        // Clean up
        try {
            in.close();
            out.close();
            clientSocket.close();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }

The client:

    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();
        }
    }

Could you guys tell me what's the problem? Thank you very much for any help

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Okay, I've figured this one out. It's not the client that hangs, it's the server. It tries to read a line of text from the client, but the client doesn't send the line separator:

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

Replace write() with println() here.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...