线程“Thread-0”中的异常java.net.BindException:地址已在使用中:JVM_Bind at

时间:2015-11-01 08:01:59

标签: java multithreading serialization

package port_channel;
import java.util.*;
import java.util.concurrent.*; 
import java.io.*;
import java.net.*; 

public class ChannelPort implements Runnable {
    int portNum; 
    int nSize; 
    PrintWriter[] outs ; 
    Listner[] listners; 
    ConcurrentLinkedQueue<MessageType> que; 

    public ChannelPort(int portNum, int networkSize) {
        this.portNum = portNum; 
        this.nSize = networkSize; 
        outs = new PrintWriter[nSize]; 
        listners = new Listner[nSize]; 
        que = new ConcurrentLinkedQueue<MessageType>(); 

    } 

    public void initialize() {
        ServerSocket serverSocket = null; 
        try {
            serverSocket = new ServerSocket(portNum); 
        } catch (IOException ioe) {  } 
        for (int j = 0; j < nSize; j++) { 
            try {
                Socket clientSocket = serverSocket.accept();       
                // not part of communication 
                outs[j] = new PrintWriter(new OutputStreamWriter(clientSocket.getOutputStream())); 
                ObjectInputStream in = new ObjectInputStream(clientSocket.getInputStream()); 
                listners[j] = new Listner(j, in, this);  
            } catch (IOException ioe) { 
                System.err.println("Failed in connection for j=" + j);
                ioe.printStackTrace(); 
                System.exit(-1);
            } 
        } 
        System.out.println("Connections are all established."); 
    } 
        //thread
    public void run() { 
        initialize(); 
        for (int j = 0; j < nSize; j++) {
            listners[j].start(); 
        }
    }

    synchronized void gotMessage(MessageType message) {
        que.offer(message); 
        notifyAll(); 
    } 

    public synchronized MessageType receive() { 
        while (que.isEmpty()) { 
            try {
                wait(); 
            } catch (InterruptedException ire) {
                ire.printStackTrace();
            }
        } 
        MessageType msg = que.poll(); 
        System.out.println("receive: " + msg);
        return msg; 
    } 

    public synchronized void broadcast(String msgStr) { 
        for (int j = 0; j < outs.length; j++) { 
            outs[j].println(msgStr); 
            outs[j].flush();
        }
    }

    public int getPortNum() {
        return portNum;
    }

    public void setPortNum(int portNum) {
        this.portNum = portNum;
    }

    public int getnSize() {
        return nSize;
    }

    public ConcurrentLinkedQueue<MessageType> getQue() {
        return que;
    }

    public static void main(String[] args) throws IOException, InterruptedException   { 
        if (args.length != 2) 
            System.out.println("usage: java ChannelPort port-number number-of-nodes");
        int portNum = Integer.parseInt(args[0]); 
        int numNode = Integer.parseInt(args[1]); 
        ChannelPort cp = new ChannelPort(portNum, numNode); 
        new Thread(cp).start(); 
        Thread.sleep(60000); 
        System.out.println("Shutdown"); 
        Iterator<MessageType> ite = cp.getQue().iterator(); 
        while (ite.hasNext()) {
            System.out.println(ite.next()); 
        }
    }
}
//thread
class Listner extends Thread { 
    int pId; 
    ObjectInputStream in; 
    ChannelPort cPort; 
    boolean done = false; 
    final int ERR_THRESHOLD = 100; 

    public Listner(int id, ObjectInputStream in, ChannelPort cPort) { 
        this.pId = id; 
        this.in = in;  
        this.cPort = cPort; 
    } 
    public void run() { 
        MessageType msg; 
        int errCnt = 0; 
        while(in != null) { 
            try { 
                msg = (MessageType)in.readObject(); 
                System.out.println("process " + pId + ": " + msg); 
                cPort.gotMessage(msg); 
            } catch (ClassNotFoundException cnfe) { 
                cnfe.printStackTrace(); 
            } catch (SocketException se) { 
                System.err.println(se); 
                errCnt++; 
                if (errCnt > ERR_THRESHOLD)  System.exit(0); 
            } catch (IOException ioe) { 
                ioe.printStackTrace(); 
            }
        }
    }
}

1 个答案:

答案 0 :(得分:2)

您是否检查了serverSocket正确创建的内容?

您在创建ServerSocket时已禁止例外。

    catch (IOException ioe) {
            //print stack trace and see why exception occurs.
    }
在第二次尝试阻止时,

serverSocket可能为空,因此serverSocket.accept()会抛出NPE,修复此错误