为什么简单的聊天Server-Client总是给我一个BindException?

时间:2014-06-16 23:03:08

标签: java sockets client-server chat

为什么下面的代码会出现以下异常:

java.net.BindException: Address already in use: JVM_Bind
    at java.net.DualStackPlainSocketImpl.bind0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketBind(DualStackPlainSocketImpl.java:106)
    at java.net.AbstractPlainSocketImpl.bind(AbstractPlainSocketImpl.java:376)
    at java.net.PlainSocketImpl.bind(PlainSocketImpl.java:190)
    at java.net.ServerSocket.bind(ServerSocket.java:376)
    at java.net.ServerSocket.<init>(ServerSocket.java:237)
    at java.net.ServerSocket.<init>(ServerSocket.java:128)
    at chap15.VerySimpleChatServer.go(VerySimpleChatServer.java:42)
    at chap15.VerySimpleChatServer.main(VerySimpleChatServer.java:36)

我尝试用netbeans编译它。我创建了一个项目,并将两个类放在其中。 这是“Head First Java”一书的一个例子,第15章

服务器:

package chap15;
import java.io.*;
import java.net.*;
import java.util.*;


public class VerySimpleChatServer
{
    ArrayList clientOutputStreams;

    public class ClientHandler implements Runnable {
        BufferedReader reader;
        Socket sock;

        public ClientHandler(Socket clientSOcket) {
            try {
                sock = clientSOcket;
                InputStreamReader isReader = new InputStreamReader(sock.getInputStream());
                reader = new BufferedReader(isReader);

            } catch (Exception ex) { ex.printStackTrace(); }
        }

        public void run() {
            String message;
            try {
                while ((message = reader.readLine()) != null) {
                    System.out.println("read " + message);
                    tellEveryone(message);
                }
            } catch (Exception ex) { ex.printStackTrace(); }
        }
    }

    public static void main(String[] args) {
        new VerySimpleChatServer().go();
    }

    public void go() {
        clientOutputStreams = new ArrayList();
        try {
            ServerSocket serverSock = new ServerSocket(5000);
            while(true) {
                Socket clientSocket = serverSock.accept();
                PrintWriter writer = new PrintWriter(clientSocket.getOutputStream());
                clientOutputStreams.add(writer);

                Thread t = new Thread(new ClientHandler(clientSocket));
                t.start();
                System.out.println("got a connection");
            }
        } catch (Exception ex) { ex.printStackTrace(); }
    }

    public void tellEveryone(String message) {
        Iterator it = clientOutputStreams.iterator();
        while (it.hasNext()) {
            try {
                PrintWriter writer = (PrintWriter) it.next();
                writer.println(message);
                writer.flush();
            } catch (Exception ex) { ex.printStackTrace(); }
        }
    }
}

2 个答案:

答案 0 :(得分:0)

代码“在此处发布”有效。

您必须将旧线程卡住或某个进程卡在某处。如果您正在调试它并且没有给它正确关闭的机会,就会发生这种情况。

答案 1 :(得分:0)

因为其他东西正在监听端口,可能是您自己程序的先前实例,但尚未终止。或者你终止了它,但仍然有TIME_WAIT状态的端口,在最后一个入站连接关闭后持续两分钟。