Network Protocols Implementation Steps: FTP, TCP, UDP
FTP Server Implementation Steps
- Start the program.
- Include necessary header files.
- Declare necessary variables.
- Print: “Enter the port address”.
- 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.
- Setup server address structure (Family to IPv4 (‘AF_INET’), IP address to any available address (‘INADDR_ANY’)).
- Convert the port number to network byte order.
- Bind the socket to the server address.
- Check if binding was successful. If not, print an error message.
- Listen for incoming connections on the socket, allowing up to 5 pending connections.
- Accept a client connection and create a new socket (‘newsd’) for communication.
- Check if connection was successful. If not, print an error message.
- Receive the file name to be transferred from the client.
- Open the requested file in read mode (‘r’).
- If the file doesn’t exist, send an error message to the client and close the connection.
- Read the file line by line using
fgets(). - Send each line of the file to the client.
- If an error occurs while sending, print an error message.
- After sending all file contents, send a completion message to the client.
- Close the connection with the client.
- 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:
- Start.
- Include necessary header files.
- Start main function and declare necessary variables.
- Create a socket using
socket()function with domain ‘AF_INET’, type ‘SOCK_STREAM’, and protocol ‘0’. - Use
bzero()function to clear the memory allocated for ‘serveraddr’. - Set server address structure (Family to ‘AF_INET’, port number based on user input, and IP address to ‘INADDR_ANY’).
- Bind the socket to the server address structure using
bind()function. - Clear the memory allocated for ‘clientaddr’.
- Use
listen()function to listen for incoming connections on the server. - Wait for and accept a connection from a client using
accept()function. This function will block until a client connects. - Read data sent by the client into the ‘message’ buffer using
read(); Print the message received from the client. - Send a response message to the client using
write()function. - Close the client socket using
close()function. - Close the server socket using
close()function. - Stop.
TCP Client Algorithm
Steps:
- Start the program.
- Include necessary header files.
- Start main function and declare required variables.
- Create client socket with domain ‘AF_INET’, type ‘SOCK_STREAM’ and protocol ‘0’.
- Use
bzero()function to clear memory allocated for ‘serveraddr’. - Set the family in the server address structure to ‘AF_INET’.
- Prompt the user to enter the port number and store it in the variable ‘port’.
- Convert port number from host to network byte order using
htons()function. - 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. - Send a message confirming the connection to the server.
- Send a message “Hi, I am client.” to the server using
send()function. - Receive message from the server and store it in the ‘message’ buffer.
- Print a message indicating the reception of the message.
- Print the received message.
- Close the client socket using
close()function. - Stop.
UDP Server Algorithm
Steps:
- Start.
- Include necessary header files.
- Start main function and declare necessary variables.
- Create a socket with domain ‘AF_INET’, type ‘SOCK_DGRAM’ (indicating UDP), and protocol ‘0’.
- Use
bzero()function to clear the memory allocated for ‘serveraddr’. - Set the server address structure (Family to ‘AF_INET’, port number based on user input, and IP address to ‘INADDR_ANY’).
- Bind the socket to the server address structure using
bind(). - Wait to receive a message from the client using
recvfrom()function. This function blocks until a message is received. - Print the message received from the client.
- Send a response message “YOUR MESSAGE RECEIVED” to the client using
sendto()function. - Close the server socket using
close()function. - Stop.
UDP Client Algorithm
Steps:
- Start.
- Include necessary header files.
- Start main function and declare necessary variables.
- Create a client socket with domain ‘AF_INET’, type ‘SOCK_DGRAM’ (indicating UDP) and protocol ‘0’.
- Use
bzero()function to clear the memory allocated for ‘serveraddr’. - Set the family in the server address structure to ‘AF_INET’.
- Prompt the user to enter the port number and store it in the variable ‘port’.
- Convert port number to network byte order using
htons()function. - Read a message from the user using
gets()function and store it in the ‘message’ buffer. - Send the message “HI I AM CLIENT…” to the server using
sendto(). - Print a message indicating that a message has been sent to the server.
- 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. - Print the received message using
printf(). - Close the client socket using
close()function. - Stop.
Sender-Receiver Algorithm Simulation
- Start the program.
- Create a structure
Framewith 2 integers: Pinfo and seq. - Declare necessary variables.
- Declare a structure variable
P. - Initialize disconnect to 0 and turn to ‘s’.
- Set errframe and errorack to 1.
- Initialize
P.PinfoandP.seqto 0. - Run a loop until disconnect is true:
- Call
sender()function. - Run a for loop with k incrementing from 1 to 10000000:
- Call
receiver()function.
- Call
- Call
Sender() Function Logic:
- Declare a static variable flag and set it to 0.
- 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’.
- Print and send a packet with sequence number
- 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.PinfoandP.seq. - Print and send packet with new sequence number.
- Update turn to ‘r’ and set flag to 1.
- Check
- If errorack is 0:
- 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:
- Declare a static variable frexp and set it to 1.
- 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).
- Print the received packet, set ack as
- 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).
- If the received number (
- If errframe is not 0:
11) Stop the program.
