-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.java
More file actions
140 lines (137 loc) ยท 3.89 KB
/
Client.java
File metadata and controls
140 lines (137 loc) ยท 3.89 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
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.Scanner;
public class Client {
static class ClientReceiver extends Thread{
Socket socket;
private DataInputStream in;
ClientReceiver(Socket socket){
this.socket = socket;
try {
in = new DataInputStream(socket.getInputStream());
}catch(IOException e) { }
}
public void run() {
while(in != null) {
try {
System.out.print(in.readUTF());
} catch(Exception e) { }
} } }
static class ClientSender extends Thread{
Socket socket;
private Scanner scan = new Scanner(System.in);
private DataOutputStream out;
private DataInputStream in;
private String id;
ClientSender(Socket socket){
this.socket = socket;
try {
in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
}catch(IOException e) { }
}
public void run() {
String title = "";
String author = "";
try {
if(out != null) {
System.out.print("Enter userID>> ");
String id = "";
while(true){
id = scan.nextLine();
if(id.split(" ").length > 1) {
System.out.print("UserID must be a single word with lowercase alphabets and numbers.\n");
}
else {
int i;
int chk = 0;
for(i = 0; i < id.length(); i++) {
if(!(id.charAt(i) > 47 && id.charAt(i)< 58) &&
!(id.charAt(i) > 96 && id.charAt(i) < 123))
{ chk = 1; break; }
}
if(chk == 0) {
System.out.print("Hello "+ id+"!\n"+id+">> ");
out.writeUTF(id);
break;
}else
System.out.print("UserID must be a single word with lowercase alphabets and numbers.\n");
}
System.out.print("Enter userID>> ");
}
}
while(out != null) { // request
String request = scan.nextLine();
if(request.equals("add")) {
System.out.print("add-title> ");
title = scan.nextLine();
if(title.length() != 0) {
System.out.print("add-author> ");
author = scan.nextLine();
if(author.length() != 0) {
out.writeUTF(request);
out.writeUTF(title);
out.writeUTF(author);
}else
out.writeUTF("error");
}else
out.writeUTF("error");
} else if(request.equals("borrow")){
System.out.print("borrow-title> ");
title = scan.nextLine();
if(title.length() != 0) {
out.writeUTF(request);
out.writeUTF(title);
}else
out.writeUTF("error");
} else if(request.equals("return")){
System.out.print("return-title> ");
title = scan.nextLine();
if(title.length() != 0) {
out.writeUTF(request);
out.writeUTF(title);
}else
out.writeUTF("error");
} else if(request.equals("info")){
out.writeUTF(request);
}else if(request.equals("search")){
while(true) {
System.out.print("Search-string> ");
String search = scan.nextLine();
if(search.length() > 2) {
out.writeUTF(request);
out.writeUTF(search);
break;
}else if(search.length() == 0) {
out.writeUTF("error");
break;
}else System.out.print("Search string must be longer than 2 characters.\n");
}
}
else
out.writeUTF(request);
}
}catch(Exception e) {}
scan.close();
}
}
public static void main(String[] args) {
if(args.length != 2) {
System.out.println("Please give the IP address and port number as arguments.");
System.exit(0);
}
try {
String serverIp = args[0];
int serverPort = Integer.parseInt(args[1]);
Socket socket = new Socket(serverIp,serverPort);
Thread receiver = new Thread(new ClientReceiver(socket));
Thread sender = new Thread(new ClientSender(socket));
receiver.start();
sender.start();
}catch (Exception e) {
System.out.print("Connection establishment failed.");
}
}
}