# socket_file_share **Repository Path**: Dashvvood/socket_file_share ## Basic Information - **Project Name**: socket_file_share - **Description**: File Transfer based on TCP socket, using mininet as simulator. - **Primary Language**: Python - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2022-11-12 - **Last Updated**: 2022-11-12 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ## How two use ? ### Protocol Design There are totally 4 versions protocols, there is their relation: ```mermaid graph TB 0(V1: Single Process & Single Thread) --> 1((Parallel)) 1 --> 2(V2: MultiProcess) 1 --> 3(V3: MultiThread) 0 --> 4((Coroutine)) 4 --> 5(V4: Async + non-Block) ``` > **V1 :** > > This version seems the simplest one, it can treat multiple requests sequentially. And it seems not very slow than its successors. > > **V2 :** > > The main process create a sub process for sharing file independently every time it got a connection from clients. > > **V3 :** > > Because the **GIL** (Global Interpreter Lock) makes the *multithread* parts of Python a fake parallelism, we use multithread for the purpose of decoupling the **connection listening** and **file transfering**. > > **V4 :** > > The asynchronism is supposed to fit well this lab, because a file application covers the network IO and File IO, a real IO-bound application. I use `ayncio`, `aiofiles` to start this **coroutine trial** , however the result is weird. ### Code Structure ```shell . |-- README.md |-- client.py # for several mininet hosts to run as client |-- data | |-- block100M # a file of 100MB | |-- create.sh # create file | `-- groundhog.jpg # an image |-- logs | `-- 20221111.log # log file |-- main.py # Entry file !!!!!!!! |-- network.py # Define a star topo and net build |-- protocol_v1.py # protocols of different version |-- protocol_v2.py |-- protocol_v3.py |-- protocol_v4.py |-- server.py # for a mininet host to run as server ``` ### **Parameters Help** As we have to run these codes respectively in different hosts on mininet, a good way to handle so many parameters is using command-line. With the help of `argparse`, we can see a brief help by running - `python3 main.py -h` ```shell # python3 main.py -h usage: main.py [-h] [--protocol PROTOCOL] [--host HOST] [--port PORT] [--logdir LOGDIR] [--name NAME] [--filedir FILEDIR] [--filename FILENAME] [--client_number CLIENT_NUMBER] optional arguments: -h, --help show this help message and exit --protocol PROTOCOL Which version of protocol --host HOST Server IP --port PORT Server port --logdir LOGDIR --name NAME --filedir FILEDIR --filename FILENAME --client_number CLIENT_NUMBER ``` ### **Example** ```shell python3 main.py --protocol 1 --host 10.0.0.1 --logdir logs --filedir data --filename groundhog.jpg --client_number 5 ``` - `--protocol 1` : I want use the protocol of version 1 - `--host 10.0.0.1` `--port 12321` the server socket will bind this address while the client sockets will connect this address. - `--logdir logs` no matter server or clients will output some information under this directory, to a specific log file *named after the date of execution*. - `--filedir data ` : the data directory of **server** - `--filename 1.jpg` file requested by clients - `--client_number 5` The number of clients in the star topo **We will see 👀 :** **What happens on terminal:** ```shell root@62bbe609bf61:/home/socket/src# python3 main.py --protocol 1 --host 10.0.0.1 --port 12321 --logdir logs --filedir data --filename groundhog.jpg --client_number 5 *** Error setting resource limits. Mininet's performance may be affected. h000 h000-eth0:s000-eth1 h001 h001-eth0:s001-eth1 h002 h002-eth0:s002-eth1 h003 h003-eth0:s003-eth1 h004 h004-eth0:s004-eth1 h005 h005-eth0:s005-eth1 mininet> ``` > We can see we enter the mininet CLI. CLI will help us test and debug, and we can abandon some **sleep** for waiting every connection's could get successful service. **What happens on folder:** ```shell . |-- README.md |-- __pycache__ | |-- network.cpython-36.pyc | `-- protocol_v1.cpython-36.pyc |-- client # new | |-- 001 | | `-- groundhog.jpg # new | |-- 002 | | `-- groundhog.jpg # new | |-- 003 | | `-- groundhog.jpg # new | |-- 004 | | `-- groundhog.jpg # new | `-- 005 | `-- groundhog.jpg # new |-- client.py |-- data | |-- block100M | |-- create.sh | `-- groundhog.jpg |-- logs | `-- 20221111.log # updated |-- main.py |-- network.py |-- protocol_v1.py |-- protocol_v2.py |-- protocol_v3.py |-- protocol_v4.py `-- server.py ``` > We can see the clients downloading successfully the target file *groundhog.jpg* The image is very direct to see whether the transfer is complete and no-error. ![Screenshot from 2022-11-11 22-28-21](README.assets/Screenshot from 2022-11-11 22-28-21.png) **What happens in log file: ** ```shell 2022-11-11 22:10:53,293 - Server - INFO - TcpFileServer's Logger Inited 2022-11-11 22:10:53,293 - Server - INFO - Server socket inited, start listening on 10.0.0.1,12321 2022-11-11 22:10:56,294 - Client002 - INFO - Logger Inited 2022-11-11 22:10:56,294 - Client002 - INFO - Try to connect to server (10.0.0.1, 12321) 2022-11-11 22:10:56,294 - Client004 - INFO - Logger Inited 2022-11-11 22:10:56,294 - Client001 - INFO - Logger Inited 2022-11-11 22:10:56,294 - Client004 - INFO - Try to connect to server (10.0.0.1, 12321) 2022-11-11 22:10:56,294 - Client001 - INFO - Try to connect to server (10.0.0.1, 12321) 2022-11-11 22:10:56,295 - Client005 - INFO - Logger Inited 2022-11-11 22:10:56,295 - Client005 - INFO - Try to connect to server (10.0.0.1, 12321) 2022-11-11 22:10:56,296 - Client003 - INFO - Logger Inited 2022-11-11 22:10:56,296 - Client003 - INFO - Try to connect to server (10.0.0.1, 12321) 2022-11-11 22:10:56,325 - Client002 - INFO - Time used for socket connection 0.031078s 2022-11-11 22:10:56,325 - Client002 - INFO - Connection done, my name ('10.0.0.3', 40230) 2022-11-11 22:10:56,325 - Client004 - INFO - Time used for socket connection 0.030903s 2022-11-11 22:10:56,325 - Client001 - INFO - Time used for socket connection 0.030878s 2022-11-11 22:10:56,325 - Client001 - INFO - Connection done, my name ('10.0.0.2', 58434) 2022-11-11 22:10:56,325 - Client004 - INFO - Connection done, my name ('10.0.0.5', 54804) 2022-11-11 22:10:56,326 - Client005 - INFO - Time used for socket connection 0.030659s 2022-11-11 22:10:56,326 - Client005 - INFO - Connection done, my name ('10.0.0.6', 42786) 2022-11-11 22:10:56,327 - Client003 - INFO - Time used for socket connection 0.030796s 2022-11-11 22:10:56,327 - Client003 - INFO - Connection done, my name ('10.0.0.4', 60688) 2022-11-11 22:10:56,340 - Server - INFO - Got connection from ('10.0.0.3', 40230) 2022-11-11 22:10:56,416 - Server - INFO - File Transfer Done to ('10.0.0.3', 40230) 2022-11-11 22:10:56,416 - Server - INFO - Finish service for ('10.0.0.3', 40230) 2022-11-11 22:10:56,416 - Server - INFO - Got connection from ('10.0.0.5', 54804) 2022-11-11 22:10:56,584 - Server - INFO - File Transfer Done to ('10.0.0.5', 54804) 2022-11-11 22:10:56,584 - Server - INFO - Finish service for ('10.0.0.5', 54804) 2022-11-11 22:10:56,584 - Server - INFO - Got connection from ('10.0.0.2', 58434) 2022-11-11 22:10:56,614 - Client002 - INFO - Done 2022-11-11 22:10:56,615 - Client002 - INFO - Time used for check_receive 0.2895s 2022-11-11 22:10:56,779 - Server - INFO - File Transfer Done to ('10.0.0.2', 58434) 2022-11-11 22:10:56,779 - Server - INFO - Finish service for ('10.0.0.2', 58434) 2022-11-11 22:10:56,779 - Server - INFO - Got connection from ('10.0.0.6', 42786) 2022-11-11 22:10:56,906 - Client004 - INFO - Done 2022-11-11 22:10:56,906 - Client004 - INFO - Time used for check_receive 0.5805s 2022-11-11 22:10:57,003 - Server - INFO - File Transfer Done to ('10.0.0.6', 42786) 2022-11-11 22:10:57,003 - Server - INFO - Finish service for ('10.0.0.6', 42786) 2022-11-11 22:10:57,003 - Server - INFO - Got connection from ('10.0.0.4', 60688) 2022-11-11 22:10:57,127 - Client001 - INFO - Done 2022-11-11 22:10:57,128 - Client001 - INFO - Time used for check_receive 0.8023s 2022-11-11 22:10:57,257 - Server - INFO - File Transfer Done to ('10.0.0.4', 60688) 2022-11-11 22:10:57,257 - Server - INFO - Finish service for ('10.0.0.4', 60688) 2022-11-11 22:10:57,295 - Client005 - INFO - Done 2022-11-11 22:10:57,295 - Client005 - INFO - Time used for check_receive 0.9691s 2022-11-11 22:10:57,481 - Client003 - INFO - Done 2022-11-11 22:10:57,481 - Client003 - INFO - Time used for check_receive 1.1543s ``` #### Docker