Note that there are some explanatory texts on larger screens.

plurals
  1. POsending file over tcp between android and pc
    primarykey
    data
    text
    <p>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;</p> <pre><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 { } } } </code></pre> <p>And the client;</p> <pre><code>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); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload