Java 使用后是否关闭所有插座?在服务器端
使用后是否必须关闭所有插座?我应该把它们放在这个代码中的什么地方?我的程序在运行时工作正常。然而,当我重新运行它时,它说“线程中的异常”main“java.net.BindException:Address ready in use:JVM_Bind”。因此,我认为我没有关闭所有的插座后使用它Java 使用后是否关闭所有插座?在服务器端,java,sockets,Java,Sockets,使用后是否必须关闭所有插座?我应该把它们放在这个代码中的什么地方?我的程序在运行时工作正常。然而,当我重新运行它时,它说“线程中的异常”main“java.net.BindException:Address ready in use:JVM_Bind”。因此,我认为我没有关闭所有的插座后使用它 import java.io.*; import java.net.*; import java.util.*; public class Server2 { public static void mai
import java.io.*;
import java.net.*;
import java.util.*;
public class Server2 {
public static void main(String args[]) throws Exception {
int PORT = 5555; // Open port 5555
//open socket to listen
ServerSocket server = new ServerSocket(PORT);
Socket client = null;
while (true) {
System.out.println("Waiting for client...");
// open client socket to accept connection
client = server.accept();
System.out.println(client.getInetAddress()+" contacted ");
System.out.println("Creating thread to serve request");
ServerStudentThread student = new ServerStudentThread(client);
student.start();
}
}
}
地址已在使用:JVM\u Bind
-表示您的操作系统在上次使用后未关闭套接字。超时约30-180秒时关闭
我不知道如何在java中实现这一点,但在C
code中,在bind
系统函数调用之前,可以这样做:
int yes = 1;
setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
int yes=1;
setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int));
这意味着:将标志(选项)设置为sockfd
socket
在java中,必须存在相应的机制来执行同样的操作。在最后
块中调用server.close()
ServerSocket server = new ServerSocket(PORT);
try {
while (true) {
System.out.println("Waiting for client...");
// open client socket to accept connection
Socket client = server.accept();
System.out.println(client.getInetAddress()+" contacted ");
System.out.println("Creating thread to serve request");
ServerStudentThread student = new ServerStudentThread(client);
student.start();
}
} finally {
server.close();
}
您正在运行一个无限while循环,有一个布尔变量来表示何时停止,我认为您没有优雅地退出,这就是端口未关闭的原因
也许你可以这样试试
import java.io.*;
import java.net.*;
import java.util.*;
public class Server2 {
static int NUM_CONN_TO_WAIT_FOR=15;
boolean exitServer =false;
public static void main(String args[]) throws Exception {
int PORT = 5555; // Open port 5555
//open socket to listen
ServerSocket server = new ServerSocket(PORT);
Socket client = null;
static int connections =0;
try
{
while (!exitServer ) {
System.out.println("Waiting for client...");
// open client socket to accept connection
if ( connections < NUM_CONN_TO_WAIT_FOR )
{
client = server.accept();
System.out.println(client.getInetAddress()+" contacted ");
System.out.println("Creating thread to serve request");
ServerStudentThread student = new ServerStudentThread(client);
student.start();
} else
{
exitServer =true;
}
connections++;
}
} catch (Exception e)
{
System.out.println(e.printStackTrace());
}
finally
{
if ( client != null)
client.close();
if ( server!= null)
server.close();
}
}
}
import java.io.*;
导入java.net。*;
导入java.util.*;
公共类服务器2{
静态整数连接到等待=15;
布尔exitServer=false;
公共静态void main(字符串args[])引发异常{
int PORT=5555;//打开端口5555
//打开插座收听
ServerSocket服务器=新的ServerSocket(端口);
socketclient=null;
静态int连接=0;
尝试
{
而(!exitServer){
System.out.println(“等待客户…”);
//打开客户端套接字以接受连接
if(连接数
您似乎已经回答了自己的问题。“你试过你自己的建议了吗?”马塞洛·坎托斯:但我不知道如何关闭插座和把它放在哪里。无论我把socket.close()放在哪里,它都说错了。你需要有一个关机钩子,每当你收到进程终止信号,然后将exitServer设置为true,这应该是一个易失性变量,发生在单独的线程中。另外,看看Tomcat HttpServer的源代码,你可能会有一个想法!!谢谢你找出我的错误。但是如果我不知道要等待什么呢。无论如何,谢谢!现在在Java7中,您可以尝试使用资源<代码>尝试(ServerSocket服务器=新的ServerSocket(端口)){…}
。它将在最后正确释放资源(将执行空检查等)