aspose file tools*
The moose likes Android and the fly likes FileOutputStream code is not executing in Socket server program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Mobile » Android
Bookmark "FileOutputStream code is not executing in Socket server program" Watch "FileOutputStream code is not executing in Socket server program" New topic
Author

FileOutputStream code is not executing in Socket server program

Asif Kadiwala
Greenhorn

Joined: Jun 10, 2008
Posts: 17
Hi all,

I am running android server application which is listening to port 4444 to retreive the file.I want to receive the file sent from Desktop socket client application. To perform this task I used following code as Android socket server. But execution stops infinitely at the

bos = new BufferedOutputStream(new FileOutputStream("123.txt")); line, Please help me to find out problem.


FTPServer.java is as follows,

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;

import android.util.Log;

public class FTPServer implements Runnable{


ServerSocket server;
Socket connection;

BufferedInputStream bis;
BufferedOutputStream bos;

byte[] receivedData;
int in;

public void run(){
try {
server = new ServerSocket(4444 );
Log.d("TCP..","FTP started...");
try {


while ( true ) {
connection = server.accept();


Log.w( "TCP","S:: receiving file... " );


receivedData = new byte[8192];
Log.d("hello:","databye done");

bis = new BufferedInputStream(connection.getInputStream());
Log.d("hello:","buffer input done");

bos = new BufferedOutputStream(new FileOutputStream("123.txt")); // this line creates the problem
Log.d("hello:","buffer output done");
while ((in = bis.read(receivedData)) != -1){
bos.write(receivedData,0,in);
}
bos.close();

Log.w("TCP","File Received..... " );
}
}
catch (IOException e ) { }

finally {


}
}
catch(Exception e){

}
}


}
Ed Burnette
Author
Ranch Hand

Joined: Jun 10, 2003
Posts: 142
You can try writing temporary files in /tmp/something or /sdcard/something, but I suspect you're really hanging on the network access. There's a nice example in Hello, Android that does network I/O; see the Translate program in http://www.pragprog.com/titles/eband/source_code and the description that goes with it in the book.

And one tip: never use empty catch/finally blocks. If you use something like this instead it will help you a lot when debugging problems:



Ed Burnette, Author of Hello Android
Blog: ZDNet's Dev Connection - Twitter: @eburnette
Asif Kadiwala
Greenhorn

Joined: Jun 10, 2008
Posts: 17
Hi Ed,

Thanks a lot for your help as well as your tip.

This example
also gives better understanding of network IO using socket communication. But this works fine if we send String messages but my requirement is to transfer files from desktop java application to android emulator. Still got same problem.

Please reply
Ed Burnette
Author
Ranch Hand

Joined: Jun 10, 2003
Posts: 142
Unless I'm misunderstanding your question, the same socket APIs can be used to transfer any data, not just strings.

For example there is at least one Android FTP client on the Market, so we know it's possible.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: FileOutputStream code is not executing in Socket server program