Tot*_*mus 5 java timeout serversocket
我正在创建一个基于 Java 的服务器。
我正在使用服务器套接字来接受传入消息。
然而,在我的程序中的某个时刻,我希望服务器套接字侦听另一个端口。
我关闭服务器套接字。并用我的新端口开始一个新的端口。一切安好。
但是,当我再次将服务器套接字更改为以前的端口时,会出现错误。
我读过一些内容,服务器套接字在关闭后仍处于超时状态一段时间。
所以这是我的问题:我可以绕过服务器套接字的这种超时状态,并在关闭端口并想要再次侦听同一端口后使端口再次可用吗?
编辑:我的函数用于创建和监听服务器套接字,我的函数用于使服务器套接字无效并在之后立即创建一个新的套接字
public void makeServerSocketWithPort(int portnr) throws IOException, Exception
{
server = new ServerSocket(portnr);
server.setReuseAddress(true);
while(!portchanged)
{
Socket sock = server.accept();
System.out.println(server.getLocalPort());
System.out.println(sock.getLocalPort());
handler = new Requesthandler(sock); //should be in a thread
System.out.println(server.getLocalPort());
System.out.println(sock.getLocalPort());
}
}
public void invalidateRequestHandler(int newPort)
{
if(server != null)
{
portchanged = true;
try {
server.close();
} catch (IOException ex) {
Logger.getLogger(Controlserver.class.getName()).log(Level.SEVERE, null, ex);
}
}
portchanged = false;
makeServerSocketWithPort(newPort);
}
Run Code Online (Sandbox Code Playgroud)
错误堆栈跟踪:
Exception in thread "main" java.net.SocketException: Socket closed
at java.net.PlainSocketImpl.socketAccept(Native Method)
at java.net.PlainSocketImpl.accept(PlainSocketImpl.java:408)
at java.net.ServerSocket.implAccept(ServerSocket.java:462)
at java.net.ServerSocket.accept(ServerSocket.java:430)
at stuff.Controlserver.makeServerSocketWithPort(Controlserver.java:63)
at stuff.Main.main(Main.java:44)
Run Code Online (Sandbox Code Playgroud)
编辑:第二次尝试修复它但无济于事:
public void makeServerSocketWithPort(int portnr, boolean invalidated) throws IOException, Exception
{
if(!invalidated)
{
server = new ServerSocket();
server.setReuseAddress(true);
server.bind(new InetSocketAddress(portnr));
portchanged = false;
}
else
{
//TODO: invalidate the old requestHandler
if(server != null)
{
try
{
server.close();
server = null;
}
catch (IOException ex)
{
Logger.getLogger(Controlserver.class.getName()).log(Level.SEVERE, null, ex);
}
}
if(server.isClosed())
{
System.out.println("closed biatch!");
}
else
{
System.out.println("surprise moddafakkaaaaa!!!");
}
//---------------------------------------------
//then make new requestHandler with new port
portchanged = true;
}
while(!portchanged)
{
if(server != null && !server.isClosed() && !invalidated)
{
Socket sock = server.accept();
System.out.println(server.getLocalPort());
System.out.println(sock.getLocalPort());
System.out.println("test");
handler = new Requesthandler(sock); //should be in a thread
handler.start();
System.out.println("ja harm");
System.out.println(server.getLocalPort());
System.out.println(sock.getLocalPort());
}
else
{
portchanged = true;
}
}
if(portchanged)
{
portchanged = false;
makeServerSocketWithPort(portnr, false);
}
}
Run Code Online (Sandbox Code Playgroud)
同样,这可以正常工作。我可以浏览我的 html 页面。当我通过其中一个网页更改端口号时,它会在我的存储 xml 文件中正确存储和更改。但是,当我更改套接字并立即通过该套接字导航到页面时,它说它已关闭并且在我重新启动应用程序之前无法工作。
我仍在寻找一种方法来规避这种重新启动。
好吧,我解开了这个谜。问题是我只需要稍微重构一下我的类以更好地支持线程。我没有关闭套接字然后创建一个新线程,而是启动了一个新线程,然后关闭了套接字。经过一番摆弄后,它似乎工作得很好。
这是操作系统的正常服务器套接字行为。操作系统将端口保持在 WAIT_TIMEOUT 状态打开。要解决这个问题,请尝试使用 ServerSocket.setReuseAddress(boolean on). 这将启用/禁用SO_REUSEADDR套接字选项。 在此处查看文档。
引用方法的javadocsetReuseAddress
当 TCP 连接关闭时,该连接可能会在连接关闭后的一段时间内保持超时状态(通常称为 TIME_WAIT 状态或 2MSL 等待状态)。对于使用众所周知的套接字地址或端口的应用程序,如果存在涉及套接字地址或端口的超时状态连接,则可能无法将套接字绑定到所需的 SocketAddress。
在使用 bind(SocketAddress) 绑定套接字之前启用 SO_REUSEADDR 允许绑定套接字,即使先前的连接处于超时状态也是如此。
| 归档时间: |
|
| 查看次数: |
7362 次 |
| 最近记录: |