Java Reference
In-Depth Information
this section, while the code for the client is an amalgamation of the fi rst part of
MessageClient.java (Chap. 2 ) and the remainder of ArrayListSerialise.java .
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.io.*;
import java.net.*;
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");
try
{
serverSocket = new ServerSocket(PORT);
}
catch(IOException ioEx)
{
System.out.println(
"Unable to attach to port!");
System.exit(1);
}
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++)
staffListOut.add(staff[i]);
startServer();
}
Search WWH ::




Custom Search