帮助第一个网络计划(Help with first networking program)

编程入门 行业动态 更新时间:2024-10-24 04:51:31
帮助第一个网络计划(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(); } } }

更多推荐

本文发布于:2023-08-05 06:57:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1430236.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:第一个   计划   网络   program   networking

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!