Java Reference
In-Depth Information
this section, while the code for the client is an amalgamation of the fi rst part of (Chap. 2 ) and the remainder of .
As with earlier cases, this example is unrealistically simple, but serves to illus-
trate all the required steps of a socket-based client-server application for transmit-
ting whole objects, without overwhelming the reader with unnecessary detail. Upon
receiving the message 'SEND PERSONNEL DETAILS' from a client, the server
simply transmits the ArrayList containing the three Personnel objects used for dem-
onstration purposes in this section.
import java.util.*;
public class PersonnelServer
private static ServerSocket serverSocket;
private static fi nal int PORT = 1234;
private static Socket socket;
private static ArrayList<Personnel> staffListOut;
private static Scanner inStream;
private static ObjectOutputStream outStream;
public static void main(String[] args)
System.out.println("Opening port…\n");
serverSocket = new ServerSocket(PORT);
catch(IOException ioEx)
"Unable to attach to port!");
staffListOut = new ArrayList<Personnel>();
Personnel[] staff =
{new Personnel(123456,"Smith", "John"),
new Personnel(234567,"Jones", "Sally Ann"),
new Personnel(999999,"Black", "James Paul")};
for (int i=0; i<staff.length; i++)
Search WWH ::

Custom Search