-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJavaFileClient.java
86 lines (71 loc) · 3.52 KB
/
JavaFileClient.java
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
importjava.io.BufferedReader;
importjava.io.DataInputStream;
importjava.io.DataOutputStream;
importjava.io.EOFException;
importjava.io.File;
importjava.io.FileOutputStream;
importjava.io.InputStreamReader;
importjava.net.Socket;
importjava.util.Scanner;
class Client
{
public static void main(String args[])throws Exception{
String address = "";
Scanner sc=new Scanner(System.in);
System.out.println (" ------------------------------------------------------------------------------------------------------------------------");
System.out.println (" FILE TRANSFER");
System.out.println (" ------------------------------------------------------------------------------------------------------------------------");
System.out.println (" This is the Client side of the program...");
System.out.println (" ________________________________________");
System.out.println(" |Enter Server Address: |");
System.out.print (" |");
address=sc.nextLine();
//create the socket on port 5000
Socket s=new Socket(address,5000);
DataInputStream din=new DataInputStream(s.getInputStream());
DataOutputStreamdout=new DataOutputStream(s.getOutputStream());
BufferedReaderbr=new BufferedReader(new InputStreamReader(System.in));
System.out.println (" |________________________________________|");
System.out.println(" |Enter start to receive the file: |");
System.out.print (" |");
String str="",filename="";
try
{
while(!str.equals("start"))
str=br.readLine();
dout.writeUTF(str);
dout.flush();
filename=din.readUTF();
System.out.println (" |________________________________________|");
System.out.println(" |Receving file from Server: "+filename);
System.out.println (" |________________________________________|");
filename="client"+filename;
System.out.println(" |Saving the sent file as: "+filename);
System.out.println (" |________________________________________|");
longsz=Long.parseLong(din.readUTF());
System.out.println (" |Size of file: "+sz+" MB");
System.out.println (" |________________________________________|");
byte b[]=new byte [1024];
System.out.println(" |Receving file from server.. |");
FileOutputStreamfos=new FileOutputStream(new File(filename),true);
longbytesRead;
do
{
bytesRead = din.read(b, 0, b.length);
fos.write(b,0,b.length);
}
while(!(bytesRead<1024));
System.out.println(" |File received...... |");
System.out.println (" |________________________________________|");
System.out.println(" |File transfer Comleted!!! |");
System.out.println (" |________________________________________|");
dout.close();
s.close();
}
catch(EOFException e)
{
//do nothing
}
}
}
//this is a typical client program