Ok, I'm trying to write a small demo app for showing how to talk to an ftp server. However, I'm running into issues. I'm fairly new to the BB api's. This demo is derived from the socket demo. When I establish the connection, I get a valid response. However, the second time through the Exchange routine, the app bombs. I posted this on the Blackberry (RIM) site as well but haven't received any replies. From what I've seen in other posts, there are some issues with the socket connection api. Is this true? Any ideas? Once I get this working then I'll repost the sample here.
Here's my current source:
Code:
/*
* ftpTest.java
* Adapted from the rim socketdemo
* Demonstrates how to connect to a ftp site
*/
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.system.*;
import net.rim.device.api.i18n.*;
import java.io.*;
import javax.microedition.io.*;
// ftpTest.java Class --------------------------------------------------------------------------
public class ftpTest extends net.rim.device.api.ui.UiApplication implements FieldChangeListener
{
private LabelField appTitle; //application title label
private ButtonField connectButton; //define the buttons
private static String host = "socket://www.yourftpserver.com:21"; //put your ftp server name here
//private static String url;
private boolean isRunning;
private StringBuffer message = new StringBuffer();
private RichTextField rtf = new RichTextField();
public static void main(String[] args) {
ftpTest TIinstance = new ftpTest(); //create an instance of this class app
TIinstance.enterEventDispatcher(); //define a handle for the event processor
}
private ftpTest() {
MainScreen ftpScreen = new MainScreen(); //create screen instance
appTitle = new LabelField("FTP Tester"); //define app title
connectButton = new ButtonField("Connect"); //define button titles
connectButton.setChangeListener(this); //define the button handles for the event processor
ftpScreen.setTitle(appTitle); //post the screen title
ftpScreen.add(connectButton); //post the buttons on the screen
ftpScreen.add(rtf); //post the rich text field on the screen
pushScreen(ftpScreen); //post the screen itself
}
public void fieldChanged(Field field, int context) { //respond to button events
if (field == connectButton) { //if Connect button selected
synchronized(getLockObject()) {
if (!isRunning) {
isRunning = true;
new ConnectThread().start();
}
}
}
}
private class ConnectThread extends Thread {
private InputStreamReader inStream;
private OutputStreamWriter outStream;
private String rVal; //ftp result
private int rLen; //length of ftp result
private String rCode; //ftp reply code
private boolean bWereIn; //true if login passed
private boolean inProcess; //true while processing ftp session
private String CRLF = String.valueOf(Short.parseShort("0d",16) + Short.parseShort("0a",16));
private int eCode;
private void exchange(String data) throws IOException {
int length = data.length(); //cache locally for better performance
int rlength = 255; //ftp reply
char[] input = new char[rlength]; //input array
rLen = 0; //reinit
outStream.write(data, 0, length); //init the data array
if (inStream.ready()) {
for (int i = 0; i<rlength; ++i) { //loop through the array
input[i] = (char)inStream.read(); //read a char into the array
if (input[i] == Short.parseShort("0d", 16)) { //is CR?
rLen = i; //get length of result
if (rLen > 0) {
inProcess = true;
} else {
inProcess = false;
}
break;
}
}
}
StringBuffer s = new StringBuffer(); //build string buffer for display
s.append("Received: ");
s.append(input, 0, rlength);
updateDisplay(s.toString());
rVal = String.valueOf(input).substring(0,rLen);
}
public void run() {
StreamConnection connection = null;
try {
updateDisplay("Starting...");
connection = (StreamConnection)Connector.open(host); //establish connection
updateDisplay("Connected.");
inStream = new InputStreamReader(connection.openInputStream()); //init inbound stream
outStream = new OutputStreamWriter(connection.openOutputStream()); //init outbound stream
char[] input = new char[1024];
exchange("STAT");
while (inProcess) {
rCode = rVal.substring(0,3); //get first 3 chars for the ftp reply code
switch(Integer.parseInt(rCode)) {
case 220: { //ready for new user
exchange("USER userid"); //send user id
break;
}
case 331: { //user id okay, ready for password
exchange("PASS password"); //send password
break;
}
case 230: { //user logged in, proceed
exchange("QUIT"); //quit the session
inProcess = false; //end the ftp process
break;
}
}
}
inStream.close();
outStream.close();
connection.close();
updateDisplay("Completed");
} catch (IOException e) {
System.err.println(e.toString());
updateDisplay(e.toString());
}
synchronized(getLockObject()) {
isRunning = false;
}
}
}
protected void onExit() {}
private void updateDisplay(final String msg) {
invokeLater(new Runnable() {
public void run() {
message.append(msg);
message.append('\n');
rtf.setText(message.toString());
}
});
}
private Object getLockObject() {
return this;
}
}