关于ServerSocket和Socket连接java.net.BindException异常
最近翻箱倒柜发现一本早买但没有看的书《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(); } }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
那必须敲回车啊,基本的智商还保留着呢
你输入后 敲回车了吗,readline
美女 问个问题,现在的Nginx 官网下载的rpm安装包,4kb 正常吗
回复
这个我不了解
搜了,但是没有一个解决方法适用于我这,换了N多端口了,还是这样的。
你不会google一下吗?很明显端口被占用了阿