-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultithreadedSocketServer.java
More file actions
186 lines (136 loc) · 5.52 KB
/
MultithreadedSocketServer.java
File metadata and controls
186 lines (136 loc) · 5.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import java.net.*;
import java.util.Date;
import java.io.*;
public class MultithreadedSocketServer {
public static int[] seats = new int[12];
public static void main(String[] args) throws Exception {
System.out.println("Welcome to the DIC lovin train reservation system!");
int counter=0;
try{
ServerSocket server=new ServerSocket(28882);
System.out.println("Server Started ....");
while(true){
counter++;
Socket serverClient=server.accept(); //server accept the client connection request
System.out.println(" >> " + "Client No:" + counter + " started!");
ServerClientThread sct = new ServerClientThread(serverClient,counter); //send the request to a separate thread
sct.start();
}
}catch(Exception e){
System.out.println(e);
}
}
}
class ServerClientThread extends Thread {
Socket serverClient;
int clientNo;
ServerClientThread(Socket inSocket,int counter){
serverClient = inSocket;
clientNo=counter;
}
@Override
public void run(){
try
{
System.out.println("Server Started ....");
// Step 2: Processing the request.
DataInputStream dis = new DataInputStream(serverClient.getInputStream());
DataOutputStream dos = new DataOutputStream(serverClient.getOutputStream());
while (true)
{
// wait for input
String input = dis.readUTF();
if(input.equals("0"))
break;
System.out.println("Values received in server:-" + input);
String result ="";
int seatnumber = 0;
int choice = Integer.parseInt(input);
seatnumber = 0;
// If they chose a window seat, attempt to book it.
if (choice == 1) {
seatnumber = bookWindow();
// No window seats available, try booking an aisle seat for them instead.
if (seatnumber == -1) {
seatnumber = bookAisle();
if (seatnumber != -1) {
result = "Sorry, we were not able to book a window seat. But do have an aisle seat." +seatnumber;
printBoardingPass(seatnumber);
}
}
else {
// Booking a window seat was successful.
result = "You are in luck, we have a window seat available!" + seatnumber;
printBoardingPass(seatnumber);
}
}
else if (choice == 2) {
// If they chose booking an isle, check to see if it is available.
seatnumber = bookAisle();
// If not available, see if we have window seats available.
if (seatnumber == -1) {
seatnumber = bookWindow();
if (seatnumber != -1) {
result = "Sorry, we were not able to book an aisle seat. But do have a window seat." + seatnumber;
printBoardingPass(seatnumber);
}
}
else {
// Booking an aisle seat was successful.
result = "You are in luck, we have an aisle seat available!" + seatnumber;
printBoardingPass(seatnumber);
}
}
else {
// Print an error message if they did not choose 1, 2, or 0 for their choice.
result ="Invalid choice made. Please try again!";
choice = 0;
}
// No window or aisle seats were available.
if (seatnumber == -1) {
result = "We are sorry, there are no window or aisle seats available.";
//System.out.println();
}
dos.writeUTF(result + " seat " + seatnumber );
dos.flush();
}
dis.close();
dos.close();
serverClient.close();
}
catch(Exception ex)
{
System.out.println(ex);
}
finally{
System.out.println("Client -" + clientNo + " exit!! ");
}
}
private static int bookAisle() {
for (int counter = 6; counter < 12; counter++) {
if (MultithreadedSocketServer.seats[counter] == 0) {
MultithreadedSocketServer.seats[counter] = 1;
return counter + 1;
}
}
return -1;
}
private static int bookWindow() {
for (int counter = 0; counter < 6; counter++) {
if (MultithreadedSocketServer.seats[counter] == 0) {
MultithreadedSocketServer.seats[counter] = 1;
return counter + 1;
}
}
return -1;
}
private static void printBoardingPass(int seatnumber) {
Date timenow = new Date();
System.out.println();
System.out.println("Date: " + timenow.toString());
System.out.println("Boarding pass for seat number: " + seatnumber);
System.out.println("This ticket is non-refundable and non-transferable.");
System.out.println("Please be curteous, do not smoke. Enjoy your trip.");
System.out.println();
}
}