views:

90

answers:

4

Here's the code.

public class testClient {
public static void main(String[] args) {
    testClient abc = new testClient();
    abc.go();
}
public void go() {
    try {
        Socket s = new Socket("127.0.0.1",  5000);
        InputStreamReader  sr = new InputStreamReader(s.getInputStream());
        BufferedReader reader = new BufferedReader(sr);
        String x = reader.readLine();
        System.out.println(x);
        reader.close();
    } catch(IOException ex) {
        ex.printStackTrace();
    }
  }
}

public class testServer {
public static void main(String[] args) {
    testServer server = new testServer();
    server.go();
}
public void go() {
    try {
        ServerSocket s = new ServerSocket(5000);
        while(true) {
            Socket sock = s.accept();

            PrintWriter writer = new PrintWriter(sock.getOutputStream());
            String toReturn = "No cake for you.";
            writer.println(toReturn);
        }
    } catch(IOException ex) {
        ex.printStackTrace();
    }
  }
}

java.io.* and java.net.* are imported in both classes.

Now, when I try to run these(using different terminals), nothing happens. What am I doing wrong?

Screen: http://i29.tinypic.com/250qlmt.jpg

+5  A: 

Your output is apparently buffered. After writing your output, try flushing:

        writer.println(toReturn);
        writer.flush();

Also, you may consider calling sock.close() in your server if you're done with the socket, otherwise the client will be left wondering what to do next.

Greg Hewgill
Writer.close() would be more to the point, as it would also accomplish the flush().
EJP
I can never remember whether calling something like PrintWriter.close() closes the underlying OutputStream or not.
Greg Hewgill
@Greg Hewgill It does, but you have to have successfully created the `PrintWriter`, so you might as well just close the underlying stream that you should have to anyway (after giving the decorator a flush in the happy case). For sockets, closing one of the streams actually closes the socket by default.
Tom Hawtin - tackline
+5  A: 

When using PrintWriter you need to call flush method. Changed your code to following and it works :).

public class testServer {
public static void main(String[] args) {
    testServer server = new testServer();
    server.go();
}
public void go() {
    try {
        ServerSocket s = new ServerSocket(5000);
        while(true) {
            Socket sock = s.accept();

            PrintWriter writer = new PrintWriter(sock.getOutputStream());
            String toReturn = "No cake for you.";
            writer.println(toReturn);
            writer.flush();
        }
    } catch(IOException ex) {
        ex.printStackTrace();
    }
  }
}
YoK
A: 

Check out this simple working example.

 import java.net.*;
    import java.io.*;
    public class Server {
        Server() throws IOException {
            ServerSocket server = new ServerSocket( 4711 );
            while ( true ) {
              Socket client = server.accept();
              InputStream in = client.getInputStream();
              OutputStream out = client.getOutputStream();
              int multiplikator1 = in.read();
              int multiplikator2 = in.read();
              int resultat = multiplikator1 * multiplikator2;
              out.write(resultat);
       }
       public static void main (String[] args) {
       try {
        Server server = new Server();
       } catch (IOException e) {
          e.printStackTrace();
       }
    }
}

    import java.net.*;
    import java.io.*;
    public class Client {
        Client() throws IOException {
            Socket server = new Socket ( "localhost", 4711 );
            InputStream in = server.getInputStream();
            OutputStream out = server.getOutputStream();
            out.write( 4 );
            out.write( 9 );
            int result = in.read();
            System.out.println( result );
            server.close();
        }
        public static void main (String[] args) {
           try {
           Client client = new Client();
         } catch (IOException e) {
           e.printStackTrace();
        }
      }
    }
InsertNickHere
A: 

Close the writer.

EJP