-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServerOperations.java
More file actions
67 lines (58 loc) · 2.39 KB
/
Copy pathServerOperations.java
File metadata and controls
67 lines (58 loc) · 2.39 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
package com.vikas;
import org.apache.commons.lang3.StringUtils;
import java.io.*;
import java.net.Socket;
public class ServerOperations extends Thread {
private final Socket clientSocket;
private InputStream inputStream;
private OutputStream outputStream;
public ServerOperations(Socket clientSocket) {
this.clientSocket = clientSocket;
}
@Override
public void run() {
try {
handleClientSocketOperations();
} catch (IOException e) {
e.printStackTrace();
}
}
public void handleClientSocketOperations() throws IOException {
inputStream = clientSocket.getInputStream();
outputStream = clientSocket.getOutputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = br.readLine()) != null) {
String[] tokens = StringUtils.split(line);
if (tokens.length > 0) {
String command = tokens[0];
if (command.equalsIgnoreCase("quit")) {
System.out.println("Quit Command Found");
break;
} else if (command.equalsIgnoreCase("login")) {
handleLoginOperation(tokens);
} else {
String command_error = "command '" + command + "' not found!!\n";
System.out.println(command_error);
outputStream.write(command_error.getBytes());
}
}
}
clientSocket.close();
}
private void handleLoginOperation(String[] tokens) throws IOException {
System.out.println("Login Command");
if (tokens.length >= 3) {
String username = tokens[1];
String password = tokens[2];
if (username.equalsIgnoreCase("vikas") && password.equalsIgnoreCase("dixit") || username.equalsIgnoreCase("rahul") && password.equalsIgnoreCase("dixit") || username.equalsIgnoreCase("guest") && password.equalsIgnoreCase("guest")) {
String loginSuccessMsg="Welcome "+username+"\n";
System.out.println("Login Successful\n"+loginSuccessMsg);
outputStream.write(loginSuccessMsg.getBytes());
}else {
System.out.println("Login Failed ");
outputStream.write("login failed\n".getBytes());
}
}
}
}