• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

FileOutputStream code is not executing in Socket server program

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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){

}
}


}
 
Author
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:


 
Asif Kadiwala
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
My cellmate was this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic