I'm working on a simple Java server to accept a port number and display internet data such as text, images, and directories and I 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 the 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?