Hallo zusammen!
Ich versuche mit einer Socketverbindung Dateien zwischen einem PC und einem Roboter auszutauschen über ftp (port 21). Ein downlad funktioniert einwandfrei mit meiner nachfolgenden Klasse. Der Download allerdings nicht. Ich bekomme die Fehlermeldung "550 Requires PROGRAM password" vom Roboter zurück.
Interresant sind die Funktionen upload bzw. download und openPassiveMode.
Ich weiß da leider nicht mehr weiter. Das Problem würd ich schon gern mittels Socket lösen, also nicht mit FtpClient von der sun-Seite oder sowas. Würd mich freuen, wenn mir da einer weiterhelfen kann.
Ich versuche mit einer Socketverbindung Dateien zwischen einem PC und einem Roboter auszutauschen über ftp (port 21). Ein downlad funktioniert einwandfrei mit meiner nachfolgenden Klasse. Der Download allerdings nicht. Ich bekomme die Fehlermeldung "550 Requires PROGRAM password" vom Roboter zurück.
Interresant sind die Funktionen upload bzw. download und openPassiveMode.
Ich weiß da leider nicht mehr weiter. Das Problem würd ich schon gern mittels Socket lösen, also nicht mit FtpClient von der sun-Seite oder sowas. Würd mich freuen, wenn mir da einer weiterhelfen kann.
Code:
class FtpSession
{
private String aAdress;
private Socket aSocket = null; //socket for connection
private Socket transferSocket = null; //socket for data transfer
private DataOutputStream dataOut; //stream for ftp commands to robot
private DataInputStream dataIn; //stream for ftp answers from robot
private InputStream transIn; //instream for file transfer
private OutputStream transOut; //outstream for file transfer
private boolean debugMode = false; //messages in console on/off
private final int BLOCKSIZE = 1024;
private final int PORT = 21; //used ftp port is 21
private final int TIMEOUT = 10000; //connection time out after 10s
//* errorcode variable to show Errormessage Dialog */
private static int errorcode = 0;
//* constructor for progressbar */
private static ShowProgBar aProgressBar;
public static boolean isconnected = false;
//* connect to server */
public int connect(String aServer, String aUser, String aPass)
{
aAdress = aServer;
try
{
// //show connection status
// aProgressBar = new ShowProgBar();
// aProgressBar.start();
//connect to server and get streams for commands
aSocket = new Socket(aServer, PORT);
aSocket.setSoTimeout(TIMEOUT);
dataIn = new DataInputStream(aSocket.getInputStream());
dataOut = new DataOutputStream(aSocket.getOutputStream());
getResponse();
//interrupt progress bar, login and open passive mode if connected
isconnected = aSocket.isConnected();
if (isconnected == true)
{
// aProgressBar.interrupt();
sendCommand("USER " + aUser);
getResponse();
sendCommand("PASS " + aPass);
getResponse();
openPassiveMode();
}
}
catch (IOException e)
{
e.printStackTrace();
errorcode = 21;
// if (!isconnected) aProgressBar.interrupt();
}
return errorcode;
}
//* close connection to server */
public int disconnect()
{
try
{
if (aSocket != null)
{
dataIn.close();
dataOut.close();
aSocket.close();
aSocket = null;
}
if (transferSocket != null)
{
transIn.close();
transOut.close();
transferSocket.close();
transferSocket = null;
}
}
catch (IOException e)
{
e.printStackTrace();
if (errorcode == 0) errorcode = 22;
}
return errorcode;
}
//* show messages in console (on/off) */
public void showMessages(boolean b)
{
debugMode = b;
}
//* upload a file */
public int upload(String from, String to)
{
try
{
openPassiveMode();
sendCommand("TYPE I"); //binary mode
getResponse();
sendCommand("STOR " + to);
getResponse();
DataOutputStream out = new DataOutputStream(transOut);
RandomAccessFile infile = new RandomAccessFile(from, "r"); //r = open only for read access
byte buffer[] = new byte[BLOCKSIZE];
int length;
while ((length = infile.read(buffer, 0, BLOCKSIZE)) > 0)
{
out.write(buffer, 0, length);
}
infile.close();
getResponse();
}
catch (IOException e)
{
e.printStackTrace();
if (errorcode == 0) errorcode = 23;
}
return errorcode;
}
//* download a file */
public int download(String from, String to)
{
try
{
openPassiveMode();
sendCommand("TYPE I"); //binary mode
getResponse();
sendCommand("RETR " + from);
getResponse();
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(new File(to)));
byte buffer[] = new byte[BLOCKSIZE];
int length;
while ((length = transIn.read(buffer, 0, BLOCKSIZE)) > 0)
{
out.write(buffer, 0, length);
}
out.close();
}
catch (IOException e)
{
e.printStackTrace();
if (errorcode == 0) errorcode = 24;
}
return errorcode;
}
//* open passive mode */
private void openPassiveMode()
{
try
{
sendCommand("PASV");
String response = getResponse();
//get passive mode string, like (10,0,2,53,42,34)
int start = response.lastIndexOf('(');
int end = response.lastIndexOf(')');
String pasvString = response.substring(start+1, end);
StringTokenizer st = new StringTokenizer(pasvString, ",");
int ipAddrArr[] = new int[6];
for (int i = 0; st.hasMoreTokens(); i++)
{ipAddrArr[i] = Integer.valueOf(st.nextToken()).intValue();}
//create new port
int port = (ipAddrArr[4] << 8) + ipAddrArr[5]; //"<< 8" = multiply with 2^8
if (transferSocket != null)
{transferSocket.close();}
//open new connection with new port
transferSocket = new Socket(aAdress, port);
transIn = transferSocket.getInputStream();
transOut = transferSocket.getOutputStream();
}
catch (IOException e)
{
e.printStackTrace();
if (errorcode == 0) errorcode = 26;
}
}
//* send command to server */
private void sendCommand(String aCommand) throws IOException
{
if (debugMode) System.out.println("PC => ROBOT: " + aCommand);
dataOut.writeBytes(aCommand + "\n");
}
//* get response from server */
private String getResponse()
{
String response = "",
newLine = null;
try
{
do
{
newLine = dataIn.readLine();
response += newLine;
if (debugMode) System.out.println("PC <= ROBOT: " + newLine);
}
while(!(Character.isDigit(newLine.charAt(0)) &&
Character.isDigit(newLine.charAt(1)) &&
Character.isDigit(newLine.charAt(2)) &&
newLine.charAt(3) == ' ')
);
}
catch (IOException e)
{
e.printStackTrace();
return null;
}
return response;
}
}