Hi My ultimate aim is to transfer files using wifi direct api in android between two devices. Once device act as a client another one act as a server as in the wifi direct sdk demo. For this, Creating a socket from the client side by using server port and host address. I want to transfer multiple files. In the receiver side while accepting the client socket connection, I have to create the file with the filename of the file which sent from the client side. But I dont know that filename from the server side.
So how to send file name using socket connection for this wifi direct transfer mode for multiple file transfer.
Creating socket from client side using server port and host address:
fileUris = intent.getExtras().getParcelableArrayList(EXTRA_STREAM);
String host = intent.getExtras().getString(
EXTRAS_GROUP_OWNER_ADDRESS);
Socket socket = new Socket();
int port = intent.getExtras().getInt(EXTRAS_GROUP_OWNER_PORT);
try {
Log.d(WifiDirectActivity.TAG, "Opening client socket - ");
socket.bind(null);
socket.connect((new InetSocketAddress(host, port)),
SOCKET_TIMEOUT);
Log.d(WifiDirectActivity.TAG,
"Client socket - " + socket.isConnected());
OutputStream stream = socket.getOutputStream();
ContentResolver cr = context.getContentResolver();
InputStream is = null;
for (int i = 0; i < fileUris.size(); i++) {
Uri uri = fileUris.get(0);
try {
is = cr.openInputStream(Uri.parse(uri.toString()));
} catch (FileNotFoundException e) {
Log.d(WifiDirectActivity.TAG, e.toString());
}
DeviceDetailFragment.copyFile(is, stream);
Log.d(WifiDirectActivity.TAG, "Client: Data written");
}
Accepting client socket connection form server side:
ServerSocket serverSocket = new ServerSocket(8988);
Log.d(WifiDirectActivity.TAG, "Server: Socket opened");
Socket client = serverSocket.accept();
Log.d(WifiDirectActivity.TAG,
"Server: connection done with client");
final File f = new File(
Environment.getExternalStorageDirectory() + "/"
+ context.getPackageName() + "/wifip2pshared-"
+ "sample");
File dirs = new File(f.getParent());
if (!dirs.exists())
dirs.mkdirs();
f.createNewFile();
Log.d(WifiDirectActivity.TAG,
"server: copying files " + f.toString());
InputStream inputstream = client.getInputStream();
copyFile(inputstream, new FileOutputStream(f));
serverSocket.close();
Really struck on giving file name at the receiver side at file creation. Is there any way to send file name along with that. please Help me on this. Thanks in advance.