Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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
 
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

        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 26-Apr-12 22:15pm
Comments
Richard MacCutchan at 27-Apr-12 4:56am
   
What happens at the client end?
ryanplawrence at 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)

  Print Answers RSS
0 OriginalGriff 277
1 Maciej Los 220
2 DamithSL 190
3 Raul Iloc 185
4 Richard MacCutchan 175
0 OriginalGriff 5,130
1 DamithSL 4,197
2 Maciej Los 3,670
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,821


Advertise | Privacy | Mobile
Web01 | 2.8.141216.1 | Last Updated 27 Apr 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100