关于ServerSocket和Socket连接java.net.BindException异常

发布于 2021-11-21 23:10:41 字数 4305 浏览 750 评论 6

    最近翻箱倒柜发现一本早买但没有看的书《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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

虐人心 2021-11-22 14:50:37

那必须敲回车啊,基本的智商还保留着呢

伪装你 2021-11-22 14:50:35

你输入后 敲回车了吗,readline

旧城烟雨 2021-11-22 14:23:07

美女 问个问题,现在的Nginx 官网下载的rpm安装包,4kb 正常吗

像你 2021-11-22 14:13:43

回复
这个我不了解

归属感 2021-11-22 13:53:57

搜了,但是没有一个解决方法适用于我这,换了N多端口了,还是这样的。

瑾兮 2021-11-22 08:44:35

你不会google一下吗?很明显端口被占用了阿

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文