1. Create a Scanner and associate it with the input stream from the socket con-
nected to the client.
2. Create an ObjectOutputStream associated with the above Socket object.
3. Create a FileInputStream , supplying the name of the image/sound fi le as the
single argument to the constructor.
4. Create a File object from the fi le name and use File method length to determine the
size of the fi le. (The File object is not needed after this, so it can be anonymous.)
5. Convert the long value from step 3 into an int and declare a byte array of this size.
(Method length has to return a long , but a byte array will accept only an int for
specifying its size.)
6. Use the FileInputStream object's read method to read the contents of the
FileInputStream into the byte array. (The byte array is supplied as the single
argument to this method.)
7. Use method writeObject of the ObjectOutputStream created in step 1 to send the
byte array to the client.
1. Create an ObjectInputStream and a PrintWriter associated with the relevant
2. Use the PrintWriter object to send a request to the server.
3. Use the readObject method of the ObjectInputStream to receive a fi le from the
4. Typecast the object received in step 3 (from type Object ) into byte .
5. Create a FileOutputStream object, supplying a string fi le name for the fi le with
which the FileOutputStream is to be associated.
6. Use the FileOutputStream object's write method to fi ll the fi le, supplying the
name of the byte array as the argument to this method.
Hopefully, all of this will fall into place when you see the code for the following
In this example, a server accepts connections from clients and returns to each client
either an image fi le called beesting.jpg (if the client sent the single-word request
'IMAGE') or a sound fi le called cuckoo.wav (if the client sent the request 'SOUND').
Upon receipt of an image, the client saves it in a fi le called image.jpg (assuming, for
simplicity's sake, that we know the fi le is going to be one in this format). Upon
receipt of a sound fi le, the client saves it with the name sound.wav (again assuming
that we know the fi le is going to be one in this format).
In the code that follows, the (now familiar?) convention of commenting in
bold type each of the lines associated with one of the steps described above has been
followed. The processing of the fi le to be transmitted (whether it be the image fi le
or the sound fi le) is handled by method sendFile , whilst the processing of the
received fi le is handled by method getFile .