gpt4 book ai didi

java - 错误消息在 Netbeans 中没有消失,为什么? "java.net.BindException: Address already in use: bind"

转载 作者:行者123 更新时间:2023-12-01 11:13:20 25 4
gpt4 key购买 nike

这似乎是一个关于如何终止已在该端口上运行的进程的重复问题,但这是一个不同的问题。当我终止该进程并重新启动它时,它仍然给出该错误消息,似乎 Netbeans 有问题,并且该错误消息被卡住并且即使在端口未使用时也不断出现。

我使用过该工具:http://www.yougetsignal.com/tools/open-ports/检查端口:6600

下面是它的内容:

enter image description here

我的代码如下所示:

import java.io.*;
import java.util.*;
import com.sun.net.httpserver.*;
import java.net.InetSocketAddress;
import java.util.concurrent.Executors;
import Utility.*;

public class Http_Server
{
static int Port=6600,Thread_Count=50,Resume_Count=0; // 8080
InetSocketAddress addr;
HttpServer server;
static String App_Id="Resume_App";
static Resume_Loader Resume_loader;
static Get_Time Timer=new Get_Time();
static Vector<String> Current_Keywords_Vector=new Vector();
static boolean Debug_B=true;

public Http_Server(int Port)
{
if (Port>-1) this.Port=Port;
Srart_Server();
}

void Srart_Server()
{
try
{
Resume_loader=new Resume_Loader();

addr=new InetSocketAddress(Port);
server=HttpServer.create(addr,0); // Line : 34
server.createContext("/"+App_Id,new MyHandler(server));
server.setExecutor(Executors.newCachedThreadPool());
server.start();
Out("Server is listening on port : "+Port);
}
catch (Exception e)
{
Resume_App.Save_To_Log(e.toString()+"\n"+Tool_Lib_Simple.Get_Stack_Trace(e));
e.printStackTrace();
Out(e.toString());
}
}

private static void out(String message) { System.out.print(message); }
private static void Out(String message) { System.out.println(message); }

public static void main(String[] args) { Http_Server Demo=new Http_Server(-1); }
}

昨天运行得很好,但今天早上我改变了:

InetSocketAddress addr;   to   static InetSocketAddress addr;
HttpServer server; to static HttpServer server;

当我运行它时,我收到以下错误消息:

Server is listening on port : 6600

java.net.BindException: Address already in use: bind
at sun.nio.ch.Net.bind0(Native Method)
at sun.nio.ch.Net.bind(Net.java:437)
at sun.nio.ch.Net.bind(Net.java:429)
at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:223)
at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:74)
at sun.net.httpserver.ServerImpl.<init>(ServerImpl.java:100)
at sun.net.httpserver.HttpServerImpl.<init>(HttpServerImpl.java:50)
at sun.net.httpserver.DefaultHttpServerProvider.createHttpServer(DefaultHttpServerProvider.java:35)
at com.sun.net.httpserver.HttpServer.create(HttpServer.java:130)
at Http_Server.Srart_Server(Http_Server.java:34)
at Http_Server.<init>(Http_Server.java:24)
at Http_Server.main(Http_Server.java:51)

然后我意识到如果它们是静态的,它们可能会将它们自己保存在内存中并占用端口,所以我将它们改回非静态,就像上面的代码一样。但现在当我运行它时,错误消息不断出现,并且在 Netbeans 中有一个“通知”,内容如下:“package-info.class 位于错误的位置,请将其删除或将其放在正确的子目录中...... “不记得具体的措辞了。但是,当我打开该网址时,网页工作正常,就好像没有错误消息一样,并且当我停止服务器时,该页面不会按预期显示。

所以看起来 Netbeans 8.0.2 出现故障,但我不知道如何使错误消息不出现,或者按照它的建议如何删除“package-info.class”或将其放入正确的子目录中dir,我什至找不到它在哪里,如何修复它?

最佳答案

关于:

there is a "notification" saying something like : "package-info.class is in wrong place, delete it or put it in the right sub directory ..." don't remember the exact wording

请再次执行此操作,并将确切的措辞复制并粘贴或屏幕截图到您的问题中。

我认为在 ExecutiveService 上显式调用 shutdown() 并在 HttpServer 上显式调用 stop(1) 比希望垃圾收集来完成要好得多。并且它允许您控制打印一些确认消息。

例如。

InetSocketAddress addr = new InetSocketAddress(6600);
ExecutorService es = Executors.newCachedThreadPool();
HttpServer server = HttpServer.create(addr, 0);;
server.createContext("/foo", new HttpHandler() {

@Override
public void handle(HttpExchange he) throws IOException {
String response = "My Response";
he.sendResponseHeaders(200, response.length());
OutputStream os = he.getResponseBody();
os.write(response.getBytes());
os.close();
}
});
server.setExecutor(es);

server.start();
System.out.println("Press enter to stop server.");

// block waiting for user to press enter.
System.in.read();
System.out.println("Shutting down");
server.stop(1);
es.shutdownNow();
System.out.println("should be shutdown now.");

在 netbeans 的底部还有一个状态行。

screenshot of status line

如果您看到此内容,则需要单击小 x 来停止您启动但可能忘记的任何进程。

关于java - 错误消息在 Netbeans 中没有消失,为什么? "java.net.BindException: Address already in use: bind",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32122270/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com