Search code examples
javasocketsserversocketreconnect

Connect To a Socket Multiple Times


Ok so when I connect the first time everything works. But then when I connect again (without stopping the server program) it says "Connection refused: connect". I thought it was because I was only accepting the connection once so I used a swing Timer to trigger an action event every 10 milliseconds and every time the action event triggered it set the clientSocket to serverSocket.accept() (clientSocket = serverSocket.accept();) so here is the code:

package org.code;

import java.net.*;
import java.io.*;

public class MainClass {
    public static void main(String[] args) {
            new MainClass();
    }


    Socket server = null;
    PrintWriter out = null;

    public MainClass() {
            try {
                    server = new Socket("192.168.0.104", 4444);
                    out = new PrintWriter(server.getOutputStream(), true);

                    out.println("start");

                    out.close();
                    server.close();
            } catch(Exception ex) {ex.printStackTrace();}
    }
}

And:

package org.code;

import java.io.*;
import java.net.*;
import javax.swing.*;

public class MainClass {
    public static void main(String[] args) {
            new MainClass();
    }

    ServerSocket serverSocket = null;

    Socket clientSocket = null;

    BufferedReader in;

    public MainClass() {
            JFrame frame = new JFrame("Minecraft Server Manager v0.1 Server");
            frame.setSize(500,500);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);

            try {
                    serverSocket = new ServerSocket(4444);

                    clientSocket = serverSocket.accept();

                    in = new BufferedReader(new            InputStreamReader(clientSocket.getInputStream()));

                    String inputLine;

                    while((inputLine = in.readLine()) != null) {
                            System.out.println(inputLine);
                            if(inputLine.equals("start")) {
                                    System.out.println("Good");
                            }
                            if(inputLine.equals("stop")) {
                                    System.out.println("Bad");
                            }
                    }

                    clientSocket.close();
                    serverSocket.close();
            } catch(Exception ex) {System.err.println("Error: " + ex.getMessage());}
    }
}

Solution

  • Your code closes the ServerSocket (and also stops) after every request.

    This is a bit more likely to work. (I didn't tested it, but with this you get the scenario)

    // in your main 
    serverSocket = new ServerSocket(4444);
    
    try {
        while (true) { 
            clientSocket = serverSocket.accept();
    
            Thread t = new ClientSocketThread(clientSocket);
            t.start();
        }
    } finally {
        serverSocket.close();
    }
    
    
    class ClientSocketThread extends Thread {
        final Socket clientSocket;
    
        ClientSocketThread(Socket clientSocket) {
            this.clientSocket = clientSocket;
        }
    
        public void run() {
            InputStream in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
            try
                String inputLine;
    
                while((inputLine = in.readLine()) != null) {
                    System.out.println(inputLine);
                    if(inputLine.equals("start")) {
                         System.out.println("Good");
                    }
                    if(inputLine.equals("stop")) {
                         System.out.println("Bad");
                    }
                }
             } finally {
                in.close()
                clientSocket.close();
             }
        }
    }