forked from atmoharam/FTP-in-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFTP_Client.java
More file actions
116 lines (111 loc) · 7.22 KB
/
FTP_Client.java
File metadata and controls
116 lines (111 loc) · 7.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Scanner;
public class client {
public static void main ( String[] args ) throws IOException {
String Extension; // to store the name of the data that will be received from server
Scanner in = new Scanner ( System.in ); // declaring a scanner (input stream) from user
in.useDelimiter ( "\n" ); // make that scanner use Delimiter of new line
try ( Socket socket1 = new Socket ( InetAddress.getLocalHost ( ), 28000 ) ) { // first socket to pass the users' info
Socket socket2 = new Socket ( InetAddress.getLocalHost ( ), 25188 ); // second socket to receive the data from server
DataInputStream ServerReadSource = new DataInputStream ( socket1.getInputStream ( ) ); // the input-stream to read from server (receive from server)
DataOutputStream ServerWriteSource = new DataOutputStream ( socket1.getOutputStream ( ) ); // the output-stream to write to server (send-to server)
//recieving message from server, and printing it to the user
String EnterUserName = ServerReadSource.readUTF ( );
System.out.println ( EnterUserName );
// allowing the user to enter the username and sending it to the server
String UserName = in.next ( );
ServerWriteSource.writeUTF ( UserName );
// accepting the servers' respond
String Login_Checker = ServerReadSource.readUTF ( );
System.out.println ( Login_Checker );
// if the user entered wrong username, will enter this if-condition and program terminates
if ( Login_Checker.equals ( "Login Failed and the connection will terminate" ) ) {
socket1.close ( );
socket2.close ( );
ServerWriteSource.close ( );
ServerReadSource.close ( );
return;
}
// to take the password from user if a right username was entered, and sending it to the server
String Password = in.next ( );
ServerWriteSource.writeUTF ( Password );
// getting the server's response to the password
Login_Checker = ServerReadSource.readUTF ( );
// checker if the password is wrong will enter this condition and the program terminate
if ( Login_Checker.equals ( "Login Failed and the connection will terminate" ) ) {
socket1.close ( );
socket2.close ( );
ServerWriteSource.close ( );
ServerReadSource.close ( );
System.out.println ( Login_Checker );
return;
}
// if the user entered right username and password, the program enters this scope
else {
while ( true ) {
String StartAgain = ServerReadSource.readUTF ( ); // to take response from server
System.out.println ( StartAgain ); // to print the server's response
String choice_of_user_to_continue_or_not = in.next ( ); // to take the user's option to continue the program or exit
ServerWriteSource.writeUTF ( choice_of_user_to_continue_or_not ); // to send the users' choice to the server
// if the user entered (close), the program will enter this if-condition & terminate
if ( choice_of_user_to_continue_or_not.equals ( "close" ) ) {
socket1.close ( );
socket2.close ( );
ServerWriteSource.close ( );
ServerReadSource.close ( );
break;
}
int Num_Of_Files = ServerReadSource.readInt ( ); //to store no. of files
String msg = ServerReadSource.readUTF ( ); // to store the server's respond
System.out.println ( msg ); // to print the server's respond
// to print the folders in this directory
for ( int i = 0 ; i < Num_Of_Files ; i++ ) {
String Directory = ServerReadSource.readUTF ( );
System.out.println ( Directory );
}
String kind_Of_Direction = in.next ( ); // to store the user's chosen folder
ServerWriteSource.writeUTF ( kind_Of_Direction ); // to send the folder's name to the server
msg =ServerReadSource.readUTF (); // to receive the server's respond about this folder
// if the wanted folder doesn't exist, will enter this if-condition
if ( msg.equals ( "This directory is not found" ) )
{
System.out.println ( msg );
continue;
}
// if this folder exist, enters this scope
else {
Num_Of_Files = ServerReadSource.readInt ( ); // to receive the number of files in this folder
msg = ServerReadSource.readUTF ( ); // to receive a message from server
System.out.println ( msg ); // printing this message
// a loop to receive & print the existing files name in the folder
for ( int i = 0 ; i < Num_Of_Files ; i++ ) {
String Data = ServerReadSource.readUTF ( );
System.out.println ( Data );
}
String Kind_Of_Data = in.next ( ); // to take from user the wanted file (to be downloaded)
ServerWriteSource.writeUTF ( Kind_Of_Data ); //to send the wanted file to server
Extension = ServerReadSource.readUTF ( ); // to receive the server's respond
// if the wanted file doesn't exists in the folder, would enter the loop
if ( Extension.equals ( "File not exist" ) ) {
System.out.println ( Extension );
continue;
}
System.out.println ( ServerReadSource.readUTF ( ) ); // printing a received line from server
File DownloadedFile = new File ( "D:\\ftp2\\" + Extension ); // the path of the downloaded file
FileOutputStream FileWriter = new FileOutputStream ( DownloadedFile ); // to write to the folder that will contain the downloaded file
DataInputStream DataDownloaded = new DataInputStream ( socket2.getInputStream ( ) ); // receive the file that will be downloaded from the server
int length = DataDownloaded.readInt ( ); // the length of the file that will be downloaded
// If there is data in the file, send the required data to the folder of downloads
if ( length > 0 ) {
byte[] DataBytes = new byte[ length ];
DataDownloaded.readFully ( DataBytes, 0, DataBytes.length );
FileWriter.write ( DataBytes );
}
}
}
}
}
}
}