Click here to Skip to main content
15,884,237 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm working on a simple java server to accept a port number and display internet data such as text, images, and directories and am having some trouble getting some code to work. this is my class

Java
public class HTTPServer {

    public static void main(String[] args) {
               
        ServerSocket socket = null;
     
        try 
        {
            socket = new ServerSocket(5000);
        } 
        catch (Exception ex0) 
        {
            System.out.println(ex0.getMessage());
            System.exit(1); // exit with error.
       
        }

This first part works fine but when I try to accept a connection the program stalls. The line "Waiting For Client Connection" is output to screen and then nothing happens

Java
        while(true)
        {
            // the server waits (using an accept call) for a client to connect.
            System.out.println("Waiting For Client Connection.");
            Socket connection = null;
            
            try 
            {
                connection = socket.accept();
            } 
            catch (Exception ex1) 
            {
                System.out.println(ex1.getMessage());
                System.exit(1); // exit with error.
            }
            
            System.out.println("Client Connected To Server");
        }
    }
    
}


The next line "Client Connected To Server" is never output to the screen and exception ex1 is never triggered. I'm very confused about this. Does anyone have any suggestions?
Posted
Comments
Richard MacCutchan 27-Apr-12 4:56am    
What happens at the client end?
ryanplawrence 27-Apr-12 10:24am    
i dont know. i am only required to implement server side for my project

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900