-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathServer.java
More file actions
169 lines (151 loc) · 4.76 KB
/
Server.java
File metadata and controls
169 lines (151 loc) · 4.76 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.JFileChooser;
public class Server {
private static int buffer = 16000000;
private static ServerSocket server_socket;
private static Socket socket;
private static BufferedOutputStream p_output;
private static BufferedInputStream input;
private static byte[] b = new byte[buffer];
private static int n;
/*
*
* ServerSocket class sets up a server on the local machine that listens on
* port number 69 and can support a backlog of 10 users.
*
*/
public static void main(String args[]) {
int port = 69;
int backlog = 10;
try {
server_socket = new ServerSocket(port, backlog);
try {
while(true) {
waitForConnection();
setupStreams();
transferData();
}
} catch(EOFException eofe) {
System.out.println("Error: " + eofe);
} finally {
closeCrap();
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
/*
*
* Waits till a client connects to the server then prints the IP address of
* the client.
*
* The accept() method loops till a connection is recieved and then returns
* a java.net.Socket object containing the info about the remote computer.
*
* The getInetAddress() method gets the IP address and the getHostName()
* converts it to String.
*
*/
private static void waitForConnection() throws IOException {
System.out.println("Waiting for connection...");
socket = server_socket.accept();
String host_name = socket.getInetAddress().getHostName();
System.out.println("Connected to " + host_name);
}
/*
*
* Sets up the output and input streams of the server.
* The output stream of the server is automatically connected
* to the input stream of the client and vice-versa.
*
* The socket.getOutputStream() and socket.getInputStream()
* methods return the respective stream objects for the
* local machine.
*
* The flush() method flushes the output stream thus clearing
* the output buffer in case there is some data left in the
* buffer. There is no flush method for the input stream.
*
*/
private static void setupStreams() throws IOException {
System.out.println("Output stream set\nSetting up input stream...");
input = new BufferedInputStream(socket.getInputStream(), buffer);
System.out.println("Input stream set");
System.out.println("Setting Directory");
String dir = getDirectory();
System.out.printf("Target directory is '%s'%n", dir);
System.out.println("Getting source file name");
String filename = readFilename();
System.out.printf("Source file name is '%s'%n", filename);
System.out.println("Setting output stream...");
p_output = new BufferedOutputStream(new FileOutputStream(dir + "/" + filename), buffer);
System.out.println("Output stream set");
}
/**
* Reads source file name from the inputstream. The start of the input stream is expected to be the UTF-8 encoded
* filename terminated by a null byte.
*
* This method reads from the input stream untill a null byte is reached and returns the result as a string.
*
* @return The source file name
* @throws IOException
*/
private static String readFilename() throws IOException {
int b;
ByteArrayOutputStream filenameBuffer = new ByteArrayOutputStream();
while ((b = input.read()) != 0) {
filenameBuffer.write(b);
}
return filenameBuffer.toString("UTF-8");
}
/*
*
* The read() method takes data from BufferedInputStream input and writes
* it to the byte array b from element 0 to 16000000-1. The number of bytes
* read are stored in n.
*
* The write() method writes data from b to BufferedOutputStream output
* from position 0 to the number of bytes written.
*
* The write() method writes only till n as writting the whole array
* creates problem during the last iteration when all array elements are
* not reinitialized and the last few elemnts contain leftover values from
* the last iteration.
*
*/
private static void transferData() throws IOException {
while((n=input.read(b, 0, buffer)) != -1) {
p_output.write(b, 0, n);
}
}
/*
*
* closeCrap() Closes all BufferedInputStream, BufferedOutputStream,
* ServerSocket and Socket after all the work is done.
*
*/
private static void closeCrap() throws IOException {
server_socket.close();
socket.close();
p_output.close();
input.close();
}
/*
*
* Use a JFileChooser to select the directory where the server wishes to
* save the files that will be sent by the client.
*
*/
private static String getDirectory() {
String file = "";
JFileChooser fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int ans = fc.showOpenDialog(null);
if(ans == JFileChooser.APPROVE_OPTION) {
file = fc.getSelectedFile().getAbsolutePath();
}
return file;
}
}