Well, I am trying to implement the ftp server and ftp client in Java. I am trying to receive a file from server. Following is line of codes. I am able to achieve Connection between server and client, but unable to send filename to server also. Well can anyone guide me whether this approach is correct or if not, please suggest proper changes.
Server's Implementation:
import java.net.*;
import java.io.*;
class MyServer {
ServerSocket ss;
Socket clientsocket;
BufferedReader fromclient;
InputStreamReader isr;
PrintWriter toclient;
public MyServer() {
String str = new String("hello");
try {
// Create ServerSocket object.
ss = new ServerSocket(1244);
System.out.println("Server Started...");
while(true) {
System.out.println("Waiting for the request...");
// accept the client request.
clientsocket = ss.accept();
System.out.println("Got a client");
System.out.println("Client Address " + clientsocket.getInetAddress().toString());
isr = new InputStreamReader(clientsocket.getInputStream());
fromclient = new BufferedReader(isr);
toclient = new PrintWriter(clientsocket.getOutputStream());
String strfile;
String stringdata;
boolean file_still_present = false;
strfile = fromclient.readLine();
System.out.println(strfile);
//toclient.println("File name received at Server is " + strfile);
File samplefile = new File(strfile);
FileInputStream fileinputstream = new FileInputStream(samplefile);
// now ready to send data from server .....
int notendcharacter;
do {
notendcharacter = fileinputstream.read();
stringdata = String.valueOf(notendcharacter);
toclient.println(stringdata);
if (notendcharacter != -1) {
file_still_present = true;
} else {
file_still_present = false;
}
} while(file_still_present);
fileinputstream.close();
System.out.println("File has been send successfully .. message print from server");
if (str.equals("bye")) {
break;
}
fromclient.close();
toclient.close();
clientsocket.close();
}
} catch(Exception ex) {
System.out.println("Error in the code : " + ex.toString());
}
}
public static void main(String arg[]) {
MyServer serverobj = new MyServer();
}
}
Client's Implementation:
import java.net.*;
import java.io.*;
class MyClient {
Socket soc;
BufferedReader fromkeyboard, fromserver;
PrintWriter toserver;
InputStreamReader isr;
public MyClient() {
String str;
try {
// server is listening on this port.
soc = new Socket("localhost", 1244);
fromkeyboard = new BufferedReader(new InputStreamReader(System.in));
fromserver = new BufferedReader(new InputStreamReader(soc.getInputStream()));
System.out.println("PLEASE ENTER THE MESSAGE TO BE SENT TO THE SERVER");
str = fromkeyboard.readLine();
System.out.println(str);
String ddd;
ddd = str;
toserver = new PrintWriter(soc.getOutputStream());
String strfile;
int notendcharacter;
boolean file_validity = false;
System.out.println("send to server" + str);
System.out.println("Enter the filename to be received from server");
strfile = fromkeyboard.readLine();
toserver.println(strfile);
File samplefile = new File(strfile);
//File OutputStream helps to get write the data from the file ....
FileOutputStream fileOutputStream = new FileOutputStream(samplefile);
// now ready to get the data from server ....
do {
str = fromserver.readLine();
notendcharacter = Integer.parseInt(str);
if (notendcharacter != -1) {
file_validity = true;
} else {
System.out.println("Read and Stored all the Data Bytes from the file ..." +
"Received File Successfully");
}
if (file_validity) {
fileOutputStream.write(notendcharacter);
}
} while(file_validity);
fileOutputStream.close();
toserver.close();
fromserver.close();
soc.close();
} catch(Exception ex) {
System.out.println("Error in the code : " + ex.toString());
}
}
public static void main(String str[]) {
MyClient clientobj = new MyClient();
}
}
The answer to the above question is :
FTP Client :
FTP Server :
>
Server side
Client side: