Network Protocols Implementation Steps: FTP, TCP, UDP

FTP Server Implementation Steps

  1. Start the program.
  2. Include necessary header files.
  3. Declare necessary variables.
  4. Print: “Enter the port address”.
  5. Create a TCP socket (‘SOCK_STREAM’) and store its file descriptor in ‘sd’.
  6. Check if the socket creation was successful. If not, print an error message.
  7. Setup server address structure (Family to IPv4 (‘AF_INET’), IP address to any available address (‘INADDR_ANY’)).
  8. Convert the port number to network byte order.
  9. Bind the socket to the server address.
  10. Check if binding was successful. If not, print an error message.
  11. Listen for incoming connections on the socket, allowing up to 5 pending connections.
  12. Accept a client connection and create a new socket (‘newsd’) for communication.
  13. Check if connection was successful. If not, print an error message.
  14. Receive the file name to be transferred from the client.
  15. Open the requested file in read mode (‘r’).
  16. If the file doesn’t exist, send an error message to the client and close the connection.
  17. Read the file line by line using fgets().
  18. Send each line of the file to the client.
  19. If an error occurs while sending, print an error message.
  20. After sending all file contents, send a completion message to the client.
  21. Close the connection with the client.
  22. Stop.

FTP Client Implementation Steps

Client Operations:

  • Start.
  • Include necessary header files.
  • Declare necessary variables.
  • Print: “Enter the port no”.
  • Create a TCP socket (‘SOCK_STREAM’) and store its file descriptor in ‘sd’.
  • Check if the socket creation was successful. If not, print an error message & exit.
  • Set up the server address structure (‘serveraddr’).
  • Set the Family to IPv4 (‘AF_INET’).
  • Set the IP address to any available address (‘INADDR_ANY’).
  • Convert the port number to network byte order.
  • Connect to the server using the created socket and server address.
  • Check if the connection was successful. If not, print an error message.
  • Prompt the user to enter the existing and new file name.
  • Open the new file in write mode (‘w’).
  • If the file cannot be opened, print an error message and exit.
  • Send the existing file name to the server.
  • Receive file contents from the server in a loop.
  • If an error occurs while receiving, print an error message.
  • Check if the received message indicates an error or completion.
  • If the file is not available, print an error message.
  • If the file transfer is completed, print a success message, close the file, and close the connection.
  • Write the received data to the new file.
  • If an error occurs while writing, print an error message and exit.
  • Stop.

TCP Server Algorithm

Steps:

  1. Start.
  2. Include necessary header files.
  3. Start main function and declare necessary variables.
  4. Create a socket using socket() function with domain ‘AF_INET’, type ‘SOCK_STREAM’, and protocol ‘0’.
  5. Use bzero() function to clear the memory allocated for ‘serveraddr’.
  6. Set server address structure (Family to ‘AF_INET’, port number based on user input, and IP address to ‘INADDR_ANY’).
  7. Bind the socket to the server address structure using bind() function.
  8. Clear the memory allocated for ‘clientaddr’.
  9. Use listen() function to listen for incoming connections on the server.
  10. Wait for and accept a connection from a client using accept() function. This function will block until a client connects.
  11. Read data sent by the client into the ‘message’ buffer using read(); Print the message received from the client.
  12. Send a response message to the client using write() function.
  13. Close the client socket using close() function.
  14. Close the server socket using close() function.
  15. Stop.

TCP Client Algorithm

Steps:

  1. Start the program.
  2. Include necessary header files.
  3. Start main function and declare required variables.
  4. Create client socket with domain ‘AF_INET’, type ‘SOCK_STREAM’ and protocol ‘0’.
  5. Use bzero() function to clear memory allocated for ‘serveraddr’.
  6. Set the family in the server address structure to ‘AF_INET’.
  7. Prompt the user to enter the port number and store it in the variable ‘port’.
  8. Convert port number from host to network byte order using htons() function.
  9. Print a message indicating an attempt to connect to the server, then use connect() function to establish a connection to the server using the client socket and server address structure.
  10. Send a message confirming the connection to the server.
  11. Send a message “Hi, I am client.” to the server using send() function.
  12. Receive message from the server and store it in the ‘message’ buffer.
  13. Print a message indicating the reception of the message.
  14. Print the received message.
  15. Close the client socket using close() function.
  16. Stop.

UDP Server Algorithm

Steps:
  1. Start.
  2. Include necessary header files.
  3. Start main function and declare necessary variables.
  4. Create a socket with domain ‘AF_INET’, type ‘SOCK_DGRAM’ (indicating UDP), and protocol ‘0’.
  5. Use bzero() function to clear the memory allocated for ‘serveraddr’.
  6. Set the server address structure (Family to ‘AF_INET’, port number based on user input, and IP address to ‘INADDR_ANY’).
  7. Bind the socket to the server address structure using bind().
  8. Wait to receive a message from the client using recvfrom() function. This function blocks until a message is received.
  9. Print the message received from the client.
  10. Send a response message “YOUR MESSAGE RECEIVED” to the client using sendto() function.
  11. Close the server socket using close() function.
  12. Stop.

UDP Client Algorithm

Steps:
  1. Start.
  2. Include necessary header files.
  3. Start main function and declare necessary variables.
  4. Create a client socket with domain ‘AF_INET’, type ‘SOCK_DGRAM’ (indicating UDP) and protocol ‘0’.
  5. Use bzero() function to clear the memory allocated for ‘serveraddr’.
  6. Set the family in the server address structure to ‘AF_INET’.
  7. Prompt the user to enter the port number and store it in the variable ‘port’.
  8. Convert port number to network byte order using htons() function.
  9. Read a message from the user using gets() function and store it in the ‘message’ buffer.
  10. Send the message “HI I AM CLIENT…” to the server using sendto().
  11. Print a message indicating that a message has been sent to the server.
  12. Print a message indicating that the client is waiting to receive a message from the server, then use recvfrom() to receive a message from the server and store it in the ‘message’ buffer.
  13. Print the received message using printf().
  14. Close the client socket using close() function.
  15. Stop.

Sender-Receiver Algorithm Simulation

  1. Start the program.
  2. Create a structure Frame with 2 integers: Pinfo and seq.
  3. Declare necessary variables.
  4. Declare a structure variable P.
  5. Initialize disconnect to 0 and turn to ‘s’.
  6. Set errframe and errorack to 1.
  7. Initialize P.Pinfo and P.seq to 0.
  8. Run a loop until disconnect is true:
    1. Call sender() function.
    2. Run a for loop with k incrementing from 1 to 10000000:
      • Call receiver() function.

Sender() Function Logic:

  1. Declare a static variable flag and set it to 0.
  2. Check if it’s the sender’s turn (turn == 's'):
    • If errorack is 0:
      • Print and send a packet with sequence number P.seq.
      • Update turn to ‘r’.
    • If errorack is not 0:
      • Check flag == 1.
      • Print the received acknowledgement (ack).
      • If P.seq == 5, set disconnect to 1 and return.
      • Increment P.Pinfo and P.seq.
      • Print and send packet with new sequence number.
      • Update turn to ‘r’ and set flag to 1.
  3. If it’s receiver’s turn (turn == 'r'):
    • Reduce the time t.
    • Print: “SENDER → Time Reducing”.
    • If t becomes 0:
      • Update turn to ‘s’.
      • Set errorack to 0.
      • Reset t to 5.

Receiver() Function Logic:

  1. Declare a static variable frexp and set it to 1.
  2. Check if it’s the receiver’s turn (turn == 'r'):
    • If errframe is not 0:
      • If the received number (P.seq) matches frexp:
        • Print the received packet, set ack as frexp = P.seq.
        • Increment frexp and set turn to ‘s’.
        • Generate and print an acknowledgement (errorack = 1).
      • If the sequence number doesn’t match:
        • Print a duplicated packet message.
        • Update turn to ‘s’.
        • Set ack to frexp – 1.
        • Generate and print an Error Acknowledgement (errorack).

11) Stop the program.