0

I am trying to make a simple echo client server program. I am able to send the value from client to server and print it . But I am not able to get the value back from server to client This is the code below

Client code

public class Client_1 {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) throws IOException {
    System.out.println("client signing on");
    Socket soc = new Socket("127.0.0.1", 5556);
    t t = new t(soc);
    Thread t1 = new Thread(t);
    t1.start();
    OutputStream os = soc.getOutputStream();

    OutputStreamWriter osw = new OutputStreamWriter(os);
    BufferedWriter bw = new BufferedWriter(osw);
    PrintWriter pw = new PrintWriter(bw, true);
    InputStream is = System.in;
    InputStreamReader isr = new InputStreamReader(is);
    BufferedReader br = new BufferedReader(isr);
    String s = br.readLine();
    while (!s.equals("end")) {
        //  System.out.println(s);
        pw.println(s);
        s = br.readLine();
    }
    pw.println("end");
    System.out.println("client signing off");
}

}

class t implements Runnable {

static Socket soc;
static String read;

public t(Socket soc) {
    this.soc = soc;

}

@Override
public void run() {
    try {
        System.out.println("conn established");
        InputStream is = soc.getInputStream();
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);
        String s = br.readLine();
        while (!s.equals("end")) {
            System.out.println(s);
            s = br.readLine();
        }

    } catch (IOException ex) {

    }
}

}

server code

public class server {

static Socket soc;
static String r;
static BufferedReader br;

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {

    try {
        System.out.println("server signing on");
        ServerSocket ss = new ServerSocket(5556);
        soc = ss.accept();
        InputStream is = soc.getInputStream();
        InputStreamReader isr = new InputStreamReader(is);
         OutputStream os;
        os = soc.getOutputStream();
         OutputStreamWriter osw = new OutputStreamWriter(os);
        BufferedWriter bw = new BufferedWriter(osw);
        PrintWriter pw = new PrintWriter(bw,true);

        br = new BufferedReader(isr);
        r = br.readLine();
        while (!r.equals("end")) {
            System.out.println(r);
        //   PrintWriter out = new PrintWriter(new FileWriter("c:\\outputfile.txt")); 
            pw.print("test");
            pw.print(r);
            r = br.readLine();

        }pw.print("end");
        System.out.println("server signing off");
    } catch (Exception e) {
        System.out.println(e);
    }
}

}

Thank you in advance.

Nikhil Kadam
  • 159
  • 1
  • 8
  • 23
  • 1
    The client does a println, so readLine works on the server, whereas the server does a print, so no new line, and the client waits for it. – JP Moresmau Apr 03 '14 at 13:31

1 Answers1

0

Where exactly in your server code are you trying to write to client? Are you trying to write on the server console and expecting it to appear on the client console? Then you would need to spawn off a thread and share the output stream object with it in order to right back to client. If you just want to echo the string that client typed in the console write back, add a os.write(r.getBytes()); and os.flush(); at the bottom of your while loop.

J.J
  • 633
  • 1
  • 6
  • 14
  • I wanted the echo the string typed by the client. I tried writing pw.flush it did worked. But instead of flush I had written PrintWriter pw = new PrintWriter(bw,true) instead of flush I had written TRUE in printwriter why didnt it worked...? – Nikhil Kadam Apr 03 '14 at 18:20
  • @nikalldway This may help explain why. Basically you need to call one of the println or printf for the autoflush to happen. http://stackoverflow.com/questions/5492405/printwriter-autoflush-puzzling-logic – J.J Apr 03 '14 at 20:04