帮助第一个网络计划(Help with first networking program)
这是代码。
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.*和java.net.* 。
现在,当我尝试运行这些(使用不同的终端)时,没有任何反应。 我究竟做错了什么?
屏幕: http : //i29.tinypic.com/250qlmt.jpg
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
最满意答案
使用PrintWriter时,您需要调用flush方法。 将代码更改为以下内容,并且工作:)。
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(); } } }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(); } } }更多推荐
发布评论