-1
        if(server!= null){
            try{
                server.endServerConnection();
            }
        }

I thought closing the Server Socket would disconnect the Server. Am I missing something? Any ideas?

Guest1235
  • 27
  • 6

2 Answers2

0

Try closing the server socket any your socket...

public class MyServer {
    public static final int PORT = 12345;
    public static void main(String[] args) throws IOException, InterruptedException {
        ServerSocket ss = ServerSocketFactory.getDefault().createServerSocket(PORT);
        Socket s = ss.accept();
        Thread.sleep(5000);
        ss.close();
        s.close();
    }
}

public class MyClient {
    public static void main(String[] args) throws IOException, InterruptedException {
        Socket s = SocketFactory.getDefault().createSocket("localhost", MyServer.PORT);
        System.out.println(" connected: " + s.isConnected());
        Thread.sleep(10000);
        System.out.println(" connected: " + s.isConnected());
    }
}

See content here...

Community
  • 1
  • 1
kpie
  • 9,588
  • 5
  • 28
  • 50
  • 1
    My server runs on a separate thread, `ServerThread` class, to avoid the gui from freezing. I'm assuming I call ServerThread.sleep() in the server class. Also, my server class doesn't have a socket object (its on the client side to accept connections) – Guest1235 Apr 23 '16 at 21:11
0

My server runs on a separate thread, ServerThread class, to avoid the gui from freezing. I'm assuming I call ServerThread.sleep() in the server class. Also, my server class doesn't have a socket object (its on the client side to accept connections)

As far as I understand you, you have the ServerSocket object declared in the client class and call the ServerSocket:accept() method from there. This method is designed to "survive" any interruptions like closing the underlaying ServerSocket or calling interrupt() on the Thread that holds the socket.

The only way of exiting the ServerSocket:accept() method is a SocketTimeOutException, as mentioned in here.

So here is an example:

ServerSocket server = new ServerSocket(PORT);
server.setSoTimeOut(MILLISECONDS_TO_TIMEOUT);

while(YOUR_SERVER_SHOULD_RUN){

    try{
        Socket s = server.accept();
        //Do stuff with the new socket
    }catch(SocketTimeOutException ignored){}
}

The timeout will cause the ServerSocket to throw an exception when it's over. You can catch this exception and ignor it. The goal is to check the condition of the while loop.

tedy42
  • 68
  • 9
  • 1
    I have my `Socket` and `ServerSocket` objects declared in the `Server` class. I accept the `socket` in the `Server` class, and the `socket` is inside a `while-loop` that runs infinitely to accept all connections. On the `Client` side I have a `Socket` object assigned to a class called `ClientConnection` (extends runnable, handles each instance of each client) – Guest1235 Apr 24 '16 at 17:07
  • 1
    I don't want my Server to simply time out, I wan't it to close upon pressing a button. Please read the question again, if you need more code I can post it. – Guest1235 Apr 24 '16 at 17:08
  • U did not get me right... what I told u is only used to break out of the accept()-method of the server socket; and **NOT** to timeout your server! read the question first and then complain.... if you don't get out of this method you won't have a chance to end the loop where you accept the incoming connections... **THE TIMEOUT IS JUST AND ONLY FOR EXITING THE ACCEPT()-METHOD, CHECKING IF THE SERVER IS SUPPOSED TO RUN** and then calling accept() agein... your button can toggle the condition for the while loop and the server will only shut down when u press your silly button! – tedy42 May 01 '16 at 17:09