2

I wanna write the code to let Client send a string to Server, Server print the string and reply a string, then Client print the string Server reply.
My Server

public class Server {

public static void main(String[] args) throws IOException {
    ServerSocket ss = null;
    Socket s = null;
    try {
        ss = new ServerSocket(34000);
        s = ss.accept();
        BufferedReader in = new BufferedReader(new InputStreamReader(
                s.getInputStream()));
        OutputStreamWriter out = new OutputStreamWriter(s.getOutputStream());

        while (true) {
            String string = in.readLine();
            if (string != null) {
                System.out.println("br: " + string);

                if (string.equals("end")) {
                    out.write("to end");
                    out.flush();
                    out.close();
                    System.out.println("end");
                    // break;
                }
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        s.close();
        ss.close();
    }
}
}

My Client:

public class Client {
public static void main(String[] args) {
    Socket socket =null;


    try {
        socket = new Socket("localhost", 34000);
        BufferedReader in =new BufferedReader(new InputStreamReader(socket.getInputStream()));
        OutputStreamWriter out = new OutputStreamWriter(socket.getOutputStream());

        String string = "";
        string = "end";
        out.write(string);
        out.flush();
        while(true){
            String string2 = in.readLine();
            if(string2.equals("to end")){
                System.out.println("yes sir");
                break;
            }
        }


    }  catch (Exception e) {
        e.printStackTrace();
    }finally{
        try {
            System.out.println("closed client");
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
}

are there some somethings wrong? if i remove the code "while(true) ..." in client class, it's OK.

5
  • so can you explain what is happening Commented Oct 21, 2013 at 8:20
  • Do you send a newLine? Commented Oct 21, 2013 at 8:21
  • "public String readLine() Reads a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed." -> docs.oracle.com/javase/7/docs/api/java/io/… Commented Oct 21, 2013 at 8:24
  • When your server is reading newline in.readLine(), you can use the following in client: PrintWriter out = new PrintWriter(socket.getOutputStream(), true); out.println(string); Commented Oct 21, 2013 at 8:28
  • Your code is wrong. If readLine() returns null you must close the socket and exit the loop. Commented Oct 21, 2013 at 8:46

4 Answers 4

4

you should add "\r\n" at the end of the String which write into stream.

example:

client :

    string = "end";
    out.write(string + "\r\n");
    out.flush();

server :

    out.write("to end" + "\r\n");
    out.flush();
    out.close();
    System.out.println("end");
                // break;
Sign up to request clarification or add additional context in comments.

2 Comments

I try, and it work well in ecplise.Can you tell me what is your IDE and what kind of OS
It doesn't work in mt case: stackoverflow.com/questions/63127834
0

I don't see the server response. You do a

System.out.println("br: " + string);

but not a

out.write(string);
out.flush();

Comments

0

Appand "\n" to end of the response from server.

outToClient.writeBytes(sb.toString() + "\n"); 

Comments

0

You are reading lines but you aren't writing lines. Add a newline, or call BufferedReader.newLine().

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.