1 import java.io.*;
2 import java.net.*;
3
4 public class StudentServer {
5 private ObjectOutputStream outputToFile;
6 private ObjectInputStream inputFromClient;
7
8 public static void main(String[] args) {
9 new StudentServer();
10 }
11
12 public StudentServer() {
13 try {
14
15 ServerSocket serverSocket = new ServerSocket(8001);
16 System.out.println("Server started ");
17
18
19 outputToFile = new ObjectOutputStream(
20 new FileOutputStream("student.dat", true));
21
22 while (true) {
23
24 Socket socket = serverSocket.accept();
25
26
27 inputFromClient =
28 new ObjectInputStream(socket.getInputStream());
29
30
31 Object object = inputFromClient.readObject();
32
33
34 outputToFile.writeObject(object);
35 System.out.println("A new student object is stored");
36 }
37 }
38 catch(ClassNotFoundException ex) {
39 ex.printStackTrace();
40 }
41 catch(IOException ex) {
42 ex.printStackTrace();
43 }
44 finally {
45 try {
46 inputFromClient.close();
47 outputToFile.close();
48 }
49 catch (Exception ex) {
50 ex.printStackTrace();
51 }
52 }
53 }
54 }