Sockets

Date: 2/4/2002

Listening on a Port

Listening on a port uses the SocketListener class. The constructor takes a port number to listen on:

SocketServer ss = new SocketServer(1234)

Use any number above 1024, except 8000, 8080, and any other number that may be reserved by another process.

The accept() method of the SocketServer accepts a connection request and returns a socket.

Connecting to a Port

Create a Socket. The constructor takes the name of the server and the port number.

Socket s = new Socket("serverName", 1234);

The name of the server could be the IP address of the your computer if it has one. Or, if you are running Apache, Tomcat, Personal Webserver, or Internet Information Server you can use "localhost" or "127.0.0.1"

To get your computer's IP address, open a DOS window and type in "ipconfig."

Transferring Data

To transfer data, you need to open the stream reader and stream writer on BOTH ends. The writer of one end writes to the reader of the other.