-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.java
More file actions
62 lines (59 loc) · 1.97 KB
/
Server.java
File metadata and controls
62 lines (59 loc) · 1.97 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
import java.io.*;
import java.net.*;
class Server
{
public static BufferedReader in;
public static BufferedWriter out;
public static void main(String[] args)
{
if(args.length > 0 && args[0].equals("start"))
{
int SERVER_SOCKET = Integer.parseInt(args[1]);
try
{
ServerSocket server = new ServerSocket(SERVER_SOCKET);
System.out.println("Server started successfully at port number: " + SERVER_SOCKET);
Socket client = server.accept();
out = new BufferedWriter(new PrintWriter(client.getOutputStream(),true));
in = new BufferedReader(new InputStreamReader(client.getInputStream()));
String command = in.readLine();
if(command.equals("upload"))
handleUpload();
else if(command.equals("download"))
handleDownload();
in.close();
server.close();
}
catch(Exception e)
{
e.printStackTrace(System.out);
}
}
else
{
System.out.println("Invalid command or port number. Please make sure your all the letters are in lower case");
}
}
public static void handleUpload() throws IOException
{
File output = new File(in.readLine());
FileOutputStream fos = new FileOutputStream(output);
int c;
while((c = in.read())!= -1)
fos.write(((char)c));
fos.close();
}
public static void handleDownload() throws IOException
{
File input = new File(in.readLine());
FileInputStream fis = new FileInputStream(input);
int c;
while((c = fis.read()) != -1)
{
System.out.print((char)c);
out.write((char)c);
}
out.flush();
fis.close();
}
}