3

Hopefully someone can shed some light on this one, and provide a workaround, or illuminate some method I'm missing here.

Just messing around trying to create a really simple web server.

So far I have:

public class SocketTest {

    public static void main(String[] args) throws IOException {

        ServerSocket sock = new ServerSocket(9876);

        while(true) {

            System.out.println("Before");
            Socket conn = sock.accept();
            System.out.println("After");

            conn.close();

        }
    }
}

Then, once the project is running, the output window displays:

Before

Which is what I expect, but then as soon as I type:

127.0.0.1:9876

Into a web browser, I end up with:

Before
After
Before
After
Before
After
Before

Showing. So the browser is obviously connecting to that port multiple times, but only the first connection contains any info in its page header.


While writing this, I've decided to go a little further with my experimentation's, and come up with:

public class SocketTest {

    public static void main(String[] args) throws IOException {

        ServerSocket sock = new ServerSocket(9876);

        while(true) {

            System.out.println("Before");
            Socket conn = sock.accept();
            System.out.println("After");

            Scanner sc = new Scanner(conn.getInputStream());

            System.out.println("BEFORE if...");

            if (sc.hasNext()) {

                System.out.println("INSIDE if...");

                String[] cnr = sc.nextLine().split("\\s+");
                System.out.println("Command: " + cnr[0] + "  -  URL: " + cnr[1]);

            } else {

                System.out.println("INSIDE ELSE!");

            }

            System.out.println("Closing...");

            conn.close();

            System.out.println("Closed!");
        }
    }
}

If you compile and run this code, you'll notice that upon first opening a page on the socket, you get the repeated socket opening, but it also appears to hang for about 10 seconds at the line:

if (sc.hasNext()) {

As though it's watching the InputStream() for a set period of time, to see if it contains any information or not..?

Also, if I send two actual pages in quick succession (or just refresh the page), it goes through immediately.

Can anyone shed some light on this?

Either how I can remove the waiting on an InputStream() that's never going to materialise, or how I can get it to ignore the phantom sockets.

Even a bit of background as to what's causing the anomaly would be great!

3
  • What is phantom sockets? Commented Dec 12, 2013 at 11:49
  • By phantom sockets, I mean a socket that's created, but has nothing present in its getInputStream(). Commented Dec 12, 2013 at 11:58
  • @MasterTeeee : did you find anything ?? Why you are getting phantom sockets???? Commented Nov 4, 2017 at 13:37

1 Answer 1

1

The accepting thread should never wait on socket's input, it should delegate data exchange to a separate thread and immediately return to execution of ServerSocket.accept() like it is described at the end of this example. For highly loaded servers (thousands of simultaneous connections), that threads can consume too much memory, so asynchronous IO can be used.

Sign up to request clarification or add additional context in comments.

4 Comments

I understand that, but this is a single threaded process. The socket is opened. The thread is made. The thread waits to see if any input is coming through the input stream. The thread's stream timeout (presumably)? The thread and the socket close. I want to eliminate that timeout, because no page is being loaded. This is more an exercise for me to understand socket behavior better, without the added complexity of multiple threads.
As Einstein said, Everything should be made as simple as possible, but not simpler. Since there are several clients trying to access the server in parallel, the solution with one thread per client (+1 thread for accepting connections) is most simple and natural. It can be solved with single thread, using asynchronous programming and non-blocking IO, but such solution would be more complex.
So there's more than one client connecting to it? How is that possible? My web browser is the only item connecting to the socket... This is never intended to actually be used by anyone other than myself.
Each request from the browser creates separate connection.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.