Hi
I am doing one socket programming in java. If server socket is down for a while then start again after that can get auto connection with the client which the server connect with previously.If possible can any one help me how to do it?
What I have tried:
This are my code. Can help me to handle this?
package multiclients;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
public class STServer {
public static class QueueClientThread extends Thread {
private BufferedReader m_in;
private PrintWriter m_out;
public QueueClientThread(BufferedReader in, PrintWriter out) {
m_in = in; m_out = out;
}
public void run() {
while (true) {
try {
String msg_buf = "Thank You";
while ((msg_buf = m_in.readLine()) != null) {
System.out.println("Response from singlethreaded server: " + msg_buf);
m_out.println("Response from singlethreaded server: " + msg_buf+ " Thank You");
}
}
catch (IOException ex) {
Logger.getLogger(STServer.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
public static void main(String[] args) throws IOException {
Integer.parseInt("5058");
@SuppressWarnings("resource")
ServerSocket serverSocket = new ServerSocket(Integer.parseInt("5058"));
while (true) {
try {
Socket clientSocket = serverSocket.accept();
PrintWriter out =
new PrintWriter(clientSocket.getOutputStream(), true);
new BufferedReader(
new InputStreamReader(clientSocket.getInputStream()));
new QueueClientThread(new BufferedReader(
new InputStreamReader(clientSocket.getInputStream())), out).start();
} catch (IOException ex) {
Logger.getLogger(STServer.class.getName()).log(Level.SEVERE, null, ex);
}
}
}}
Client
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.InterruptedIOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
public class MTServer {
public static class QueueClientThread extends Thread {
private BufferedReader m_in;
private PrintWriter m_out;
public QueueClientThread(BufferedReader in, PrintWriter out) {
m_in = in; m_out = out;
}
public void run() {
while (true) {
try {
String msg_buf = "\0";
while ((msg_buf = m_in.readLine()) != null) {
System.out.println("Message sent back to client: " + msg_buf);
m_out.println("Message sent back to client: " + msg_buf);
}
}catch (InterruptedIOException iioe)
{
System.err.println ("Remote host timed out");
} catch (IOException ex) {
Logger.getLogger(MTServer.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
public static class QueueMTServerThread extends Thread {
private Socket m_Socket;
public QueueMTServerThread(Socket socket) {
m_Socket = socket;
}
@SuppressWarnings("resource")
public void run() {
try (
PrintWriter out =
new PrintWriter(m_Socket.getOutputStream(), true);
BufferedReader in = new BufferedReader(
new InputStreamReader(m_Socket.getInputStream()));
) {
String inputLine;
while ((inputLine = in.readLine()) != null) {
System.out.println(inputLine);
String hostName = "127.0.0.1";
int portNumber = Integer.parseInt("5058");
Socket clientSocket = new Socket(hostName, portNumber);
PrintWriter outputStream = new PrintWriter(
clientSocket.getOutputStream(), true);
outputStream.println(inputLine);
new QueueClientThread(new BufferedReader(
new InputStreamReader(clientSocket.getInputStream())), out).start();
}
} catch (InterruptedIOException iioe)
{
System.err.println ("Remote host timed out");
}catch (IOException e) {
System.out.println(e.getMessage());
}
}
}
@SuppressWarnings({ "resource", "unused" })
public static void main(String[] args) throws IOException {
int portNumber = Integer.parseInt("5056");
try {
ServerSocket serverSocket = new ServerSocket(
Integer.parseInt("5056"));
while (true) {
Socket clientSocket = serverSocket.accept();
new QueueMTServerThread(clientSocket).start();
}
}catch (InterruptedIOException iioe)
{
System.err.println ("Remote host timed out");
} catch (IOException ex) {
Logger.getLogger(MTServer.class.getName()).log(Level.SEVERE, null, ex);
}
}
}