Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...