0

So I am running a piece of code that is supposed to create a Socket a ServerSocket, when I run my code it meets a stand still at line 14 but doesn't throw any exception. What have I missed?

public class Syncronizer {

    InputStream instr = null;
    PrintStream prnstr = null;
    BufferedReader br = null;
    DataInputStream in;
    DataOutputStream out;
    ServerSocket TCPServerSocket;

    public Syncronizer() {
        try {
            instr = System.in;
            br = new BufferedReader(new InputStreamReader(instr));
            TCPServerSocket = new ServerSocket(7007);
            Socket clientSocket = TCPServerSocket.accept();
            in = new DataInputStream(clientSocket.getInputStream());
            out = new DataOutputStream(clientSocket.getOutputStream());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    ...
}

Just to spare you the counting it is this line that halts my program:

Socket clientSocket = TCPServerSocket.accept();

EDIT: With halt I mean that my program still runs but doesn't go beyond this point in the code just like a infinite loop.

Swidtter
  • 83
  • 1
  • 3
  • 9

1 Answers1

1

accept() is expected to block until a connection comes in on the port the socket the code calls accept() for is listening on.

You might like to RTFM here: http://docs.oracle.com/javase/7/docs/api/java/net/ServerSocket.html#accept%28%29

Listens for a connection to be made to this socket and accepts it. The method blocks until a connection is made.

alk
  • 69,737
  • 10
  • 105
  • 255