77import java .util .ArrayList ;
88
99public class Exercise_31_06Server implements StudentAddressConstants {
10- private ObjectOutputStream outputToClient ;
11- private ObjectInputStream inputFromClient ;
1210 private ArrayList <StudentAddress > listOfStudentAddresses = new ArrayList <>();
1311 private int currentIndex = 0 ;
1412 private int countOfClients = 0 ;
@@ -21,7 +19,7 @@ public Exercise_31_06Server() {
2119 try {
2220 ServerSocket serverSocket = new ServerSocket (8000 );
2321 System .out .println ("Server started!" );
24-
22+
2523 while (countOfClients < 2 ) {
2624 Socket socket = serverSocket .accept ();
2725 countOfClients ++;
@@ -44,45 +42,76 @@ public HandleAClient(Socket socket) {
4442 @ Override
4543 public void run () {
4644 try {
47- inputFromClient = new ObjectInputStream (socket .getInputStream ());
48- outputToClient = new ObjectOutputStream (socket .getOutputStream ());
45+ ObjectInputStream inputFromClient = new ObjectInputStream (socket .getInputStream ());
46+ ObjectOutputStream outputToClient = new ObjectOutputStream (socket .getOutputStream ());
47+ int currentIndex = 0 ;
4948
5049 while (true ) {
5150 int request = inputFromClient .readInt ();
5251 switch (request ) {
5352 case ADD :
5453 StudentAddress element = (StudentAddress )(inputFromClient .readObject ());
5554 listOfStudentAddresses .add (element );
56- currentIndex = 0 ;
55+ outputToClient .writeInt (listOfStudentAddresses .size () - 1 );
56+ outputToClient .flush ();
5757 break ;
5858 case FIRST :
5959 if (!listOfStudentAddresses .isEmpty ()) {
60+ outputToClient .writeBoolean (true );
61+ outputToClient .flush ();
6062 outputToClient .writeObject (listOfStudentAddresses .get (0 ));
61- currentIndex = 0 ;
63+ outputToClient .flush ();
64+ }
65+ else {
66+ outputToClient .writeBoolean (false );
67+ outputToClient .flush ();
6268 }
6369 break ;
6470 case NEXT :
65- if (currentIndex < listOfStudentAddresses .size () - 1 ) {
66- outputToClient .writeObject (listOfStudentAddresses .get (++currentIndex ));
71+ outputToClient .writeInt (listOfStudentAddresses .size ());
72+ outputToClient .flush ();
73+ currentIndex = inputFromClient .readInt ();
74+ if (listOfStudentAddresses .size () > 1 && currentIndex < listOfStudentAddresses .size ()) {
75+ outputToClient .writeBoolean (true );
76+ outputToClient .flush ();
77+ outputToClient .writeObject (listOfStudentAddresses .get (currentIndex ));
78+ outputToClient .flush ();
79+ }
80+ else {
81+ outputToClient .writeBoolean (false );
82+ outputToClient .flush ();
6783 }
6884 break ;
6985 case PREVIOUS :
70- if (currentIndex > 0 && !listOfStudentAddresses .isEmpty ()) {
71- outputToClient .writeObject (listOfStudentAddresses .get (--currentIndex ));
86+ currentIndex = inputFromClient .readInt ();
87+ if (listOfStudentAddresses .size () > 1 && currentIndex >= 0 ) {
88+ outputToClient .writeBoolean (true );
89+ outputToClient .flush ();
90+ outputToClient .writeObject (listOfStudentAddresses .get (currentIndex ));
91+ outputToClient .flush ();
92+ }
93+ else {
94+ outputToClient .writeBoolean (false );
95+ outputToClient .flush ();
7296 }
7397 break ;
7498 case LAST :
99+ outputToClient .writeInt (listOfStudentAddresses .size () - 1 );
75100 if (!listOfStudentAddresses .isEmpty ()) {
101+ outputToClient .writeBoolean (true );
102+ outputToClient .flush ();
76103 currentIndex = listOfStudentAddresses .size () - 1 ;
77104 outputToClient .writeObject (listOfStudentAddresses .get (currentIndex ));
105+ outputToClient .flush ();
106+ }
107+ else {
108+ outputToClient .writeBoolean (false );
109+ outputToClient .flush ();
78110 }
79111 break ;
80112 default :
81113 break ;
82114 }
83- for (StudentAddress data : listOfStudentAddresses ) {
84- System .out .println (data );
85- }
86115 }
87116 }
88117 catch (IOException ex ) {
0 commit comments