import java.io.*; import java.net.*; import java.util.*; public class StudentServer { // Input from the client on the socket connection private ObjectInputStream inputFromClient; // Output each Student object we receive to a flat file private ObjectOutputStream outputToFile; public static void main( String[] args ) { new StudentServer(); } public StudentServer() { try { // Create a server socket ServerSocket serverSocket = new ServerSocket( 8125 ); System.out.println( "Server started at " + new Date() ); // Create an object output stream to write to a file outputToFile = new ObjectOutputStream( new FileOutputStream( "s.dat", true ) ); while ( true ) { // Listen for a new connection request Socket socket = serverSocket.accept(); // BLOCKED // Create an input stream from the socket inputFromClient = new ObjectInputStream( socket.getInputStream() ); // Read from input Object object = inputFromClient.readObject(); // BLOCKED if ( object instanceof Student ) { Student s = (Student)object; System.out.println( s.getId() ); } // Write to the file outputToFile.writeObject( object ); System.out.println( "Wrote a new student object to the file" ); } } catch( ClassNotFoundException ex ) { ex.printStackTrace(); } catch( IOException ex ) { ex.printStackTrace(); } finally { try { inputFromClient.close(); outputToFile.close(); } catch ( Exception ex ) { ex.printStackTrace(); } } } }