Hello Greenfoot community,
I want to write a sipmel server client conection. The server works very good but i got some problems with the InputStream which should be "hello_world_from_server" of the client it doesn't get any informations from the server but if I use my browser as client I get the Stream. So what is wrong with my code?
That is my server code
And that is the client code
Thank you.
import greenfoot.*; import java.net.*; import java.io.*; /** * Write a description of class world here. * * @author (your name) * @version (a version number or a date) */ public class server extends World { /** * Constructor for objects of class world. * */ public server() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(10, 10, 50); } public void act() { try { ServerSocket server = new ServerSocket(1234); while (true) new ServConn(server.accept()); } catch(IOException e) { System.out.println("I/O Error "+e); } } } class ServConn { Socket sock; ServConn(Socket s) { sock=s; run(); } public void run() { try { InputStream in = sock.getInputStream(); System.out.println(in.available()); BufferedReader buff = new BufferedReader(new InputStreamReader(in)); while (buff.ready()) { System.out.println(buff.readLine()); } OutputStream out = sock.getOutputStream(); PrintStream ps = new PrintStream(out, true); ps.println("hello_world_from_server"); sock.close(); } catch(IOException e) { System.out.println("I/O Error "+e); } } }
import greenfoot.*; import java.net.*; import java.io.*; /** * Write a description of class world here. * * @author (your name) * @version (a version number or a date) */ public class client extends World { /** * Constructor for objects of class world. * */ public client() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(10, 10, 50); } public void act() { try{ new ServConn(new Socket( "127.0.0.1", 1234 )); } catch (IOException e){System.out.println("I/O Error "+e);}; } } class ServConn { Socket sock; ServConn(Socket s) { sock=s; run(); } public void run() { try { InputStream in = sock.getInputStream(); System.out.println(in.available()); BufferedReader buff = new BufferedReader(new InputStreamReader(in)); while (buff.ready()) { System.out.println(buff.readLine()); } OutputStream out = sock.getOutputStream(); PrintStream ps = new PrintStream(out, true); ps.println("hello_world_from_client"); sock.close(); } catch(IOException e) { System.out.println("I/O Error "+e); } } }