Everyone is aware of socket programming in java. we write a code as below:
ServerSocket serverSocket = new ServerSocket(1234);
Socket server = serverSocket.accept();
We know that we create object of serverSocket and next we write serverSocket.accept();
code to receive client request. we know that serverSocket.accept();
wait until new request comes.
but my question is : what serverSocket.accept();
method does internally? might be is running in while loop ? how server identify that any new request is came to serve ? what is the internal implmentation of serverSocket.accept();
method? Any One has idea about this?
On Linux, the ServerSocket.accept()
ultimately (in native code) does an accept
syscall (see man 2 accept
) which blocks waiting for a suitable incoming connection.
There is no while
loop in the Java code or in the native code. I've no idea what happens inside the Linux kernel, but at that point this is no longer a Java question.
The same would probably apply for Java on Windows, and for C# or any other programming language that you cared to consider.