Java Socket Closes After Connection?
        Posted  
        
            by 
                Matthew
            
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Matthew
        
        
        
        Published on 2012-04-09T05:27:13Z
        Indexed on 
            2012/04/09
            5:28 UTC
        
        
        Read the original article
        Hit count: 193
        
java
Why does this port/socket close once a connection has been made by a client?
package app;
import java.io.*;
import java.net.*;
public class socketServer {
public static void main(String[] args) {
    int port = 3333;
    boolean socketBindedToPort = false;
    try {
        ServerSocket ServerSocketPort = new ServerSocket(port);
        System.out.println("SocketServer Set Up on Port: " + port);
        socketBindedToPort = true;
        if(socketBindedToPort == true) {
            Socket clientSocket = null;
            try {
                clientSocket = ServerSocketPort.accept();//This method blocks until a socket connection has been made to this port.
                System.out.println("Waiting for client connection on port:" + port);
                /** THE CLIENT HAS MADE A CONNECTION **/
                System.out.println("CLIENT IS CONENCTED");
            } 
            catch (IOException e) {
                System.out.println("Accept failed: " + port);
                System.exit(-1);
            }
        }
        else {
            System.out.println("Socket did not bind to the port:" + port);
        }
    }
    catch(IOException e) {
        System.out.println("Could not listen on port: " + port);
        System.exit(-1);
    }
}
}
© Stack Overflow or respective owner