Android ServerSocket Programming with jCIFS Streaming Files
Introduction
This article delves into the intricacies of establishing a server on an Android device using ServerSocket and facilitating file transfer via jCIFS, a Java library for interacting with SMB/CIFS network protocols.
Setting up the Environment
* **Android Studio:** The primary IDE for Android development.
* **jCIFS Library:** Import the jCIFS library into your Android project. You can download it from [https://jcifs.samba.org/](https://jcifs.samba.org/).
Implementing the Server
“`java
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import jcifs.smb.SmbFile;
public class FileServer {
public static void main(String[] args) {
try {
// Create a ServerSocket on a specific port
ServerSocket serverSocket = new ServerSocket(8080);
System.out.println(“Server started on port 8080”);
while (true) {
// Accept a client connection
Socket clientSocket = serverSocket.accept();
System.out.println(“Client connected: ” + clientSocket.getInetAddress().getHostAddress());
// Handle the client request
handleClientRequest(clientSocket);
}
} catch (IOException e) {
e.printStackTrace();
}
}
private static void handleClientRequest(Socket clientSocket) {
try {
// Get input and output streams
InputStream inputStream = clientSocket.getInputStream();
OutputStream outputStream = clientSocket.getOutputStream();
// Receive file name from client
byte[] fileNameBytes = new byte[1024];
int bytesRead = inputStream.read(fileNameBytes);
String fileName = new String(fileNameBytes, 0, bytesRead);
// Create jCIFS SmbFile object
SmbFile smbFile = new SmbFile(“//your-server/shared-folder/” + fileName);
// Stream file data
if (smbFile.exists()) {
System.out.println(“File found: ” + fileName);
InputStream fileInputStream = smbFile.getInputStream();
byte[] buffer = new byte[1024];
int bytesReadFromFile;
while ((bytesReadFromFile = fileInputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesReadFromFile);
}
fileInputStream.close();
} else {
System.out.println(“File not found: ” + fileName);
}
// Close streams and socket
inputStream.close();
outputStream.close();
clientSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
“`
“`
Server started on port 8080
Client connected: 192.168.1.10
File found: myfile.txt
“`
Client-Side Implementation
“`java
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
public class FileClient {
public static void main(String[] args) {
try {
// Connect to the server
Socket socket = new Socket(“192.168.1.10”, 8080);
// Send file name to server
String fileName = “myfile.txt”;
OutputStream outputStream = socket.getOutputStream();
outputStream.write(fileName.getBytes());
// Receive file data
InputStream inputStream = socket.getInputStream();
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
// Handle received data
// …
}
// Close streams and socket
inputStream.close();
outputStream.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
“`
Explanation
* **Server:** The ServerSocket class creates a socket on the specified port (8080). The accept() method waits for a client to connect.
* **jCIFS:** The jCIFS library is utilized to interact with the SMB/CIFS network protocol for accessing files on a remote server.
* **File Transfer:** The server streams the requested file data to the client.
* **Client:** The FileClient connects to the server using the IP address and port. It sends the requested file name and receives the file data.
Advantages
* **Efficient File Transfer:** The server utilizes jCIFS for direct communication with the SMB/CIFS server, optimizing file transfer.
* **Cross-Platform Compatibility:** jCIFS supports various platforms, making the file transfer process compatible across diverse environments.
* **Enhanced Security:** SMB/CIFS protocols provide robust security features for secure file transfer.
Conclusion
This article provides a detailed guide to creating a basic file server on Android using ServerSocket and jCIFS. This powerful combination empowers developers to build Android applications with efficient and secure file transfer capabilities.