Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Knock Knock Server

According to this website, http://docs.oracle.com/javase/tutorial/networking/sockets/clientServer.html it says this :

Server: "Knock knock!"

Client: "Who's there?"

Server: "Dexter."

Client: "Dexter who?"

Server: "Dexter halls with boughs of holly."

Client: "Groan."

I thought that if the server is listening on a port that it is waiting for a knock, so the client would be knocking and the server would be saying who's there. I've checked other websites as well and have seen the same client/server responses.

Can someone please explain why this is not the case?

like image 395
Brandon Ling Avatar asked Oct 29 '12 20:10

Brandon Ling


People also ask

What is ServerSocket?

ServerSocket is a java.net class that provides a system-independent implementation of the server side of a client/server socket connection. The constructor for ServerSocket throws an exception if it can't listen on the specified port (for example, the port is already being used).

What is client server in Java?

The client and server are the two main components of socket programming. The client is a computer/node that request for the service and the server is a computer/ node that response to the client. In Java, using socket programming, we can connect the client and server.

How do I listen to a port in Java?

Invoke the ServerSocket 's accept() method to listen on the configured port for a client connection. When a client connects to the server, the accept() method returns a Socket through which the server can communicate with the client.


1 Answers

That tutorial says:

When you successfully get a connection between the client and server, you will see the following text displayed on your screen:

Server: Knock! Knock!

So it's actually the client that initiates the protocol (in network terms) but the server that initiates the joke (in conversational terms). Which I agree is rather confusing.

It might help if you think of it as a Joke Server - in order to get a joke, you have to ask for one! If it was the other way round, you'd have a server that just listens to jokes, which isn't as useful (unless you have absolutely no friends).

like image 174
DNA Avatar answered Sep 27 '22 16:09

DNA