File Transfer Using Socket Communication
Project Overview
This project implements a file transfer system using direct socket communication between two devices. The goal is to transfer files without relying on external servers, demonstrating skills in Android development, concurrency handling, and maintaining UI responsiveness.
The application consists of two parts:
- Host (Server): Accepts incoming connections and receives files.
- Client: Sends selected files to the host.
Features
- Socket Communication: • Establishes direct communication between two devices using ServerSocket and Socket.
- File Transfer: • Supports files up to 50 MB in size. • Displays file transfer progress using a progress bar. • Handles connection drops and incomplete transfers gracefully.
- Concurrency: • Ensures UI responsiveness using coroutines and Dispatchers.IO.
- User Interface: • Simple and intuitive interface for starting the server, selecting files, and displaying transfer status.
Technology Stack • Programming Language: Kotlin • Concurrency: Coroutines • Socket Communication: Socket and ServerSocket from Java’s networking library. • UI Framework: Android XML layouts and components.
Getting Started
Prerequisites • Android Studio installed on your system. • Two Android devices connected to the same network (e.g., Wi-Fi).
Installation
- Clone the repository or download the project zip file.
- Open the project in Android Studio.
- Build the project and run it on two devices: • Device 1: Start as the Host (Server). • Device 2: Start as the Client and select a file to send.
Usage Instructions
Host (Server)
- Launch the application on the first device.
- Click “Start Server” to begin listening for client connections.
- Observe the status updates: • “Server started on port …” • “Waiting for client…”
- Upon a successful connection, the server receives the file and saves it in the device’s local storage.
Client
- Launch the application on the second device.
- Select a file using the file picker dialog.
- The file transfer begins automatically after selection.
- Observe the file transfer progress and success status.
File Transfer Workflow
- Server Side: • Starts a ServerSocket on the specified port. • Waits for a client connection using accept(). • Reads the file in chunks and saves it locally. • Updates the progress bar and displays a success message upon completion.
- Client Side: • Automatically starts sending the selected file after it is chosen. • Reads the file and sends it in chunks. • Updates the progress bar and displays a success message upon completion.
Challenges and Solutions
- Thread-Safe UI Updates: • Used Dispatchers.Main with coroutines to handle UI updates from background threads.
- Connection Management: • Ensured proper resource management by closing sockets and streams after each operation.
- File Size Handling: • Processed files in chunks to handle large files efficiently.
Future Enhancements
- Retry Mechanism: • Automatically retry file transfers if the connection is dropped.
- Foreground Service: • Enable transfers to continue even when the app is in the background.
- Notification Integration: • Notify the user of transfer success or failure via system notifications.