Repository navigation
Expand file tree
/
Copy pathserver.java
More file actions
151 lines (99 loc) · 3.78 KB
/
Copy pathserver.java
File metadata and controls
151 lines (99 loc) · 3.78 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
import java.io.*;
import java.net.*;
import java.util.*;
public class Server {
public static void main(String[] args) throws IOException {
String blah = IO.readString(); //Server
int ports = IO.readInt();
ServerSocket svc = new ServerSocket(ports, 5);//listen on port 12345
Socket conn = svc.accept(); //it waits till it gets a connection
String str1 = "Bad input. Try again" + '\n';
//While start
while(true){
String result = "";
// get input/output streams for the socket
BufferedReader fromClient = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
DataOutputStream toClient = new DataOutputStream(conn.getOutputStream());
// reading the data from the client
String line = fromClient.readLine();
System.out.println("got line \" " + line + "\"");
String[] str = line.split(" ");
//now we check each input received from the user
if (str.length > 2)
toClient.writeBytes(str1);
// if the input has bounce in it
else if ( str[0].equalsIgnoreCase("bounce") ) {
//I know that input should be only two parts(for some inputs).
//So I split it to check it later
//making sure that input is in the right structure
if( str.length != 2 ){
toClient.writeBytes(str1);
}else{
//we made sure that structure is right.
//now we bounce back a message to the user.
String snr = str[1];
//Create the result
result = snr.toUpperCase() + '\n';
//Write back the result to the user:
toClient.writeBytes(result);
System.out.println(result + "-> bounced");
}
//end of bounce
} else if (str[0].equalsIgnoreCase("GET")){
//making sure that input is in the right structure
if( str.length != 2 )
toClient.writeBytes(str1 + '\n');
else{
System.out.println("else entered"); //test
String path = str[1];
// path = "src/" + path; //making the file path
System.out.println("path is : " + path);
System.out.println("path has created, ready to test the existance");
File file = new File(path);
String curLine = " ";
//checking whether or not the file exists in our directory
if (file.exists() == false ) {
toClient.writeBytes("ERROR:no such file" + '\n');
System.out.println("ERROR: no such file"); // test
}
// Now that we know the file exists, we get whats inside it
// and send it to the user.
else{
System.out.println("File existance confirmed. Ready to do sending process");//test
BufferedReader input = new BufferedReader(new FileReader(path));
String Line = null; //not declared within while loop
while ( (Line = input.readLine()) != null){
curLine = curLine.concat(";");
curLine = curLine.concat(Line);
//Process line
}//end of while
toClient.writeBytes(curLine + '\n');
System.out.println("User should recieved the data");//test
}// end of else
}
}else if (str[0].equalsIgnoreCase("EXIT")){
//making sure that input is in the right structure
if( str.length == 1 ){
toClient.writeBytes("~SESSION WILL CLOSE" + + '\n');
break;
}
else if (str.length == 2){
toClient.writeBytes("~" + str[1] + + '\n');
// toClient.writeBytes("SERVEROFF");
break;
}else
toClient.writeBytes("bad input . Try again \n");
}
// This case happens when none of the inputs are correct
else{
toClient.writeBytes("bad input . Try again \n");
}
// return bad input
}
//While ends
System.out.println("server exiting\n");
conn.close(); //closing the connection
svc.close(); //stop listening
}
}