qua
qua

Reputation: 53

sending file over tcp between android and pc

I want to establish a communication between 2 android devices. with a server to client (through a server) methodology. So generally I was thinking sending the file to a server (pc) then fetch the file on server and send to the other device. So I was working the second part, trying to send a file from pc to android. but for some reason the client cannot connecting to server. here is my code;

public class TCPServer extends Thread {

public static final int SERVERPORT = 8901;

public static void main() {

    try {
        System.out.println("S: Connecting...");

        ServerSocket serverSocket = new ServerSocket(SERVERPORT);
        System.out.println("S: Socket Established...");

        Socket client = serverSocket.accept();
        System.out.println("S: Receiving...");

        ObjectOutputStream put = new ObjectOutputStream(
                client.getOutputStream());

        String s = "adios.wav";
        String str = "C:/";
        String path = str + s;
        System.out.println("The requested file is path: " + path);
        System.out.println("The requested file is : " + s);
        File f = new File(path);
        if (f.isFile()) {
            FileInputStream fis = new FileInputStream(f);

            byte[] buf = new byte[1024];
            int read;
            while ((read = fis.read(buf, 0, 1024)) != -1) {
                put.write(buf, 0, read);
                put.flush();
            }

            System.out.println("File transfered");
            client.close();
            serverSocket.close();
            fis.close();
        }

    } catch (Exception e) {
        System.out.println("S: Error");
        e.printStackTrace();
    } finally {

    }
}
}

And the client;

public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Socket s = null;
    BufferedInputStream get = null;

    try {
        s = new Socket("192.168.198.1", 8901);
        get = new BufferedInputStream(s.getInputStream());

        int u;

        String str = "/mnt/sdcard/ad.wav";
        FileOutputStream fs = new FileOutputStream(new File(str));
        byte jj[] = new byte[1024];
        while ((u = get.read(jj, 0, 1024)) != -1) {
            fs.write(jj, 0, u);
        }
        fs.close();
        System.out.println("File received");
        s.close();
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(0);
    }
}
}

Upvotes: 1

Views: 6805

Answers (1)

ja_mesa
ja_mesa

Reputation: 1969

I don't think the client part will work.

You should do the socket stuff in another thread or in AsyncTask.

For example:

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ...
        new doitAsync().execute();
    }

   class doitAsync extends AsyncTask<Void, Integer, Integer>
   {

       @Override
       protected void onPostExecute(Integer result)
       {
           if (result == -1)
           {
               System.exit(0);
           }
       }

       @Override
       protected Integer doInBackground(Void... params)
       {
           Socket s = null;
           BufferedInputStream get = null;

           try {
               s = new Socket("192.168.198.1", 8901);
               get = new BufferedInputStream(s.getInputStream());

               int u;

               String str = "/mnt/sdcard/ad.wav";
               FileOutputStream fs = new FileOutputStream(new File(str));
               byte jj[] = new byte[1024];
               while ((u = get.read(jj, 0, 1024)) != -1) {
                   fs.write(jj, 0, u);
               }
               fs.close();
               System.out.println("File received");
               s.close();
           } catch (Exception e) {
               e.printStackTrace();
               return -1; // failed
           }
           return 0;
       }

Also, you may consider the

onProgressUpdate() 

of the asynctask if you want to update a progress bar so the application won't seem frozen.

Upvotes: 2

Related Questions