Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
9a2d255
Update student information in README
tonle05 Nov 13, 2025
64c7d4c
Add 'Bonjour' to text.txt
tonle05 Nov 22, 2025
7003080
Upload files
tonle05 Nov 22, 2025
acdf13f
Add LaTeX document for RPC file transfer implementation
tonle05 Dec 4, 2025
d696c8a
Add files via upload
tonle05 Dec 4, 2025
5b207b5
Create received_files
tonle05 Dec 4, 2025
35f4a68
Delete Practice 2/received_files
tonle05 Dec 4, 2025
9bfe1c6
Create test.txt with year-end party description
tonle05 Dec 4, 2025
5ee1fd8
Update Student Group ID in README.md
tonle05 Dec 15, 2025
0fce90c
Update author information in the document
tonle05 Dec 15, 2025
cc7e2a3
Add files via upload
tonle05 Dec 15, 2025
292f265
Add files via upload
tonle05 Dec 15, 2025
e99b840
Add MPI File Transfer Report document
tonle05 Dec 15, 2025
ce704e1
Add files via upload
tonle05 Dec 15, 2025
2382e62
Add LaTeX document for Word Count MapReduce implementation
tonle05 Dec 15, 2025
322abee
Add files via upload
tonle05 Dec 15, 2025
94d03c9
Add files via upload
tonle05 Dec 15, 2025
88b25d4
Add LaTeX document for Longest Path MapReduce
tonle05 Dec 15, 2025
6c0e994
Add files via upload
tonle05 Dec 15, 2025
7b13f27
Add GlusterFS practical work documentation
tonle05 Dec 15, 2025
a1df9b1
Add files via upload
tonle05 Dec 15, 2025
dec9318
Add files via upload
tonle05 Dec 15, 2025
42cde0b
Create LaTeX document for Sudoku web app project
tonle05 Dec 15, 2025
2807413
Add files via upload
tonle05 Dec 15, 2025
d75a9b4
Add requirements for Flask and Gunicorn
tonle05 Dec 15, 2025
352cd54
Add index.html template for Sudoku GAE
tonle05 Dec 15, 2025
808d6c5
Add files via upload
tonle05 Dec 15, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 118 additions & 0 deletions Practice 1/01.tcp.file.transfer.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
\documentclass[11pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\usepackage{hyperref}
\usepackage{listings}
\usepackage{caption}
\usepackage{tikz}
\usepackage{float}
\usetikzlibrary{positioning,arrows.meta}

\title{Practical Work 1: TCP File Transfer}
\author{Le Quy Ton}
\date{\today}

\lstset{
basicstyle=\ttfamily\small,
breaklines=true,
frame=single
}

\begin{document}
\maketitle

\section*{1. Introduction}
This report documents an individual implementation of a one-to-one TCP
file transfer utility (client \& server) using POSIX sockets in C. The
purpose is to design a minimal protocol, implement the transfer over a
reliable TCP stream, and demonstrate end-to-end file delivery.

\section*{2. Protocol Design}
The protocol uses short ASCII headers exchanged before streaming raw binary
data. It is intentionally simple to avoid parsing complexity and to make
testing straightforward.

\subsection*{Protocol Steps}
\begin{enumerate}
\item Client connects to server (TCP three-way handshake).
\item Client sends header: \texttt{SEND <filename>\textbackslash n}.
\item Server replies: \texttt{OK}.
\item Client sends header: \texttt{SIZE <number>\textbackslash n}.
\item Server replies: \texttt{OK}.
\item Client streams raw binary file data (exactly \texttt{<number>} bytes).
\item Server writes received bytes to disk under the received filename.
\item Server replies: \texttt{DONE} and the connection may close.
\end{enumerate}

\clearpage
\section*{3. Protocol Diagram}

\begin{figure}[htbp]
\centering
\includegraphics[width=0.92\textwidth]{diagram.png}
\caption{Socket-level workflow: open, connect/accept, write/read, close.}
\label{fig:protocol_image}
\end{figure}

\section*{4. System Organization}
\begin{itemize}
\item \textbf{Client:} Establishes TCP connection (socket(), connect()),
sends metadata headers and streams file bytes (send()).
\item \textbf{Server:} Listens on a port (socket(), bind(), listen()),
accepts connection (accept()), reads headers, receives bytes (recv())
and writes them to disk.
\end{itemize}

\section*{5. Implementation}
Below is a concise description of how the transfer is implemented and a
short illustrative snippet showing the exchange of headers (not the full
program).

\subsection*{Runtime sequence}
\begin{enumerate}
\item Server: create socket, setsockopt(SO\_REUSEADDR), bind(port), listen().
\item Client: create socket, connect(server\_ip, port).
\item Client sends 'SEND <filename>\textbackslash n'; server reads and responds 'OK'.
\item Client sends 'SIZE <numBytes>\textbackslash n'; server responds 'OK'.
\item Client: read file in chunks and \texttt{send()} them to server.
\item Server: loop \texttt{recv()} until total bytes received == <numBytes>, write chunks to file.
\item Server sends 'DONE' and either closes or waits for next connection (design choice).
\end{enumerate}

\subsection*{Minimal code snippet illustrating header exchange}
\begin{lstlisting}[language=C, basicstyle=\ttfamily\footnotesize]
/* client side (concept) */
sprintf(buf,"SEND %s\n", filename);
send(sock, buf, strlen(buf), 0);
recv(sock, buf, sizeof(buf), 0); /* expect "OK" */

sprintf(buf,"SIZE %d\n", filesize);
send(sock, buf, strlen(buf), 0);
recv(sock, buf, sizeof(buf), 0); /* expect "OK" */

/* then stream binary bytes via send() */
\end{lstlisting}

\subsection*{Notes on robustness}
\begin{itemize}
\item All \texttt{recv()} return values must be checked (0 = peer closed,
negative = error).
\item Use a loop to accumulate partial reads/writes until the declared
file size is reached.
\item Consider basic sanity checks: file size limit, filename validity,
and handling abrupt disconnects.
\end{itemize}
\includegraphics[width=0.5\textwidth]{server.png}
\includegraphics[width=0.5\textwidth]{client.png}
\section*{7. Work distribution}
This practical work was completed individually by the author. All design,
implementation and testing steps were done by myself.

\section*{8. Conclusion}
A minimal TCP-based file-transfer protocol was designed and implemented.
The system demonstrates reliable file delivery over TCP using a small,
clear protocol and can be extended for multiple clients, authentication,
or reliability checks if required.

\end{document}
Binary file added Practice 1/LeQuyTon_23BI14425_Practical_1.pdf
Binary file not shown.
60 changes: 60 additions & 0 deletions Practice 1/client.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>

#define BUF 4096

int main(int argc, char *argv[]) {
if (argc != 4) {
printf("Usage: %s <server_ip> <port> <file>\n", argv[0]);
return 0;
}

char *server_ip = argv[1];
int port = atoi(argv[2]);
char *filename = argv[3];

int sock = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in addr;

addr.sin_family = AF_INET;
addr.sin_port = htons(port);
inet_pton(AF_INET, server_ip, &addr.sin_addr);

connect(sock, (struct sockaddr*)&addr, sizeof(addr));
printf("[CLIENT] Connected to server.\n");

// get file size
FILE *f = fopen(filename, "rb");
fseek(f, 0, SEEK_END);
int filesize = ftell(f);
fseek(f, 0, SEEK_SET);

char buffer[1024];
char data[BUF];

// SEND filename
sprintf(buffer, "SEND %s\n", filename);
send(sock, buffer, strlen(buffer), 0);
recv(sock, buffer, sizeof(buffer), 0);

// SEND size
sprintf(buffer, "SIZE %d\n", filesize);
send(sock, buffer, strlen(buffer), 0);
recv(sock, buffer, sizeof(buffer), 0);

// SEND file data
int n;
while ((n = fread(data, 1, BUF, f)) > 0) {
send(sock, data, n, 0);
}
fclose(f);

recv(sock, buffer, sizeof(buffer), 0);
printf("[CLIENT] Transfer complete.\n");

close(sock);
return 0;
}
Binary file added Practice 1/client.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Practice 1/diagram.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
63 changes: 63 additions & 0 deletions Practice 1/server.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>

#define PORT 9000
#define BUF 4096

int main() {
int server_fd, client_fd;
struct sockaddr_in addr;
char buffer[1024], filename[128];
char data[BUF];
int filesize;
int received;

server_fd = socket(AF_INET, SOCK_STREAM, 0);

int opt = 1;
setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));

addr.sin_family = AF_INET;
addr.sin_port = htons(PORT);
addr.sin_addr.s_addr = INADDR_ANY;

bind(server_fd, (struct sockaddr*)&addr, sizeof(addr));
listen(server_fd, 1);

printf("[SERVER] Waiting for connection on port %d...\n", PORT);
client_fd = accept(server_fd, NULL, NULL);
printf("[SERVER] Client connected!\n");

// --- receive filename ---
recv(client_fd, buffer, sizeof(buffer), 0);
sscanf(buffer, "SEND %s", filename);
printf("[SERVER] Receiving file: %s\n", filename);
send(client_fd, "OK\n", 3, 0);

// --- receive size ---
recv(client_fd, buffer, sizeof(buffer), 0);
sscanf(buffer, "SIZE %d", &filesize);
printf("[SERVER] File size: %d bytes\n", filesize);
send(client_fd, "OK\n", 3, 0);

// --- receive binary data ---
FILE *f = fopen(filename, "wb");
received = 0;
while (received < filesize) {
int n = recv(client_fd, data, BUF, 0);
fwrite(data, 1, n, f);
received += n;
}
fclose(f);

printf("[SERVER] File saved.\n");
send(client_fd, "DONE\n", 5, 0);

close(client_fd);
close(server_fd);

return 0;
}
Binary file added Practice 1/server.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions Practice 1/text.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Bonjour
Loading