- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.java
More file actions
Latest commit
98 lines (98 loc) · 2.22 KB
/
Copy pathclient.java
File metadata and controls
98 lines (98 loc) · 2.22 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
importjava.io.BufferedReader;
importjava.io.FileOutputStream;
importjava.io.InputStreamReader;
importjava.io.PrintWriter;
importjava.net.*;
importjava.io.DataOutputStream;
importjava.io.FileInputStream;
importjava.io.File;
importjava.nio.file.*;
importjava.io.DataInputStream;
publicclassclientimplementsRunnable
{
BufferedReaderbr1, br2;
PrintWriterpr1;
Socketsocket;
Threadt1, t2;
Stringin = "", out = "";
publicclient()
{
try
{
t1 = newThread(this);
t2 = newThread(this);
socket = newSocket("localhost", 5000);
t1.start();;
t2.start();
}
catch (Exceptione)
{
}
}
publicvoidrun()
{
try
{
if (Thread.currentThread() == t2)
{
while(true)
{
br1 = newBufferedReader(newInputStreamReader(System.in));
pr1 = newPrintWriter(socket.getOutputStream(), true);
in = br1.readLine();
if (in.contains("files"))
{
String[] splited = in.split("\\s+");
byte[] allBytes = Files.readAllBytes(Paths.get(splited[1]));
Filefile = newFile(splited[1]);
pr1.println(in+" "+file.length());
DataOutputStreamdos = newDataOutputStream(socket.getOutputStream());
dos.write(allBytes);
}
else
{
pr1.println(in);
if (in.equals("END") || out.equals("END"))
socket.close();
}
}
}
else
{
while(true)
{
br2 = newBufferedReader(newInputStreamReader(socket.getInputStream()));
out = br2.readLine();
if (out.contains("files"))
{
String[] splited = out.split("\\s+");
FileOutputStreamfos = newFileOutputStream("recfrmserver"+splited[1]);
byte[] b = newbyte[4096];
intremaining = Integer.parseInt(splited[2]);
intread=0;
DataInputStreamdis = newDataInputStream(socket.getInputStream());
while((read = dis.read(b, 0, Math.min(b.length, remaining))) > 0)
{
remaining-=read;
fos.write( b, 0 , read);
}
}
else
{
//System.out.println("down");
System.out.println("Server says : : : " + out);
if (in.equals("END")|| out.equals("END"))
socket.close();
}
}
}
}
catch (Exceptione)
{
}
}
publicstaticvoidmain(String[] args)
{
newclient();
}
}