top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration

Facebook Login
Site Registration

Socket send file from android to server and vice versa

+3 votes
57 views

I want to send a file (video) from android to server and vice versa. And I want the setup to work for N number of devices (more than 1). I assume we can use socket, yet I am not sure if using Socket for this requirement is the best way. Thanks in advance.

posted Jul 27, 2016 by Akshay

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote
 
Best answer

Sending a file in Socket is same as sending any string or integer data. The only difference is that you will send the bytes of the file instead of string/int. To do so,

File file = new File (FILE_TO_SEND); // get your file here
byte[] bytes  = new byte [(int)file.length()]; // create byte array
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
bis.read(bytes,0,bytes.length); // Read bytes from file.
OutputStream os = socket.getOutputStream(); // Get output stream from socket
os.write(bytes,0,bytes.length);

Note: this will work only if the file.length is within integer range. If the file is huge, then you have to repeat the process with new set of bytes until the full file is uploaded.

Hope this helps!

answer Aug 7, 2016 by Vinod Kumar K V
Similar Questions
+5 votes

How the scenario will be handled in case both client as well as server sends CER towards each other ?

+1 vote

I working on a project which needs adding a service with JNI to the system server and also a new daemon in init.rc script. I have successfully done this.
I might need the system service to periodically send SIGUSR1/2 signals to the daemon. Linux requires a sender of signals to be root. Do I have this privilege inside system server in Android?

+1 vote

Let's say I want to compare two csv files: file A and file B. They are both similarly built - the first column has product IDs (one product per row) and the columns provide some stats about the products such as sales in # and $.

I want to compare these files - see which product IDs appear in the first column of file A and not in B, and which in B and not A.
Finally, it would be very great if the result could be written into two new CSV files - one product ID per row in the first column. (no other data in the other columns needed)

This is the script I tried:

import csv

#open CSV's and read first column with product IDs into variables pointing to lists
A = [line.split(',')[0] for line in open('Afile.csv')]
B = [line.split(',')[0] for line in open('Bfile.csv')]

#create variables pointing to lists with unique product IDs in A and B respectively 
inAnotB = list(set(A)-set(B))
inBnotA = list(set(B)-set(A))

print inAnotB
print inBnotA

c = csv.writer(open("inAnotB.csv", "wb"))
c.writerow([inAnotB])

d = csv.writer(open("inBnotA.csv", "wb"))
d.writerow([inBnotA])

print "done!" 

But it doesn't produce the required results.
It prints IDs in this format:

247158132n

and nothing to the csv files.

Contact Us
+91 9880187415
sales@queryhome.net
support@queryhome.net
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Bangalore-560102
Karnataka INDIA.
QUERY HOME
...