Skip to content

ArielElb/TCP-Server

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 

Repository files navigation

TCP-Server

image

TCP Traffic Analysis and TCP Server Implementation

Part I: TCP Traffic Analysis In this part, we will analyze the traffic generated by running a simple client-server code using the TCP protocol. The client will send two messages to the server, one with the client's name and the other with the client's ID number. We will capture the traffic using Wireshark and analyze the TCP connection process and the sequence and acknowledgment numbers.

TCP Connection Process

  1. The client initiates the connection by sending a SYN packet to the server.
  2. The server responds with a SYN-ACK packet, acknowledging the client's request and establishing a connection.
  3. The client sends an ACK packet to confirm the server's acknowledgment and finalize the connection establishment.
  4. Once the connection is established, the client sends the first message (client's name) to the server.
  5. The server receives the message and sends a response back to the client.
  6. The client sends the second message (client's ID number) to the server.
  7. The server receives the message and sends a response back to the client.
  8. Finally, either the client or the server terminates the connection by sending a FIN packet, and the other party acknowledges it with an ACK packet.

Sequence and Acknowledgment Numbers

  • The sequence number in TCP represents the number assigned to the first byte of data in a segment. The acknowledgment number is the next sequence number that the receiver expects to receive.

Throughout the TCP connection process, the sequence and acknowledgment numbers are updated as follows:

  1. During the connection establishment, the sequence number is randomly chosen by the client (SYN packet) and the acknowledgment number is set to the client's sequence number plus one (SYN-ACK packet).
  2. After the connection is established, the sequence and acknowledgment numbers are updated based on the amount of data exchanged between the client and the server.
  3. For each message sent by the client, the sequence number is incremented by the number of bytes in the message, and the acknowledgment number is updated based on the next expected sequence number.
  4. The server responds to each message by setting its sequence number to the acknowledgment number received from the client and vice versa.
  5. When the connection is terminated, the sequence and acknowledgment numbers are updated accordingly to indicate the end of data transmission.
  • To capture the traffic and analyze the TCP connection process, we will run the provided client and server code snippets.

Part II: TCP Server Implementation

In this part, we need to implement a TCP server that serves files requested by the client. The server will handle GET requests from the client and send the corresponding files back. The server should support both plain text files and binary files such as images.

The server implementation follows these steps:

  1. Receive a client request with a GET command and the file name/path.
  2. Parse the request to extract the file name/path.
  3. Check if the requested file exists in the server's "files" folder.
  4. If the file exists, read its contents and determine the file size.
  5. If the file is a plain text file, construct an HTTP response with the status code "200 OK," connection type, content length, and file contents.
  6. If the file is a binary file (jpg or ico), read the file in binary form and construct the HTTP response accordingly.
  7. If the file does not exist, construct an HTTP response with the status code "404 Not Found."
  8. Send the constructed HTTP response back to the client.
  9. If the connection type in the client's request is "close," close the connection after sending the file. Otherwise, keep the connection alive for the next request.
  10. Set a timeout for the server socket, so if no response is received within 1 second, close the current connection and handle the next client.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages