Java 无法使服务器套接字关闭
我正在制作一个简单的聊天服务器,使每个连接在一个新线程上运行 旧版本为服务器启动了一个线程,它执行了一个while循环,在发送停止消息时停止,然后关闭套接字 新版本将永远循环,并为每个新连接创建一个新线程。现在我无法关闭套接字连接 如果按下一个键,主螺纹停止,插座将保持打开状态。因此,当我再次运行程序时,我需要更改套接字编号 服务器代码Java 无法使服务器套接字关闭,java,sockets,tcp,Java,Sockets,Tcp,我正在制作一个简单的聊天服务器,使每个连接在一个新线程上运行 旧版本为服务器启动了一个线程,它执行了一个while循环,在发送停止消息时停止,然后关闭套接字 新版本将永远循环,并为每个新连接创建一个新线程。现在我无法关闭套接字连接 如果按下一个键,主螺纹停止,插座将保持打开状态。因此,当我再次运行程序时,我需要更改套接字编号 服务器代码 while(true) { //////////////////////////////////////
while(true)
{
///////////////////////////////////////////////////
// get a new connection
///////////////////////////////////////////////////
System.out.println("Aceepting connections on port 1030 \r");
try{
// Get New Connection
// wait for ever on accepting new connections
server.setSoTimeout(0);
connection=server.accept();
cConnection thread = new cConnection("thread3", connection);
} catch(IOException ec)
{
System.out.println(ec.getMessage());
}
}
启动服务器的代码
现在,每条消息都以一个新线程的形式出现,因此我无法告诉它停止并关闭套接字。您需要提供一个必须是全局可访问的标志,以便当某些客户端想要停止服务器时,更改变量ans停止bucle。举例来说:
class YourServer {
private static boolean execute = true;
public static synchronized void stop() {
execute = false;
}
public void yourMethod() {
while(execute) {
// implement your server here
}
}
}
当客户端发送命令STOP时,您必须执行以下操作
YourServer.stop();
如果您想要一个停止命令来停止服务器,您可以调用System.exit()来强制程序存储,或者只需关闭
服务器。查看您的问题,我明白了一件事,因为您正在
而(true),所以您的控件总是卡在connection=server.accept()上;正在侦听新连接。因此,为了停止套接字,首先需要找到一种方法来停止while循环中的循环。或者您可以设置一个变量,比如(int clientsConnected)来检查客户机的数量,当客户机数量为零时,停止while循环。所以你可以停止你的插座
下面是我为客户端编写的示例代码,它在关闭套接字时也执行相同的操作。
希望这能解决你的问题
class GetNamesFromServer implements Runnable
{
private Socket sForName, sForId;
private BufferedReader in, inForName, inForId;
private PrintWriter outForName, outForId;
private static String clientNames;
public GetNamesFromServer(Socket s1, Socket s2)
{
sForName = s1;
sForId = s2;
}
public void run()
{
try
{
outForName = new PrintWriter(sForName.getOutputStream(), true);
outForName.println(Client.clientName);
System.out.println("Send Name : " + Client.clientName);
outForName.flush();
}
catch(IOException e)
{
System.err.println("Error sending Name to the Server.");
}
try
{
inForId = new BufferedReader(new InputStreamReader(sForId.getInputStream()));
Client.clientId = (inForId.readLine()).trim();
System.out.println("Client ID is : " + Client.clientId);
}
catch(IOException e)
{
System.err.println("Error Receiving ID from Server.");
}
try
{
inForName = new BufferedReader(new InputStreamReader(sForName.getInputStream()));
while (true)
{
clientNames = inForName.readLine();
if (clientNames != null && clientNames != "")
{
clientNames = clientNames.substring(1, clientNames.length() - 1);
System.out.println("Names Received : " + clientNames);
String[] names = clientNames.split(", ");
Client.nameClients.clear();
for (String element: names)
Client.nameClients.add(element);
Client.nPane.setText("");
int size = Client.nameClients.size();
System.out.println("Size of list : " + size);
for (int i = 0; i < size; i++)
{
String name = Client.nameClients.get(i);
String colour = Character.toString(name.charAt(0));
name = name.substring(1, name.length()) + "\n";
appendToNamePane(name, ReceiveMessages.getColour(Integer.parseInt(colour)), "Lucida Console");
}
System.out.println("Clients Online : " + Client.nameClients);
}
int index = Client.nameClients.indexOf(Client.clientId + Client.clientName);
**if (index == -1)
{
sForName.close();
break;
}**
}
}
catch(IOException e)
{
System.err.println("Error Receiving Names of Clients from Server");
}
}
问候
if (totalClients == 0)
{
socket.close();
serverSocket.close();
}