-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.java
More file actions
128 lines (110 loc) · 5.87 KB
/
Client.java
File metadata and controls
128 lines (110 loc) · 5.87 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
import java.io.*;
import java.net.*;
import java.util.*;
public class Client{
public static void main(String[] args) throws UnknownHostException, IOException{
String username = args[0];
String server_ip = args[1];
int server_port = Integer.parseInt(args[2]);
Socket s = new Socket(server_ip,server_port);
DataInputStream dis = new DataInputStream(s.getInputStream());
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
Scanner scn = new Scanner(System.in);
// Send username to the server
dos.writeUTF("username: " + username);
// send message thread
Thread sendMessage = new Thread(new Runnable(){
public void run(){
while (true){
// read the message to deliver.
String msgSend = scn.nextLine();
try {
// write on the output stream
dos.writeUTF(msgSend);
// --------------------------------Client exit condition -------------------------------//
if(msgSend.equals("exit")){
System.out.println("Exiting...");
s.close();
return;
}
String[] splitedMessage = msgSend.split("\\s+");
String protocol = splitedMessage[splitedMessage.length - 1];
//------------------------- Send file attributes and then transfer File ---------------------------//
if(protocol.equals("tcp") || protocol.equals("udp")){
String filename = splitedMessage[1];
String filepath = System.getProperty("user.dir") + "/" + filename;
File myFile = new File (filepath);
if(myFile.exists()){
String fileAttributes = "FileAttributes: " + Long.toString(myFile.length()) + " " + filename + " " +protocol;
dos.writeUTF(fileAttributes);
System.out.println("Sending "+filename+"...");
byte [] mybytearray = new byte [(int)myFile.length()];
FileInputStream fis = new FileInputStream(myFile);
BufferedInputStream bis = new BufferedInputStream(fis);
bis.read(mybytearray,0,mybytearray.length);
OutputStream os = s.getOutputStream();
os.write(mybytearray,0,mybytearray.length);
os.flush();
bis.close();
System.out.println("Sent file...");
}
else
System.out.println(filename + " does not exist in the required path...");
}
}
catch (IOException e) {
e.printStackTrace();
}
}
}
});
// readMessage thread
Thread readMessage = new Thread(new Runnable(){
public void run() {
while (true) {
try {
// read the message sent to this client
String msgRcv = dis.readUTF();
if(msgRcv.equals("No more clients can be accepted...")){
s.close();
break;
}
String[] splitedMessage = msgRcv.split("\\s+");
int filesize;
String filename,protocol,sender;
//-----------------------Recieve file attribuites and then the file ------------------------//
if(splitedMessage[0].equals("FileAttributes:")){
filesize = Integer.parseInt(splitedMessage[1]);
filename = splitedMessage[2];
protocol = splitedMessage[3];
sender = splitedMessage[4];
System.out.println("Recieving " + filename + " from " + sender + "...");
int current = 0, tillNow = 0;
String destFile = System.getProperty("user.dir") + "/" + filename;
byte [] mybytearray = new byte [6022386];
InputStream is = s.getInputStream();
FileOutputStream fos = new FileOutputStream(destFile);
BufferedOutputStream bos = new BufferedOutputStream(fos);
while ((current = is.read(mybytearray)) > 0) {
bos.write(mybytearray, 0, current);
tillNow += current;
if(tillNow >= filesize)
break;
}
bos.flush();
bos.close();
System.out.println("Recieved " + filename + " from " + sender + "...");
continue;
}
System.out.println("=> " + msgRcv);
}
catch (IOException e) {
e.printStackTrace();
}
}
}
});
sendMessage.start();
readMessage.start();
}
}