最近翻箱倒柜发现一本早买但没有看的书《JAVA网络编程精解》。
根据书上第一章的例子写了个服务端与客户端程序。但是运行客户端时报错,无解。
在这里谢谢大虾们给指点一二。
下面代码是服务端启动命令,我将客户端与服务端分别打包成jar文件了:
E:\networkRun>java -jar server.jar
下面为客户端:
E:\networkRun>java -jar client.jar Exception in thread "main" java.net.BindException: Address already in use: JVM_B ind at java.net.PlainSocketImpl.socketBind(Native Method) at java.net.PlainSocketImpl.bind(PlainSocketImpl.java:365) at java.net.ServerSocket.bind(ServerSocket.java:328) at java.net.ServerSocket.<init>(ServerSocket.java:194) at java.net.ServerSocket.<init>(ServerSocket.java:106) at org.network.server.EchoServer.<init>(EchoServer.java:19) at org.network.server.EchoServer.main(EchoServer.java:69)
下面分别列出源码:
server:
package org.network.server; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; public class EchoServer { private ServerSocket serverSocket; private int port = 8091; public EchoServer() throws IOException { serverSocket = new ServerSocket(port); } public PrintWriter getWriter(Socket socket) throws IOException { OutputStream socketOut = socket.getOutputStream(); return new PrintWriter(socketOut); } public BufferedReader getReader(Socket socket) throws IOException { InputStream socketIn = socket.getInputStream(); return new BufferedReader(new InputStreamReader(socketIn)); } public String echo(String msg) { return "echo: " + msg; } public void service() { while (true) { Socket socket = null; try { socket = serverSocket.accept(); System.out.println("New connection accepted " + socket.getInetAddress() + ":" + socket.getPort()); BufferedReader bufReader = getReader(socket); PrintWriter pntWriter = getWriter(socket); String msg = null; while ((msg = bufReader.readLine()) != null) { System.out.println(msg); pntWriter.println(echo(msg)); if (msg.equals("bye")) { break; } } } catch (IOException e) { e.printStackTrace(); } finally { if (socket != null) { try { socket.close(); } catch (Exception e2) { e2.printStackTrace(); } } } } } public static void main(String args[]) throws Exception { new EchoServer().service(); } }
Client:
package org.network.client; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.net.Socket; public class EchoClient { private String host = "127.0.0.1"; private int port = 8091; private Socket socket; public EchoClient() throws IOException { socket = new Socket(host, port); } public PrintWriter getWriter(Socket socket) throws IOException { OutputStream socketOut = socket.getOutputStream(); return new PrintWriter(socketOut); } public BufferedReader getReader(Socket socket) throws IOException { InputStream socketIn = socket.getInputStream(); return new BufferedReader(new InputStreamReader(socketIn)); } public void talk() throws IOException { try { BufferedReader br = getReader(socket); PrintWriter pw = getWriter(socket); BufferedReader localReader = new BufferedReader( new InputStreamReader(System.in)); String msg = null; while ((msg = localReader.readLine()) != null) { pw.println(msg); System.out.println(br.readLine()); if (msg.equals("bey")) { break; } } } catch (IOException e) { // TODO: handle exception e.printStackTrace(); } finally { if (socket != null) socket.close(); } } public static void main(String[] args) throws Exception { new EchoClient().talk(); } }
你不会google一下吗?很明显端口被占用了阿搜了,但是没有一个解决方法适用于我这,换了N多端口了,还是这样的。任务管理器,结束所有java进程,重新开始回复<aclass='referer'target='_blank'>@os4china:这个我不了解美女问个问题,现在的Nginx官网下载的rpm安装包,4kb正常吗你输入后敲回车了吗,readline那必须敲回车啊,基本的智商还保留着呢
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。