John Park
John Park

Reputation: 290

Stuck at socket.accept()

Hello I just started a Java Enterprise Edition class. This is my first exposure to this side of java programming so this is all pretty new to me. I was reading my textbook and decided to type it one of the codes given to me to try it out. This code is not mine. The program should output "hello, enter BYE to exit" and then echo back anything that is typed into the prompt. For some reason the code hangs at the try block containing s.accept (it outputs 1 then 2 then hangs). I was just wondering if anyone would have any insight as to why this isnt working for me when i copied it exactly from my textbook. Here is the code:

import java.io.*;
import java.net.*;
import java.util.*;

public class EchoServer
{
    public static void main (String[] args) throws IOException
    {
    System.out.println("1");
    try (ServerSocket s = new ServerSocket(8189))
            {
    System.out.println("2");
                    try(Socket incoming = s.accept())
                    {
    System.out.println("3");
                            InputStream inStream = incoming.getInputStream();
                            OutputStream outStream = incoming.getOutputStream();

                            try(Scanner in = new Scanner(inStream))
                            {
                            PrintWriter out = new PrintWriter(outStream,true);
                            out.println("Hello! Enter BYE to exit.");
                            boolean done = false;

                                    while(!done && in.hasNextLine())
                                    {

                                            String line = in.nextLine();
                                            out.println("Echo: " + line);
                                                    if(line.trim().equals("BYE"))
                                                            done = true;
                                    }
                            }
                    }

            }
}
} 

Im sure this is something relatively simple to explain, im just new to this and was wondering why this isnt working when i try to run it.

Upvotes: 1

Views: 8756

Answers (3)

skytreader
skytreader

Reputation: 11707

You're dealing with networking. Great. Let's distinguish between terms a bit.

socket.accept() is an example of a blocking function call. I can't find any link to provide a quick interpretation but, to oversimplify, your code stops at that point until some event it is waiting for finally happens, in this case, a connection from a corresponding client. Hence, its behaving normally, as expected, as documented. You'll encounter lots of other blocking function calls waiting for all sorts of events like an item is inserted to a queue, threads finish processing, etc.

In contrast, the word "hang" as normally used, refers to a deadlock or (less commonly), kernel panic. This is usually a mistake on the programmer's part.

Upvotes: 1

Exorcismus
Exorcismus

Reputation: 2482

If you read the documentation you will see that Socket.accept() actually hangs the thread until a client connect , so after the connection is established it will continue also your using an echo protocol , so you need to make sure that the client in this case is ANOTHER server that supports echo protocol

Upvotes: 1

torquestomp
torquestomp

Reputation: 3344

Is there a corresponding EchoClient demo in the textbook?

Socket.accept() hangs by design until a client connects to the port that is being waited on, in this case, port 8189. Your program is working fine

Upvotes: 4

Related Questions