Copyright (c) SEMM NL All rights reserved.
Author : Paul Hamaker. Part of

Two classes, ConnServer and ConnClient, that contain only main methods, so they have to be run stand-alone.

We assume, they run on the same host.

The ConnServer has to be started first.

It uses a and a

The ServerSocket is created to listen to port 7654, presumably not in use by another process.

Port numbers that can be used, are in the range 1025-65535, although some of these may be in use, like 1099 for RMI.

( 80 is the HTTP default, 21 FTP etc. )

If it goes as planned, the server is going to wait for a client request that will arriving at port 7654.

If ConnClient is started now, it will try to connect to this port on the local host, :

If the server accepts the call, it continues.

The client continues, too, by asking the created socket for an outputstream and wrapping it in a, because we want to use its println method :

Likewise, the input stream, wrapped also, so we can use BufferedReader's readLine method :

The server does the same and expects the client to be the first to send something :

which it does.

The flush means : send it immediately !

The server answers.

The client receives.

And sends.

Which is received by the server.

The proper order to close : streams first, followed by their sockets.




By using DataIn/OutputStream or ObjectIn/OutputStreamIt instead you can transfer any datatype.


To make transport much more efficient, you should use a, when dealing with large volumes of data.

This means that physical transport would take place in units of 1024 bytes each.

We show it although it doesn't apply to this example.


To keep this example simple, we've left out things that should be in a real world application, like time-out handling.


When dealing with strings, you should think about using DataIn/OutputStream's read/writeUTF, as an efficient way of transporting Unicode strings.


An applet can connect to a ServerSocket on the host that it originates from.