0% found this document useful (0 votes)
22 views

Parallel File Transfer Explanation

Uploaded by

Mohsin Abbasi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Parallel File Transfer Explanation

Uploaded by

Mohsin Abbasi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Parallel File Transfer Program

Introduction
This document explains the functionality of a parallel file transfer program that uses socket
programming and OpenMP for parallelism. The program is implemented in C++ and
consists of two main parts: the server and the client. The server listens for incoming client
connections, receives files from multiple clients in parallel, and writes the received data to
disk. The client connects to the server, reads a file from disk, and sends it to the server.

Code Explanation

1. Server Code
The server code is responsible for accepting connections from multiple clients, receiving file
data, and writing the data to disk. The key components of the server code are as follows:

a. Includes and Defines


```cpp
#include <iostream>
#include <fstream>
#include <thread>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <cstring>
#ifdef _OPENMP
#include <omp.h>
#endif
#define PORT 8080
#define BUFFER_SIZE 1024
```
These headers are included for standard input/output operations, file handling, threading,
and socket programming. The OpenMP header is included to enable parallelism.

b. handleClient Function
This function handles each client connection in a separate thread. It receives the file name
from the client, reads the incoming data, and writes it to a file on disk.

c. main Function
The main function creates a server socket, binds it to an address and port, and listens for
incoming client connections. When a client connects, a new thread is created to handle the
client using the handleClient function.
2. Client Code
The client code is responsible for connecting to the server, reading a file from disk, and
sending the file data to the server. The key components of the client code are as follows:

a. Includes and Defines


```cpp
#include <iostream>
#include <fstream>
#include <vector>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#ifdef _OPENMP
#include <omp.h>
#endif
#define SERVER_IP "127.0.0.1"
#define PORT 8080
#define BUFFER_SIZE 1024
```
These headers are included for standard input/output operations, file handling, and socket
programming.

b. main Function
The main function creates a socket, connects to the server, and sends the file name to the
server. It then reads the file from disk into a buffer and uses OpenMP to send the file data to
the server in parallel chunks.

Compilation and Execution


Since the default Clang compiler on macOS does not support OpenMP, you need to install
LLVM and libomp using Homebrew. Here are the steps to compile and run the programs:

1. Install LLVM and libomp


```bash
brew install llvm libomp
```
This command installs LLVM and libomp using Homebrew.

2. Compile the Server and Client Code


```bash
/usr/local/opt/llvm/bin/clang++ -std=c++11 -fopenmp server.cpp -o server -lpthread
-I/usr/local/opt/llvm/include -L/usr/local/opt/llvm/lib
/usr/local/opt/llvm/bin/clang++ -std=c++11 -fopenmp client.cpp -o client -lpthread
-I/usr/local/opt/llvm/include -L/usr/local/opt/llvm/lib
```
These commands compile the server and client code using the Clang++ compiler from LLVM
with OpenMP support.

Running the Programs


1. Start the server by running:
```bash
./server
```
2. Run the client in a separate terminal window:
```bash
./client
```
The client will prompt you to enter the file name to send.

Error Handling and Performance


Both the server and client code include error handling to manage issues such as socket
creation failure, connection errors, and file handling errors. The performance can be
evaluated by measuring the time taken to transfer a file using both serial and parallel
execution.

Conclusion
This program demonstrates how to use socket programming and OpenMP to transfer files
in parallel between a server and multiple clients. The code provides a basic framework for
efficient file transfer and showcases the use of parallelism for performance improvement.

You might also like