The following code fragment is typical for initiating a socket connection.
This example creates a new Socket connection to port
port at the remote computer host:
Socket socket = new Socket( host, port );
Similarly, the following code demonstrates how we listen for
incoming connections. This example creates a ServerSocket
listening on port port, and then enters an infinite loop,
accepting and processing incoming connections:
ServerSocket serverSocket = new ServerSocket( port );
while (true) {
Socket socket = serverSocket.accept();
doSomethingWithNewConnection( socket );
}
Secure sockets work in a very similar manner, but before we can implement
them for our example we must complete several steps. We'll go over
these steps in the panels that follow.