-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIO.java
More file actions
117 lines (110 loc) · 4.55 KB
/
IO.java
File metadata and controls
117 lines (110 loc) · 4.55 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
import java.net.*;
import java.io.*;
import java.util.Scanner;
public class IO {
private void client() throws IOException {
Scanner sc = new Scanner(System.in);
boolean exe = true;
while (exe == true) {
System.out.println("Enter The Target(receiver) machine's address : ");
String ip = sc.nextLine();
Socket socket = new Socket(ip, 5055);
OutputStream out = socket.getOutputStream();
System.out.println("Enter the path of the file with fileName : example(C:/Path/to/File_Name.xyz)");
String path = sc.nextLine();
FileInputStream fis = new FileInputStream(path);
byte[] buffer = new byte[1024];
int bytesReturned;
while ((bytesReturned = fis.read(buffer)) != -1) {
out.write(buffer, 0, bytesReturned);
}
out.close();
System.out.println("File Sent Successfully..!!");
System.out.println(
"Do you want to send another file?\n1. Yes - Continue\n2. No - Exit\n3. Or Do You Want To Change Role - i.e from Sender to Receiver");
int option = sc.nextInt();
if (option == 2) {
exe = false;
} else if (option == 3) {
exe = false;
main(null);
} else if (option == 1) {
System.out.println("Ok");
} else {
socket.close();
fis.close();
exe = false;
sc.close();
}
}
}
private void server() throws IOException {
Scanner sc = new Scanner(System.in);
boolean exe = true;
ServerSocket server = new ServerSocket(5055);
System.out.println(
"Set the path where you want to save the received file : example(C:/Path/to/) or *just press enter to save in the current directory.");
String path = sc.nextLine();
while (exe == true) {
System.out.println(
"Give a name for the received file : example(fileName.xyz) *Be careful about the extension of file.");
String fileName = sc.nextLine();
System.out.println("\u001B[33m");
System.out.println("Waiting For Sender...");
System.out.println("\u001B[0m");
Socket s = server.accept();
InputStream in = s.getInputStream();
FileOutputStream out = new FileOutputStream(path + fileName);
byte[] buffer = new byte[1024];
int bytesReturned;
while ((bytesReturned = in.read(buffer)) != -1) {
out.write(buffer, 0, bytesReturned);
}
System.out.println("\u001B[32m");
System.out.println("File Saved Successfully At " + path + " As " + fileName);
System.out.println(
"Do you want to recieve another file?\n1. Yes - Continue\n2. No - Exit\n3. Or Do You Want To Change Role - i.e from Receiver to Sender");
int option = sc.nextInt();
if (option == 2) {
exe = false;
} else if (option == 3) {
exe = false;
main(null);
} else if (option == 1) {
System.out.println("Ok");
} else {
server.close();
out.close();
exe = false;
sc.close();
}
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Choose your role : \n1.Sender\n2.Receiver");
int opt = sc.nextInt();
IO role = new IO();
if (opt == 1) {
try {
role.client();
} catch (IOException e) {
System.out.print("\u001B[31m");
System.out.println(
e.getMessage() + " An Error Occurred While Processing Your Request, Please Try Again.");
}
} else if (opt == 2) {
try {
role.server();
} catch (IOException e) {
System.out.print("\u001B[31m");
System.out.println(
e.getMessage() + " An Error Occurred While Processing Your Request, Please Try Again.");
}
} else {
System.out.print("\u001B[31m");
System.out.println("Choose a valid option");
}
sc.close();
}
}