-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUser.java
More file actions
229 lines (221 loc) · 10.7 KB
/
User.java
File metadata and controls
229 lines (221 loc) · 10.7 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import java.awt.* ;
import java.io.* ;
import java.net.* ;
import java.nio.file.*;
import java.util.* ;
public class User {
public static String ip="127.0.0.1";
public static int port = 6661;
public static DatagramSocket clientSocUDP;
public static void main(String args[]) {
try
{
if(args.length==0)
{
System.out.println("No Username given\n");
System.exit(0);
}
Socket clientSoc;
DataInputStream din;
DataOutputStream dout;
String LoginName=args[0];
clientSocUDP = new DatagramSocket();
clientSoc = new Socket(ip,6666) ;
System.out.println("Connected to Server at localhost Port-6666(TCP)");
din = new DataInputStream(clientSoc.getInputStream());
dout = new DataOutputStream(clientSoc.getOutputStream());
String a="HELLO SERVER!!";
byte[] file_contents = new byte[1000];
file_contents = a.getBytes();
DatagramPacket initial = new DatagramPacket(file_contents,file_contents.length,InetAddress.getByName(ip),port); /*For sending a packet via UDP, we should know 4 things, the message to send, its length, ipaddress of destination, port at which destination is listening.*/
//System.out.println(initial);
clientSocUDP.send(initial); //send for testing
dout.writeUTF(LoginName);
//Recieve messages
new Thread(new RecievedMessagesHandler (din,LoginName)).start();
//Send messages
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
String inputLine=null;
while(true)
{
try
{
inputLine=bufferedReader.readLine();
dout.writeUTF(inputLine);
if(inputLine.equals("LOGOUT"))
{
clientSoc.close();
din.close();
dout.close();
System.out.println("Logged Out");
System.exit(0);
}
StringTokenizer tokenedcommand = new StringTokenizer(inputLine);
//check file transfer
String comm,fl,typ;
comm = tokenedcommand.nextToken();
if(comm.equals("reply"))
{
boolean isFile=false;
if(tokenedcommand.hasMoreTokens())
{
fl=tokenedcommand.nextToken();
if(tokenedcommand.hasMoreTokens())
{
typ=tokenedcommand.nextToken();
//file transfer
if(typ.equals("tcp"))
{
isFile=true;
File file = new File(fl);
FileInputStream fpin = new FileInputStream(file);
BufferedInputStream bpin = new BufferedInputStream(fpin);
long fileLength = file.length(), current=0, start = System.nanoTime();
dout.writeUTF("LENGTH "+fileLength); //sending filelength to the server
int size = 1000; //sending file content in chunks of size 1000 bytes
while(current!=fileLength)
{
if(fileLength - current >= size) current+=size;
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");
}
fpin.close();
bpin.close();
System.out.println("TCP: Sent file");
}
else if(typ.equals("udp"))
{
int size=1024;
isFile=true;
File file = new File(fl);
FileInputStream fpin = new FileInputStream(file);
BufferedInputStream bpin = new BufferedInputStream(fpin);
long fileLength = file.length(), current =0, start =System.nanoTime();
dout.writeUTF("LENGTH "+fileLength);
while(current!=fileLength)
{
if(fileLength - current >= size) current+=size;
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("Sending file..."+(current*100/fileLength)+"% complete");
}
fpin.close();
bpin.close();
System.out.println("UDP: Sent file");
}
}
}
}
} catch(Exception e){
System.out.println(e);
break;
}
}
}
catch(Exception e) {
System.out.println(e);
System.exit(0);
}
}
}
class RecievedMessagesHandler implements Runnable {
private DataInputStream server;
private String LoginName;
public RecievedMessagesHandler(DataInputStream server,String LoginName) {
this.server = server;
this.LoginName = LoginName;
}
@Override
public void run() {
String inputLine=null;
while(true)
{
try {
inputLine=server.readUTF();
StringTokenizer st = new StringTokenizer(inputLine);
String message_type = st.nextToken();
if(message_type.equals("FILE"))
{
//File recieve
String fileName=st.nextToken();
String typ=st.nextToken();
st.nextToken();
int fileLength = Integer.parseInt(st.nextToken());
byte[] file_contents = new byte[1000];
Path base_dir = Paths.get(System.getProperty("user.dir"));
Path dir = Paths.get(base_dir.toString(), "temp",LoginName);
File dir_ = new File(dir.toString());
if(!dir_.exists()) dir_.mkdirs();
dir = Paths.get(dir_.toString(),fileName);
FileOutputStream fpout = new FileOutputStream(dir.toString());
//System.out.println(dir.toString());
//System.out.println(fileLength);
BufferedOutputStream bpout = new BufferedOutputStream(fpout);
DatagramPacket receivePacket;
if(typ.equals("TCP"))
{
int bytesRead=0,size=1000,current=0,total = fileLength;
if(size>fileLength)size=fileLength;
while((bytesRead=server.read(file_contents,0,size))!=-1 && fileLength>0)
{
bpout.write(file_contents,0,size);
if(total - current >= size) current+=size;
else current=total;
System.out.println("Recieving file..."+(current*100/total)+"% complete");
fileLength-=size;
if(size>fileLength) size=fileLength;
//file_contents = new byte[1000];
}
bpout.flush();
fpout.close();
bpout.close();
System.out.println("TCP: Recieved file");
}
else
{
int size=1024,current=0,total = fileLength;
file_contents = new byte[size];
if(size>fileLength) size=fileLength;
System.out.println("DEBUG: UDP FILELENGTH ==> "+fileLength);
while(fileLength>0)
{
receivePacket = new DatagramPacket(file_contents, size);
System.out.println("DEBUG: UDP start"); //start
User.clientSocUDP.receive(receivePacket);
System.out.println("DEBUG: UDP received"); //recieved chunk
bpout.write(file_contents,0,size);
System.out.println("DEBUG: UDP write"); //write chunk
if(total - current >= size) current+=size;
else current=total;
System.out.println("Recieving file..."+(current*100/total)+"% complete");
fileLength-=size;
if(size>fileLength) size=fileLength;
}
bpout.flush();
fpout.close();
bpout.close();
System.out.println("UDP: Recieved file");
}
}
else
System.out.println(inputLine);
}
catch(Exception e){
e.printStackTrace(System.out);
break;
}
}
}
}