3
回答
关于ServerSocket和Socket连接java.net.BindException异常
利用AWS快速构建适用于生产的无服务器应用程序,免费试用12个月>>>   

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

}

 

举报

以下是问题补充:

  • @智商已暴露 :现在我把文件单独便宜出class来,然后分别执行。客户端不报错了,但是输入一行字母,服务端没有任何反应,貌似消息没有传送到。麻烦看一下我输入输出流写的有问题么? (5年前)
  • @智商已暴露 :系统环境是win7( 64bit) jdk1.6(32bit),这个会不会有影响呢? (5年前)
顶部