Sockets

An abstraction used in networking

The term socket is overloaded, in the sense that there are multiple meanings of the word.

It can mean:

Often these two “sockets” are in a one-to-one relationship, but not always. There are times when one <IP-address,port> pair may correspond to multiple “sockets” in the OS abstraction sense (this happens more often with server side sockets than client side sockets.)

In the Java programming language there is a class Socket that depending on the context, may correspond to either of these meanings, but it is close to the second sense of the word socket.

Some Socket Code Examples from Chapter 15 of HFJ

BindException

A question that sometimes gets asked on Homework and/or Exams is this one:

Some versions of this question were more specific and said that the socket was being created with the line of code:

Socket s = new Socket("127.0.0.1",20000);

Unfortunately, that’s not really a good example to use. Better to use this, taken from the VerySimpleChatServer.java program of Chapter 15:

ServerSocket serverSock = new ServerSocket(5000);

And the best way to see the error actually occur is to run that program on the same system, in two different shell windows:

In one window:

169-231-163-224:hfj-chap15 pconrad$ java -cp build chap15/VerySimpleChatServer
...

In the other:

169-231-163-224:hfj-chap15 pconrad$ java -cp build chap15/VerySimpleChatServer
java.net.BindException: Address already in use
	at java.net.PlainSocketImpl.socketBind(Native Method)
	at java.net.AbstractPlainSocketImpl.bind(AbstractPlainSocketImpl.java:382)
	at java.net.ServerSocket.bind(ServerSocket.java:375)
	at java.net.ServerSocket.<init>(ServerSocket.java:237)
	at java.net.ServerSocket.<init>(ServerSocket.java:128)
	at chap15.VerySimpleChatServer.go(Unknown Source)
	at chap15.VerySimpleChatServer.main(Unknown Source)
169-231-163-224:hfj-chap15 pconrad$ 

The problem is that on a given system, you can only have one server listening on a given port at a time.

One way to fix the error is to allow the user to: