-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.java
More file actions
129 lines (121 loc) · 4.39 KB
/
Client.java
File metadata and controls
129 lines (121 loc) · 4.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
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
// A Java program for a Client
import java.net.*;
import java.io.*;
import java.util.*;
import java.util.stream.*;
public class Client
{
// initialize socket and input output streams
private Socket socket = null;
private BufferedReader input = null;
private DataOutputStream out = null;
private DataInputStream in = null;
private void printTabularFormat(String s){
int count = 0;
for(int i = 0; i<s.length();i++){
if(s.charAt(i) == ';') count++;
}
String[] splitted = s.split(";");
String[][] table = new String[count][];
int itr = 0;
for (String string : splitted) {
if(string.length() > 0){
table[itr] = string.split(",");
}
itr++;
}
itr=0;
Map<Integer, Integer> columnLengths = new HashMap<>();
Arrays.stream(table).forEach(a -> Stream.iterate(0, (i -> i < a.length), (i -> ++i)).forEach(i -> {
if (columnLengths.get(i) == null) {
columnLengths.put(i, 0);
}
if (columnLengths.get(i) < a[i].length()) {
columnLengths.put(i, a[i].length());
}
}));
for(int i = 0; i<count; i++){
for(int j = 1; j<table[i].length; j++){
System.out.print(String.format("| %-"+columnLengths.get(j)+"s", table[i][j])+ " " );
}
System.out.println("|");
}
}
// constructor to put ip address and port
public Client(String address, int port)
{
// establish a connection
try
{
socket = new Socket(address, port);
System.out.println("Connected");
// takes input from terminal
input = new BufferedReader(
new InputStreamReader(System.in));
// recieves data from socket
in = new DataInputStream(
new BufferedInputStream(socket.getInputStream()));
// sends output to the socket
out = new DataOutputStream(socket.getOutputStream());
}
catch(UnknownHostException u)
{
System.out.println(u);
}
catch(IOException i)
{
System.out.println(i);
}
// string to read message from input
String line = "";
System.out.println("Supported queries are SELECT, INSERT, UPDATE, DELETE, DESC and SHOW TABLES. Query 'OVER' for closing connection and 'OVER and OUT' for closing connection and shutting down server.");
// keep reading until "Over" is input
try {
while (!line.strip().toUpperCase().equals("OVER") && !line.strip().toUpperCase().equals("OVER AND OUT"))
{
System.out.print("Query: ");
line = input.readLine();
if(line.strip().toUpperCase().equals("SHOW TABLES")){
printTabularFormat("ID,Tables;1,faculty;2,student;");
}
else{
out.writeUTF(line);
String readline = in.readUTF();
if(
(readline.length()>5 && readline.substring(0,5).equals("Error")) ||
(readline.length()>6 && readline.substring(0,6).equals("INSERT")) ||
(readline.length()>6 && readline.substring(0,6).equals("DELETE")) ||
(readline.length()>6 && readline.substring(0,6).equals("UPDATE"))
)
System.out.println(readline);
else
printTabularFormat(readline);
}
}
}
catch(EOFException ex)
{
System.out.println("Closing the Connection");
}
catch(IOException i)
{
System.out.println("Server Offline");
}
// close the connection
try
{
input.close();
out.close();
in.close();
socket.close();
}
catch(IOException i)
{
System.out.println("Server Offline");
}
}
public static void main(String args[])
{
Client client = new Client("127.0.0.1", 5000);
}
}