【问题标题】:Address Already in Use Error When Running my Chat Program运行我的聊天程序时地址已在使用中错误
【发布时间】:2018-11-12 00:32:23
【问题描述】:

我正在尝试创建一个聊天程序,但遇到了一些问题。我试图通过运行它的两个实例(一个是客户端,一个是服务器)在我的机器上对其进行测试。发生这种情况时,在创建第二个实例后,我收到一个 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());
}

【问题讨论】:

  • 你用什么地址?如果你有一个运行的程序副本已经绑定到相同的地址,你会得到这个错误。在启动新实例之前正确终止您现有的应用程序。
  • ServerSockets 正在侦听专用端口。但是,一次只能有一个 Socket 监听一个端口。有一些保留端口以及一些对于某些应用程序是常见的。例如,托管网站的网络服务器很可能会侦听端口 80。如果您要侦听的端口已在使用中,则会出现您在帖子中提到的异常。

标签: java sockets networking ip-address


【解决方案1】:

您正在打开一个服务器套接字

//Server
private void startRunning() {
try {
   server = new ServerSocket(9999, 100);

这开始在端口 9999 上侦听。

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

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

为什么在客户端需要一个 serversocket? 这会导致您在问题中提到的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-26
    • 2019-12-28
    • 1970-01-01
    • 2016-04-20
    • 1970-01-01
    • 2011-11-04
    • 2012-07-26
    相关资源
    最近更新 更多