-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer_library.java
More file actions
77 lines (66 loc) · 1.78 KB
/
Server_library.java
File metadata and controls
77 lines (66 loc) · 1.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
/**
* This is a TCP server program that receives the name of a file from the client.
* It then reads the contents of
* the file as a string and sends the string back to the file.
*/
import java.net.*;
import java.util.Scanner;
import java.io.*;
public class Server_library {
Socket sock;
InputStream recvStream;
OutputStream sendStream;
String request;
String response;
public Server_library(Socket s) throws IOException, UnknownHostException {
sock = s;
recvStream = sock.getInputStream();
sendStream = sock.getOutputStream();
}
void getRequest() {
try {
int dataSize;
while ((dataSize = recvStream.available()) == 0);
byte[] recvBuff = new byte[dataSize];
recvStream.read(recvBuff, 0, dataSize);
request = new String(recvBuff, 0, dataSize);
} catch (IOException ex) {
System.err.println("IOException in getRequest");
}
}
void process() {
String line;
try {
File openFile = new File(request);
File f = new File(request);
if (f.isFile()) {
System.out.println("filename ... " + request + " exits");
Scanner inFile = new Scanner(openFile);
while (inFile.hasNext()) {
line = inFile.nextLine();
line = line + "\n";
byte[] sendBuff = line.getBytes();
sendStream.write(sendBuff, 0, sendBuff.length);
sendStream.flush();
}
inFile.close();
} else {
System.out.println("filename ... " + request + " does NOT exit");
byte[] sendBuff = new byte[100];
sendBuff = "File Does Not Exist\n".getBytes();
sendStream.write(sendBuff, 0, sendBuff.length); // send no file
// line
}
} catch (Exception e) {
}
}
void close() {
try {
recvStream.close();
sendStream.close();
sock.close();
} catch (IOException ex) {
System.err.println("IOException in close");
}
}
}