Skip to content

Commit c9634a6

Browse files
committed
commit Exercise_31_06
1 parent 29e865a commit c9634a6

File tree

3 files changed

+73
-21
lines changed

3 files changed

+73
-21
lines changed

Chapter_31/exercise_31_06Client/Exercise_31_06Client.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public class Exercise_31_06Client extends Application implements StudentAddressC
2929

3030
private String host = "localhost";
3131
private Socket socket;
32+
private int index = 0;
3233
private ObjectOutputStream outputToServer;
3334
private ObjectInputStream inputFromServer;
3435

@@ -78,6 +79,8 @@ public void start(Stage primaryStage) {
7879
StudentAddress address = getDataFromTextFields();
7980
outputToServer.writeInt(ADD);
8081
outputToServer.writeObject(address);
82+
outputToServer.flush();
83+
index = inputFromServer.readInt();
8184
}
8285
catch (IOException ex) {
8386
ex.printStackTrace();
@@ -99,7 +102,33 @@ private void setButtonAction(Button bt, int request) {
99102
bt.setOnAction(e -> {
100103
try {
101104
outputToServer.writeInt(request);
102-
setTextFields((StudentAddress)(inputFromServer.readObject()));
105+
outputToServer.flush();
106+
switch(request) {
107+
case FIRST :
108+
index = 0;
109+
break;
110+
case NEXT :
111+
int size = inputFromServer.readInt();
112+
if(index < size - 1) {
113+
index++;
114+
}
115+
outputToServer.writeInt(index);
116+
outputToServer.flush();
117+
break;
118+
case PREVIOUS :
119+
if(index > 0) {
120+
index--;
121+
}
122+
outputToServer.writeInt(index);
123+
outputToServer.flush();
124+
break;
125+
case LAST :
126+
index = inputFromServer.readInt();
127+
break;
128+
}
129+
if(inputFromServer.readBoolean()) {
130+
setTextFields((StudentAddress)(inputFromServer.readObject()));
131+
}
103132
}
104133
catch (IOException ex) {
105134
ex.printStackTrace();

Chapter_31/exercise_31_06Client/StudentAddressConstants.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
package exercise_31_06Client;
22

33
public interface StudentAddressConstants {
4-
public static final int NAME = 32;
5-
public static final int STREET = 32;
6-
public static final int CITY = 20;
7-
public static final int STATE = 4;
8-
public static final int ZIP = 5;
9-
public static final int TOTAL = NAME + STREET + CITY + STATE + ZIP;
104
public static final int ADD = 1;
115
public static final int FIRST = 2;
126
public static final int NEXT = 3;

Chapter_31/exercise_31_06Server/Exercise_31_06Server.java

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
import java.util.ArrayList;
88

99
public 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

Comments
 (0)