I'm trying to exchange files between two devices using android wifi direct. Any of the device can send and receive files. After discovering and connecting the two devices,I can only successfully send and receive a file/files upon the first time calling send. I'm unable to send any file afterwards. In addition,transfer speed is very slow(it takes about three minutes to transfer a 10mb file)
void startServer(){
String intRoot = new FilesDisplayFragment().internalDirRoot;
ExecutorService serverExecutor = newSingleThreadExecutor();
serverExecutor.submit(new Runnable() {
@Override
public void run() {
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(8888);
} catch (IOException e) {
e.printStackTrace();
}
try {
socket = serverSocket.accept();
} catch (IOException e) {
e.printStackTrace();
}
BufferedInputStream bis = null;
try {
bis = new BufferedInputStream(socket.getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
DataInputStream dis = new DataInputStream(bis);
int filesCount = 0;
try {
filesCount = dis.readInt();
} catch (IOException e) {
e.printStackTrace();
}
File[] files = new File[filesCount];
for(int i = 0; i < filesCount; i++)
{
long fileLength = 0;
try {
fileLength = dis.readLong();
} catch (IOException e) {
e.printStackTrace();
}
String fileType = null;
try {
fileType = dis.readUTF();
} catch (IOException e) {
e.printStackTrace();
}
String fileName = null;
try {
fileName = dis.readUTF();
} catch (IOException e) {
e.printStackTrace();
}
if (fileType == "apk"){
File dirs = new File(intRoot + "RemoteView/" + "app");
if(!dirs.exists()) dirs.mkdirs();
files[i] = new File(dirs.toString() + "/" + fileName);
}
FileOutputStream fos = null;
try {
fos = new FileOutputStream(files[i]);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
BufferedOutputStream bos = new BufferedOutputStream(fos);
for(int j = 0; j < fileLength; j++) {
try {
bos.write(bis.read());
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
});
}
void startClient(Intent intent){
socket = new Socket();
String intRoot = new FilesDisplayFragment().internalDirRoot;
Parcelable parcelable = intent.getParcelableExtra("hostAdd");
Parceler parceler = Parcels.unwrap(parcelable);
String hostAdd = parceler.getGroupOwnerAddress().getHostAddress();
ExecutorService clientExecutor = newSingleThreadExecutor();
clientExecutor.submit(new Runnable() {
@Override
public void run() {
try {
socket.connect(new InetSocketAddress(hostAdd, 8888), 5000);
} catch (IOException e) {
e.printStackTrace();
}
BufferedInputStream bis = null;
try {
bis = new BufferedInputStream(socket.getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
DataInputStream dis = new DataInputStream(bis);
int filesCount = 0;
try {
filesCount = dis.readInt();
} catch (IOException e) {
e.printStackTrace();
}
File[] files = new File[filesCount];
for(int i = 0; i < filesCount; i++)
{
long fileLength = 0;
try {
fileLength = dis.readLong();
} catch (IOException e) {
e.printStackTrace();
}
String fileType = null;
try {
fileType = dis.readUTF();
} catch (IOException e) {
e.printStackTrace();
}
String fileName = null;
try {
fileName = dis.readUTF();
} catch (IOException e) {
e.printStackTrace();
}
if (fileType == "apk"){
File dirs = new File(intRoot + "RemoteView/" + "app");
if(!dirs.exists()) dirs.mkdirs();
files[i] = new File(dirs.toString() + "/" + fileName);
}
FileOutputStream fos = null;
try {
fos = new FileOutputStream(files[i]);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
BufferedOutputStream bos = new BufferedOutputStream(fos);
for(int j = 0; j < fileLength; j++) {
try {
bos.write(bis.read());
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
});
}
void send(Intent intent){
Parcelable parcelable = intent.getParcelableExtra("selected_wifi_files");
Parceler parceler = Parcels.unwrap(parcelable);
File[] files = parceler.getSelectedFilesWifi();
ExecutorService sendExecutor = newSingleThreadExecutor();
sendExecutor.submit(new Runnable() {
@Override
public void run() {
If(bos == null){
BufferedOutputStream bos = null;
try {
bos = new BufferedOutputStream(socket.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
DataOutputStream dos = new DataOutputStream(bos);
BufferedOutputStream bos = null;
try {
bos = new BufferedOutputStream(socket.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
DataOutputStream dos = new DataOutputStream(bos);
}
try {
dos.writeInt(files.length);
} catch (IOException e) {
e.printStackTrace();
}
for(File file : files)
{
long length = file.length();
try {
dos.writeLong(length);
} catch (IOException e) {
e.printStackTrace();
}
String fileType = FilenameUtils.getExtension(String.valueOf(file));
try {
dos.writeUTF(fileType);
} catch (IOException e) {
e.printStackTrace();
}
String name = file.getName();
try {
dos.writeUTF(name);
} catch (IOException e) {
e.printStackTrace();
}
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
BufferedInputStream bis = new BufferedInputStream(fis);
int theByte = 0;
while(true) {
try {
if (!((theByte = bis.read()) != -1)) break;
} catch (IOException e) {
e.printStackTrace();
}
try {
bos.write(theByte);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
});
}
bos.write(bis.read())Indeed that makes the transfer slow reading and writing one byte at the time.