TCP

TCP chatroom

This is the first assignment of “Computer Network: a top- down approach” course. It’s a simple TCP chatroom. The server can accept multiple clients and exchange messages and files with them. The server and client are implemented by python.

Socket Programming Lab Report

Name: Bao
Student ID: xxxxxxxxxx
Date: 29/03


1.Experiment Objectives

  • To understand the basics of socket programming
  • To implement server-client architecture using TCP protocol.
  • To implement multi-thread server using TCP protocol.

2.Experimental Environment

  • Programming Language: Python 3.12.4
  • Libraries: socket and threading module in python
  • Tool: Wireshake for packet capture

Usage

For single thread TCP part,

Server
1
2
cd ./TCP/
python server.py 18080 input

Options:

  • 18080: Server listening port (default:18080)
  • input: Server response type (default:upper), including “input” and “upper”
Client
1
2
cd ./TCP
python client.py 127.0.0.1 18080

Options:

  • 127.0.0.1 server IP to connect to (default:127.0.0.1)
  • 18080 server port to connect to (default:18080)

For multiple thread TCP server part

Server
1
2
cd ./Threading/
python server.py 18080 upper 10

Options:

  • 18080: Server listening port (default:18080)
  • 10: the maximum number of clients the server can accept (default:10)
  • upper: Server response mode (default:upper), including “input” and “upper”
Client
1
2
cd ./Threading/
python client.py client0 127.0.0.1 18080

And for a new client

1
2
cd ./Threading/
python client.py client1 127.0.0.1 18080

And so on…
Options:

  • 127.0.0.1 server IP to connect to (default:127.0.0.1)
  • 18080 server port to connect to (default:18080)
  • client1 client name (default:client0), necessary for more than 1 clients, to separately manage the files different clients own.

Note:

  • All above commands are executed in the root directory of the project, and you can run without any arguments to use the default values.
  • Server is set a time out limit of 120, which is not necessary. It’s just to easily quit the server when it’s not being used.
  • Client is set a time out limit of 30.

After connection is set up, the client and server go in a circle of “receive - send”. User repeatedly give commands through client.
There are 3 types of input commands:

  • ask for file:someFile :require server sending someFile to the folder client manages
  • send file:someFile :send local file someFile to folder “server” that server manages
  • exit :quit connction with server
  • else: send the input message encoded by ‘UTF-8’ to server, and receive reponse from server. The argument responseType in class TCP_Server/Multithread_Server determines how server response. responseType = upper automatically return the total uppercase of client massage, responseType = input require server input response by hand.

3.Experimental Procedure

Using Wireshake, we can capture the packets sent between client and server.
Because multi-thread version totally inludes functions of single thread version, I will only show the result of multi thread version here.
The following figures shows the example:
cmd
During the connection, the WireShark capture the packets sent between client and server.
When the connections are setup:
capture
When client0 exits and client1 is receiving shore.jpg from server:
result
I didn’t show the whole process because shore.jpg takes too many packets to be sent.

For more test, the ./server/ has 出师表.txt, the ./client has keep.jpg.

4.Experiment Analysis

It can be easily found that “3-way” hand shake happens. Looking at the wireshark captures, before the first request of port 9461 and the connection of port 9465, there are 3 packets sent between client and server:No.109,110,111. In 109, client0 sends a SYN packet to server, and in 110, server sends a SYN-ACK packet to client0, and in 111, client0 sends a ACK packet to server. After that, the connection is established. Of course, the same thing happens for client1, which is demonstrated in No.144,145,146.

5.Experiment Summary

TCP can be visualized like the following picture:
TCP
With the experiment, we can better under stand why TCP is reliable.

×

纯属好玩

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

文章目录
  1. 1. TCP chatroom
  2. 2. Socket Programming Lab Report
    1. 2.1. 1.Experiment Objectives
    2. 2.2. 2.Experimental Environment
    3. 2.3. Usage
      1. 2.3.0.1. Server
      2. 2.3.0.2. Client
      3. 2.3.0.3. Server
      4. 2.3.0.4. Client
    4. 2.3.1. Note:
  3. 2.4. 3.Experimental Procedure
  4. 2.5. 4.Experiment Analysis
  5. 2.6. 5.Experiment Summary
,