开发者社区> 问答> 正文

关于ServerSocket和Socket连接java.net.BindExce?报错

    最近翻箱倒柜发现一本早买但没有看的书《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();
	}

}

 

展开
收起
爱吃鱼的程序员 2020-06-22 21:29:16 808 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    你不会google一下吗?很明显端口被占用了阿搜了,但是没有一个解决方法适用于我这,换了N多端口了,还是这样的。任务管理器,结束所有java进程,重新开始回复<aclass='referer'target='_blank'>@os4china:这个我不了解美女问个问题,现在的Nginx官网下载的rpm安装包,4kb正常吗你输入后敲回车了吗,readline那必须敲回车啊,基本的智商还保留着呢

    2020-06-22 21:29:34
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
JAVA开发手册1.5.0 立即下载