-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.java
More file actions
178 lines (174 loc) · 8.07 KB
/
client.java
File metadata and controls
178 lines (174 loc) · 8.07 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
170
171
172
173
174
175
176
177
178
import java.awt.* ;
import java.io.* ;
import java.net.* ;
import java.util.* ;
public class client {
public static String ip;
public static int port = 6661;
public static DatagramSocket clientSocUDP;
public static void main(String args[]) {
try {
if (args.length < 1) {
System.out.println("IP not provided. Running on 127.0.0.1");
ip = "127.0.0.1";
} else {
ip = args[0];
}
clientSocUDP = new DatagramSocket();
Socket clientSoc;
clientSoc = new Socket(ip,6666) ;
System.out.println("Connected to Server at localhost Port-6666(TCP)");
DataInputStream din;
din = new DataInputStream(clientSoc.getInputStream());
DataOutputStream dout;
dout = new DataOutputStream(clientSoc.getOutputStream());
String a = "hrlli";
byte[] file_contents = new byte[1000];
file_contents = a.getBytes();
DatagramPacket initial = new DatagramPacket(file_contents,file_contents.length,InetAddress.getByName(ip),port);
clientSocUDP.send(initial);
String LoginName;
//Send messages
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
String inputLine = null;
while(1==1) {
try {
inputLine = bufferedReader.readLine();
dout.writeUTF(inputLine);
if (inputLine.equals("LOGOUT")) {
dout.close();
din.close();
clientSoc.close();
System.out.println("Logged Out");
System.exit(0);
}
//check file transfer
String comm;
String fl;
StringTokenizer tokenedcommand = new StringTokenizer(inputLine);
comm = tokenedcommand.nextToken();
if (comm.equals("create_user")) {
if (tokenedcommand.hasMoreTokens()) {
fl = tokenedcommand.nextToken();
LoginName = fl;
new Thread(new RecievedMessagesHandler (din,LoginName)).start();
} else {
System.out.println("Error! No Username argument provided.");
}
} else if (comm.equals("upload")) {
fl = tokenedcommand.nextToken();
System.out.println(fl);
File file = new File(fl);
FileInputStream fpin = new FileInputStream(file);
BufferedInputStream bpin = new BufferedInputStream(fpin);
long fileLength = file.length();
long current=0;
dout.writeUTF("LENGTH "+fileLength);
try {
while(current != fileLength) {
int size=1000;
if (fileLength - current >= size) {
current = size + current;
} else {
size = (int)(fileLength-current);
current = fileLength;
}
file_contents = new byte[size];
bpin.read(file_contents, 0, size);
dout.write(file_contents);
System.out.println("Sending file ..."+(current*100/fileLength)+"% complete");
}
System.out.println("File Sent successfully");
} catch (Exception e) {
System.out.println(e);
System.out.println("File sending unsuccessfull");
}
} else if (comm.equals("upload_udp")) {
int size = 1024;
fl = tokenedcommand.nextToken();
File file = new File(fl);
FileInputStream fpin = new FileInputStream(file);
BufferedInputStream bpin = new BufferedInputStream(fpin);
long current = 0;
long fileLength = file.length();
dout.writeUTF("LENGTH " + fileLength);
try {
while(current != fileLength) {
if (fileLength - current >= size) {
current = size + current;
} else {
size = (int)(fileLength-current);
current = fileLength;
}
file_contents = new byte[size];
bpin.read(file_contents, 0, size);
DatagramPacket sendPacket = new DatagramPacket(file_contents, size, InetAddress.getByName(ip), port);
clientSocUDP.send(sendPacket);
System.out.println("File sending "+(current*100/fileLength)+"% complete");
}
System.out.println("File Sent");
} catch (Exception e) {
System.out.println(e);
System.out.println("File sending unsuccessfull");
}
}
} catch (Exception e) {
System.out.println(e);
break;
}
}
}
//catch(UnknownHostException e) {System.out.println("Cannot find Server"); System.exit(0);}
//catch(IOException e) {System.out.println(e);System.exit(0);}
catch (Exception e) {
System.out.println(e);
System.exit(0);
}
}
}
class RecievedMessagesHandler implements Runnable {
private String LoginName;
private DataInputStream server;
public RecievedMessagesHandler(DataInputStream server,String LoginName) {
this.server = server; this.LoginName = LoginName;
}
@Override
public void run() {
String inputLine=null;
while(1==1) {
try {
inputLine=server.readUTF();
StringTokenizer st = new StringTokenizer(inputLine);
if (st.nextToken().equals("FILE")) {
System.out.println(inputLine);
String fileName=st.nextToken();
st.nextToken();
int fileLength = Integer.parseInt(st.nextToken());
System.out.println("Recieving file " + fileName);
byte[] file_contents = new byte[1000];
FileOutputStream fpout = new FileOutputStream(fileName);
BufferedOutputStream bpout = new BufferedOutputStream(fpout);
int size=1000;
if(size > fileLength) {
size = fileLength;
}
int bytesRead;
while((bytesRead=server.read(file_contents,0,size))!=-1 && fileLength>0) {
bpout.write(file_contents,0,size);
fileLength-=size;
if(size > fileLength) {
size=fileLength;
}
}
bpout.flush();
System.out.println("File Recieved");
} else {
System.out.println(inputLine);
}
} catch(Exception e) {
e.printStackTrace(System.out);
break;
}
}
}
}