To send and receive file via socket over Wifi I have used following code...
Client side :
Socket socket = new Socket(dstAddress, dstPort);
int bufferSize=socket.getReceiveBufferSize();
InputStream in=socket.getInputStream();
DataInputStream clientData = new DataInputStream(in);
String fileName = clientData.readUTF();
System.out.println(fileName);
OutputStream output = new FileOutputStream("/sdcard/"+fileName);
byte[] buffer = new byte[bufferSize];
int read;
while((read = clientData.read(buffer)) != -1){
output.write(buffer, 0, read);
}
//close every thing
output.flush();
output.close();
socket.close();
Server side:
File file = new File(Environment.getExternalStorageDirectory(), "/sdcard/test.amr");
byte[] mybytearray = new byte[8092];
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
DataInputStream dis = new DataInputStream(bis);
OutputStream os;
try {
os = socket.getOutputStream();
DataOutputStream dos = new DataOutputStream(os);
dos.writeUTF(file.getName());
dos.writeLong(mybytearray.length);
int read;
while((read = dis.read(mybytearray)) != -1){
dos.write(mybytearray, 0, read);
}
os.flush();
os.close();
socket.close();
At this point I am receiving file 'test.amr' from server without change of its original size. But when I try to play the file in client device it can't be played.
Note : mp3, avi and txt file received using above code can be opened and played perfectly.
Please suggest how to solve this issue.