0

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?

Bart
  • 19,692
  • 7
  • 68
  • 77
ryan lawrence
  • 39
  • 1
  • 4
  • A silly question but I do not know your level... are you trying to connect (via telnet or through browser) to that port 5000? – SJuan76 Apr 27 '12 at 07:23
  • How are you testing the client connection to your server? – beny23 Apr 27 '12 at 07:23
  • im using netbeans. im a noob so i dont know what im doing. im just going off some code my professor gave me. am i supposed to use a web service from netbeans b/c i just created a class? – ryan lawrence Apr 27 '12 at 07:35
  • @SJaun76 i guess im trying to connect through browser but i dont know how to do that. – ryan lawrence Apr 27 '12 at 07:36
  • @beny23 i think what im trying to do is use server to connect to port. the user being the only client. my professor says we need only worry about the server side and must implement get function – ryan lawrence Apr 27 '12 at 07:38
  • I suggest you work through http://docs.oracle.com/javase/tutorial/networking/sockets/index.html first – artbristol Apr 27 '12 at 07:51
  • @ryanlawrence: Can you edit the post and show how you are connecting as a client? – beny23 Apr 27 '12 at 11:14
  • @beny23 this is all the code i have so far. im using netbeans. should i be using some web service or is the java code enough to make a connection? – ryan lawrence Apr 27 '12 at 14:27
  • @ryanlawrence it doesn't really matter how you connect to your server, e.g pointing your browser to http://localhost:5000/ would do it, but your code won't return from the accept method until a client connects. Calling telnet localhost 5000 would do it as well. – beny23 Apr 27 '12 at 16:57
  • @benny23 my professor says that the server should connect to any web browser as the client. How would I do that? – ryan lawrence Apr 29 '12 at 09:52
  • If your professor really said that he should be demoted. The browser connects to the server, not the other way around. Clearly you have never connected any client to this server, so it just sits around forever until you do. – user207421 Feb 08 '19 at 23:31

1 Answers1

0

I suspect that the problem is that you are calling accept on Socket, not on ServerSocket, that's why nothing happens. See this simple example for the most basic server socket application.

Community
  • 1
  • 1
Piotr Kochański
  • 21,862
  • 7
  • 70
  • 77