Transfer Ikon Betwixt Android Devices, Via Socket

Posted on

The post “File transfer via Socket, betwixt Android devices” exhibit how to ship text file betwixt Android devices using Socket. In this example, nosotros are going to ship ikon file of “png” via Socket, with ObjectOutputStream inwards server side and ObjectInputStream inwards customer side.

The png file is fixed “android-er_sketch_1000.png” at ExternalStorageDirectory inwards server, as well as received file inwards customer volition survive salvage as “test.png” at ExternalStorageDirectory.

android-er_sketch_1000.png

Here I listing the MainActivity of both server as well as client. For the layout files, refer to the instance of “File transfer via Socket, betwixt Android devices“.

Server side MainActivity.java

package com.example.androidsocketfiletransferserver;  import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.ObjectOutputStream; import java.net.InetAddress; import java.net.NetworkInterface; import java.net.ServerSocket; import java.net.Socket; import java.net.SocketException; import java.util.Enumeration;  import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.os.Environment; import android.widget.TextView; import android.widget.Toast;  /*  * Permission needed:  * <uses-permission android:name="android.permission.INTERNET"/>  * <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>  */  world degree MainActivity extends ActionBarActivity {   TextView infoIp, infoPort;   static concluding int SocketServerPORT = 8080;  ServerSocket serverSocket;    ServerSocketThread serverSocketThread;    @Override  protected void onCreate(Bundle savedInstanceState) {   super.onCreate(savedInstanceState);   setContentView(R.layout.activity_main);   infoIp = (TextView) findViewById(R.id.infoip);   infoPort = (TextView) findViewById(R.id.infoport);    infoIp.setText(getIpAddress());      serverSocketThread = novel ServerSocketThread();   serverSocketThread.start();  }   @Override  protected void onDestroy() {   super.onDestroy();      if (serverSocket != null) {    endeavor {     serverSocket.close();    } grab (IOException e) {     // TODO Auto-generated grab block     e.printStackTrace();    }   }  }   someone String getIpAddress() {   String ip = "";   endeavor {    Enumeration<NetworkInterface> enumNetworkInterfaces = NetworkInterface      .getNetworkInterfaces();    spell (enumNetworkInterfaces.hasMoreElements()) {     NetworkInterface networkInterface = enumNetworkInterfaces       .nextElement();     Enumeration<InetAddress> enumInetAddress = networkInterface       .getInetAddresses();     spell (enumInetAddress.hasMoreElements()) {      InetAddress inetAddress = enumInetAddress.nextElement();       if (inetAddress.isSiteLocalAddress()) {       ip += "SiteLocalAddress: "         + inetAddress.getHostAddress() + "\n";      }      }     }    } grab (SocketException e) {    // TODO Auto-generated grab block    e.printStackTrace();    ip += "Something Wrong! " + e.toString() + "\n";   }    provide ip;  }    world degree ServerSocketThread extends Thread {    @Override   world void run() {    Socket socket = null;        endeavor {     serverSocket = novel ServerSocket(SocketServerPORT);     MainActivity.this.runOnUiThread(new Runnable() {       @Override      world void run() {       infoPort.setText("I'm waiting here: "         + serverSocket.getLocalPort());      }});          spell (true) {      socket = serverSocket.accept();      FileTxThread fileTxThread = novel FileTxThread(socket);      fileTxThread.start();     }    } grab (IOException e) {     // TODO Auto-generated grab block     e.printStackTrace();    } in conclusion {     if (socket != null) {      endeavor {       socket.close();      } grab (IOException e) {       // TODO Auto-generated grab block       e.printStackTrace();      }     }    }   }   }    world degree FileTxThread extends Thread {   Socket socket;      FileTxThread(Socket socket){    this.socket= socket;   }    @Override   world void run() {    File file = novel File(      Environment.getExternalStorageDirectory(),       "android-er_sketch_1000.png");        byte[] bytes = novel byte[(int) file.length()];    BufferedInputStream bis;    endeavor {     bis = novel BufferedInputStream(new FileInputStream(file));     bis.read(bytes, 0, bytes.length);          ObjectOutputStream oos = novel ObjectOutputStream(socket.getOutputStream());      oos.writeObject(bytes);     oos.flush();          socket.close();          concluding String sentMsg = "File sent to: " + socket.getInetAddress();     MainActivity.this.runOnUiThread(new Runnable() {       @Override      world void run() {       Toast.makeText(MainActivity.this,          sentMsg,          Toast.LENGTH_LONG).show();      }});         } grab (FileNotFoundException e) {     // TODO Auto-generated grab block     e.printStackTrace();    } grab (IOException e) {     // TODO Auto-generated grab block     e.printStackTrace();    } in conclusion {     endeavor {      socket.close();     } grab (IOException e) {      // TODO Auto-generated grab block      e.printStackTrace();     }    }       }  } } 

 exhibit how to ship text file betwixt Android devices using Socket Transfer ikon betwixt Android devices, via SocketDownload the files inwards Server side.

Client side MainActivity.java

package com.example.androidsocketfiletransferclient;  import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.net.Socket; import android.support.v7.app.ActionBarActivity; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; import android.os.Bundle; import android.os.Environment;  /*  * Permission needed:  * <uses-permission android:name="android.permission.INTERNET"/>  * <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>  */  world degree MainActivity extends ActionBarActivity {   EditText editTextAddress;  Button buttonConnect;  TextView textPort;   static concluding int SocketServerPORT = 8080;   @Override  protected void onCreate(Bundle savedInstanceState) {   super.onCreate(savedInstanceState);   setContentView(R.layout.activity_main);    editTextAddress = (EditText) findViewById(R.id.address);   textPort = (TextView) findViewById(R.id.port);   textPort.setText("port: " + SocketServerPORT);   buttonConnect = (Button) findViewById(R.id.connect);      buttonConnect.setOnClickListener(new OnClickListener(){     @Override    world void onClick(View v) {     ClientRxThread clientRxThread =       novel ClientRxThread(       editTextAddress.getText().toString(),        SocketServerPORT);          clientRxThread.start();    }});  }   someone degree ClientRxThread extends Thread {   String dstAddress;   int dstPort;    ClientRxThread(String address, int port) {    dstAddress = address;    dstPort = port;   }    @Override   world void run() {    Socket socket = null;        endeavor {     socket = novel Socket(dstAddress, dstPort);          File file = novel File(       Environment.getExternalStorageDirectory(),        "test.png");      ObjectInputStream ois = novel ObjectInputStream(socket.getInputStream());     byte[] bytes;     FileOutputStream fos = null;     endeavor {      bytes = (byte[])ois.readObject();      fos = novel FileOutputStream(file);      fos.write(bytes);     } grab (ClassNotFoundException e) {      // TODO Auto-generated grab block      e.printStackTrace();     } in conclusion {      if(fos!=null){       fos.close();      }           }         socket.close();                MainActivity.this.runOnUiThread(new Runnable() {       @Override      world void run() {       Toast.makeText(MainActivity.this,          "Finished",          Toast.LENGTH_LONG).show();      }});         } grab (IOException e) {      e.printStackTrace();          concluding String eMsg = "Something wrong: " + e.getMessage();     MainActivity.this.runOnUiThread(new Runnable() {       @Override      world void run() {       Toast.makeText(MainActivity.this,          eMsg,          Toast.LENGTH_LONG).show();      }});         } in conclusion {     if(socket != null){      endeavor {       socket.close();      } grab (IOException e) {       // TODO Auto-generated grab block       e.printStackTrace();      }     }    }   }  }  } 

 exhibit how to ship text file betwixt Android devices using Socket Transfer ikon betwixt Android devices, via SocketDownload the files inwards customer side.


More instance of Android Network programming
Bi-directional communication betwixt Client as well as Server, using ServerSocket, Socket, DataInputStream as well as DataOutputStream