运行聊天程序时,地址已经使用错误

时间:2018-06-01 20:32:59

标签: java sockets networking ip-address

我正在尝试创建一个聊天程序,我遇到了一些问题。我试图通过运行它的两个实例(一个客户端,一个服务器)在我的机器上测试它。发生这种情况时,在我创建第二个实例后,我收到一个BindException错误,说明该地址已被使用。我认为可能是问题的根源是,由于聊天是较大应用程序的一小部分,我正在尝试制作一个具有服务器和客户端功能的程序,并且用户在加载应用程序时选择一个。下面,我附上了相关的代码。任何和所有的帮助将不胜感激! (抱歉格式不佳)

public ChatPanel(){
  //Sets up main panel and variables
  this.setBackground(new Color(0, 155, 228));
  this.setFocusable(false);  
  this.setLayout(new BorderLayout());
  textBox = new JTextField();
  chatBox = new MessagePanel();
  this.add(chatBox, BorderLayout.CENTER);
  this.add(textBox, BorderLayout.SOUTH);
  begin();}

  public void begin() {
  String[] options = {"Server", "Client"};
  int entry = JOptionPane.showOptionDialog(null, "Server or Client?", "Setup",
        JOptionPane.YES_NO_OPTION, JOptionPane.NO_OPTION, null, options, options[0]);
  thisIsAServer = !(entry == 1);
  showMessage("" + entry);
  if(thisIsAServer) startRunning();
  else {
     serverIP = (String)JOptionPane.showInputDialog(null, "Enter the IP Address of the Host ", "Connect to Host",
           JOptionPane.PLAIN_MESSAGE, null, null, "");
     startRunningClient();
  }
  }

  //Server
  private void startRunning() {
  try {
     server = new ServerSocket(9999, 100);
     while(true) {
        try {
           waitForConnection();
           setUpStreams();
           whileChatting();
        } catch(EOFException eof) {
           showMessage("Chat Ended");
        }finally {
           close();
        }
     }
  } catch (IOException e) {e.printStackTrace();}
  }

  //Server
  private void waitForConnection() throws IOException{
  System.out.println("1");
  showMessage("Waiting for connection");
  connection = server.accept();
  System.out.println("1.5");
  showMessage("Connected to: " + connection.getInetAddress().getHostName());
  }

  //Server
  private void setUpStreams() throws IOException{
  System.out.println("2");
  output = new ObjectOutputStream(connection.getOutputStream());
  output.flush();
  input = new ObjectInputStream(connection.getInputStream());
  showMessage("Streams successfully setup");
  }

  //Client
  private void setUpStreamsC() throws IOException{
  System.out.println("2");
  output = new ObjectOutputStream(connection.getOutputStream());
  output.flush();
  input = new ObjectInputStream(connection.getInputStream());
  showMessage("Streams successfully setup");
  }

  //Server
  private void whileChatting() throws IOException{
  System.out.println("3");
  String message = "Hello World";
  sendMessage(message);
  do {
     try {
        message = (String) input.readObject();
        showMessage(message);
     }catch(ClassNotFoundException cnfe) {
        showMessage("Error");
     }
  }while(!message.equals("CLIENT - END") || !message.equals("SERVER - END"));
  }

  //Client
  private void whileChattingC() throws IOException{
  System.out.println("3");
  String message = "Hello World";
  sendMessage(message);
  do {
     try {
        message = (String) input.readObject();
        showMessage(message);
     }catch(ClassNotFoundException cnfe) {
        showMessage("Error");
     }
  }while(!message.equals("CLIENT - END") || !message.equals("SERVER - END"));
  }

  //Server
  private void close() {
  try {
     output.close();
     input.close();
     connection.close();
  } catch (IOException e) {e.printStackTrace();}
  }

  //Server
  public void sendMessage(String message) {
  String name = "CLIENT";
  if(thisIsAServer) name = "CLIENT";

  try {
     output.writeObject(name + " - " + message);
     output.flush();
     showMessage("\n " + name + " - " + message);
  }catch(IOException e) {chatBox.getChatBox().append("\nERROR");}

  }

  //Server
  private void showMessage(String message) {
  chatBox.getChatBox().append(message);
  }

  private void startRunningClient() {
  try {
     server = new ServerSocket(9999, 100);
     while(true) {
        try {
           connectToServer();
           setUpStreamsC();
           whileChattingC();
        } catch(EOFException eof) {
           showMessage("Chat Ended");
        }finally {
           close();
        }
     }
  } catch (IOException e) {e.printStackTrace();}
}

private void connectToServer() throws IOException{
  showMessage("Attempting connection... \n");
  connection = new Socket(InetAddress.getByName(serverIP), 9999);
  showMessage("Connected to: " + connection.getInetAddress().getHostName());
}

1 个答案:

答案 0 :(得分:1)

您正在

打开服务器套接字
//Server
private void startRunning() {
try {
   server = new ServerSocket(9999, 100);

这开始侦听端口9999。

然后再次在客户端代码中打开服务器端口。

 private void startRunningClient() {
  try {
     server = new ServerSocket(9999, 100);

为什么在客户端需要服务器套接字? 这会导致您在问题中提到的问题。