Help with the first network program

Here is 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 are java.net.*imported into both classes.

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

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

+3
source share
4 answers

When using PrintWriter, you need to call the flush method. Changed your code as follows 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();
    }
  }
}
+5
source

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

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

, sock.close() , , , .

+5

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

Close the author.

+1
source

Source: https://habr.com/ru/post/1757541/


All Articles