当服务器在Java中关闭时,如何重新连接客户端? [英] How do I reconnect a client when server is down in Java?
问题描述
我有一个服务器,它可以在客户端连接时接受套接字.我希望能够关闭本地服务器,并让客户端尝试重新连接大约5次,如果我启动服务器,客户端应指示您已重新连接.
I have a server that accepts sockets from whenever a client connects. I want to be able to shutdown my local server and let my client try to reconnect for about 5 times, and if I start my server the client should indicate that you have reconnected again.
我有点理解,这是在try{} catch(IOException){Here goes the code for handleing reconnect}
中完成的,我想使用我最初用来连接的同一套接字.我不想创建new Client
原因,然后必须再次输入用户名和类似的内容
I understand somewhat that this is done in the try{} catch(IOException){Here goes the code for handleing reconnect}
I want to use the same socket that I first used to connect. I don't want to create a new Client
cause then I have to enter username and stuff like that all over again
我试图创建一个像clientSocket = new Socket("localhost", portnr)
这样的新套接字,但是我不知道这是否是正确的方法.如果您有回答此问题的示例,请链接它们.我不介意阅读,只要有据可查.预先感谢!
I tried to creating a new socket like clientSocket = new Socket("localhost", portnr)
but I don't know if this is the correct way to go. If you have examples that answers this please link them. I dont mind reading as long as it is good documented. Thanks in advance!
编辑. 这是我的客户班
EDIT. Here is my Client Class
public class Client {
public static void main(String[] args) {
Client client = new Client();
client.connect();
}
//------------------------------------------------------------
//METHOD CONNECT
//------------------------------------------------------------
private void connect(){
int reConnectTries = 0;
Socket clientsocket;
try {
//------------------------------------------------
//Sets up variables needded for execution
clientsocket = new Socket("localhost", 8900);
DataOutputStream OUT = new DataOutputStream(clientsocket.getOutputStream());
ListenforMessages listen = new ListenforMessages(clientsocket);
//We don't want to enter username all the time
//So this goes not in the while-loop
//------------------------------------------------
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter username");
String username = keyboard.nextLine();
//Sends username to sever so it can be added to a list
OUT.writeUTF(username);
//------------------------------------------------
//------------------------------
//Creates a thread to listen on messages from server(Other clients in this case)
Thread trd = new Thread(listen);
trd.start();
//------------------------------
while (true) {
try {
String sendMessage = keyboard.nextLine();
OUT.writeUTF(sendMessage);
OUT.flush();
} catch (Exception e) {
System.err.println("Could not send message to server. " + e);
}
}
} catch (IOException e) {
System.err.println("Couldnt establish a connection: " + e);
}
}
//------------------------------------------------------------
//CLASS FOR HANDLEING INPUT. We create a class for input on a new thread
//This is cause we don't want it to block other processes.
//----------------------------------------------------------------
class ListenforMessages implements Runnable{
Socket mySocket;
DataInputStream IN;
public ListenforMessages(Socket X) throws IOException {
this.mySocket = X;
}
@Override
public void run() {
try {
IN = new DataInputStream(mySocket.getInputStream());
while (true) {
System.out.println(IN.readUTF());
}
} catch (Exception e) {
System.err.println("Couldn't fetch message from server.Error: " + e);
}
}
}
}
推荐答案
有两个解决方案可以解决此问题,但简单的方法是让客户端尝试在设置时重新连接(打开与服务器的新连接)间隔.例如,您可以尝试执行以下操作以使客户端每3分钟尝试重新连接一次:
There's a couple of solutions to this problem, but a simple one would to be have the client try to reconnect (open a new connection to the server) at set intervals. For example, you could try something like this to have your client try to reconnect once every 3 minutes:
while(true) {
try {
clientSocket = new Socket("localhost", portnr);
break; // We connected! Exit the loop.
} catch(IOException e) {
// Reconnect failed, wait.
try {
TimeUnit.MINUTES.sleep(3);
} catch(InterruptedException ie) {
// Interrupted.
}
}
}
这样,客户端将尝试连接,如果连接失败,请等待3分钟再尝试.
This way, the client will try to connect, and if it fails, wait for 3 minutes before trying again.
这篇关于当服务器在Java中关闭时,如何重新连接客户端?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!