线程"main"中的异常java.net.BindException:地址已在使用中-仅在Netbeans中出错 [英] Exception in thread "main" java.net.BindException: Address already in use - Error in Netbeans only

查看:118
本文介绍了线程"main"中的异常java.net.BindException:地址已在使用中-仅在Netbeans中出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的机器上,以下代码在Eclipse中编译,但在Netbeans中引发异常.错误消息显示线程"main"中的异常java.net.BindException:地址已在使用中".

Netbeans中正确的配置是什么,以使此代码可以编译?问题似乎与我有两个主要职能有关.如果我开始运行其中一个应用程序,则第二个应用程序将无法启动,并引发上面发布的异常.

Server.java

import java.io.*;
import java.net.*;

public class Server {

    public static void main(String[] args) throws Exception {

        Server myServ = new Server();
        myServ.run();

    }

    public void run() throws Exception {

        ServerSocket mySS = new ServerSocket(9999);
        Socket SS_accept = mySS.accept();

        InputStreamReader mySR = new InputStreamReader(SS_accept.getInputStream());
        BufferedReader myBR = new BufferedReader(mySR);

        String temp = myBR.readLine();
        System.out.println(temp);

    }

}

Client.java

import java.io.*;
import java.net.*;

public class Client {

    public static void main(String[] args) throws Exception {

        Client myCli = new Client();
        myCli.run();

    }

    public void run() throws Exception {

        Socket mySkt = new Socket("localhost", 9999);
        PrintStream myPS = new PrintStream(mySkt.getOutputStream());

        myPS.println("Hello server");

    }

}

解决方案

实现我想要的方法是右键单击要运行的特定类(例如Server.java),然后选择运行此文件".这使我只能运行服务器应用程序.然后,对另一个文件Client.java执行相同的过程.

但是,在这种特殊情况下,Netbeans有点令人困惑/不安. Netbeans的工作是运行Server进程,但是将该进程标记为项目的名称(例如MyTestNetworkingProject),并在其上添加运行编号,从而为我们提供MyTestNetworkingProject run #1(实际上,它忽略了#1第一个过程).然后,如果我转到Client.java文件并选择运行此文件",它将生成第二个进程MyTestNetworkingProject run #2.然后,它会在屏幕底部向下生成第二个结果窗口,因为当创建新流程时会在新选项卡中生成这些结果.

由于我的特定代码的性质,我想在结果窗口中看到以确认我的应用程序正在运行,这是我想观察Server.java结果窗口(在本例中为MyTestNetworkingProject run #1).鉴于我上面概述的运行不同文件的确切步骤顺序,运行#2是最后一个运行过程,因此位于顶部的选项卡覆盖了运行#1选项卡.我可以单击运行#1并在控制台("Hello服务器")中查看我希望看到的结果,但是我只需要知道/记住MyTestNetworkingProject run #1代表服务器应用程序而不是客户端应用程序. >

IMO,不爽.

On my machine, the following code compiles within Eclipse but throws an exception within Netbeans. The error message says "Exception in thread "main" java.net.BindException: Address already in use".

What is the proper configuration within Netbeans to make this code compile? It seems like the problem has to do with the fact that I have two main functions. If I start running either one of the apps, the second will fail to start, throwing the exception posted above.

Server.java

import java.io.*;
import java.net.*;

public class Server {

    public static void main(String[] args) throws Exception {

        Server myServ = new Server();
        myServ.run();

    }

    public void run() throws Exception {

        ServerSocket mySS = new ServerSocket(9999);
        Socket SS_accept = mySS.accept();

        InputStreamReader mySR = new InputStreamReader(SS_accept.getInputStream());
        BufferedReader myBR = new BufferedReader(mySR);

        String temp = myBR.readLine();
        System.out.println(temp);

    }

}

Client.java

import java.io.*;
import java.net.*;

public class Client {

    public static void main(String[] args) throws Exception {

        Client myCli = new Client();
        myCli.run();

    }

    public void run() throws Exception {

        Socket mySkt = new Socket("localhost", 9999);
        PrintStream myPS = new PrintStream(mySkt.getOutputStream());

        myPS.println("Hello server");

    }

}

解决方案

The way to achieve what I want is to right-click on the particular class (ex. Server.java) that I want to run and select "Run this file". This enables me to run only the Server app. Then, do the same process for the other file, Client.java.

However, Netbeans is somewhat confusing/deceiving in this particular circumstance. What Netbeans does is it runs the Server process, but labels that process as the name of the project (ex. MyTestNetworkingProject) and puts a run number on it, thus giving us MyTestNetworkingProject run #1 (it actually leaves out the #1 on the first process). Then, if I go to the Client.java file and select "Run this file", it generates a second process, MyTestNetworkingProject run #2. It then generates a second results window down at the bottom of the screen, as it generates these in new tabs as new processes get created.

Because of the nature of my specific code, what I wanted to see in my results window to confirm that my application was working was I wanted to observe the Server.java results window (which in this case is MyTestNetworkingProject run #1). Given my exact sequence of steps outlined above of running the different files, run #2 is the last run process and thus the tab on top, covering the run #1 tab. I can click on run #1 and see the results I was hoping to see in the console ("Hello server"), but I just have to know/remember that MyTestNetworkingProject run #1 represents the Server app and not the Client app.

Uncool, IMO.

这篇关于线程"main"中的异常java.net.BindException:地址已在使用中-仅在Netbeans中出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆