Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In the following Python code tell me, is this using UDP or TCP? Explain how to t

ID: 3706920 • Letter: I

Question

In the following Python code tell me, is this using UDP or TCP? Explain how to tell the difference?

#Write a TCP echo server
import socket

#Request port number
portReq = int(input("Please insert the port number: "))

#In the TCP Echo server, we create a socket and bind to a advertized port number.
sock = socket.socket()

#Establish a variable to hold the server address and port number we are binding to
bound_addr = ('localhost', portReq)

#Bind the socket to the address
sock.bind(bound_addr)

#After binding , the process listens for incoming connections, listen() enables server to accept connections
sock.listen(5)


#Then an infinite loop is started to process the client requests for connections.
while 1:

    #After a connection is requested , it accepts the connection from the client machine and forks a new process.
   new_sock, conn_addr = sock.accept()
  
    #Establish message variable
   message = ''
  
    #Receive data until the client closes the connection
   while 1:

        #The new process receives data from the client using recv() function and echoes the same data using the send() function.
       data = new_sock.recv(4096)
      
        #If no data is sent back, then break out of the loop
       if not data:
            break
        #Decode the data sent back and put in into our message variable
       message += data.decode('ascii')
      
    #Convert to uppercase - just to change the message up a little
   response = 'To live is to suffer, to survive is to find some meaning in the suffering - Friedrich Nietzsche'
   #message.upper()
  
    #Encode and send back the message to the client (echo the message)
   new_sock.send(response.encode('ascii'))
  
    #Close the forked process
   new_sock.close()

#Release the resources and mark socket as closed
sock.close()

Explanation / Answer

To determine whether it is using UDP or TCP let's find out the properties of both ,

In UDP :

Echo Server:

Since there is no connection , the server does not need to listen for and accept connections. It only needs to use bind() to associate its socket with a port, and then wait for individual messages.

Messages are read from the socket using recvfrom(), which returns the data as well as the address of the client from which it was sent .

Echo Client :

The UDP echo client is similar the server, but does not use bind() to attach its socket to an address. It uses sendto() to deliver its message directly to the server, and recvfrom() to receive the response.

Now , In TCP :

Echo Server :

It receives incoming messages and echos them back to the sender. It starts by creating a TCP/IP socket.

Then bind() is used to associate the socket with the server address.

Calling listen() puts the socket into server mode, and accept() waits for an incoming connection.

accept() returns an open connection between the server and client, along with the address of the client. The connection is actually a different socket on another port (assigned by the kernel). Data is read from the connection with recv() and transmitted with send().

When communication with a client is finished, the connection needs to be cleaned up using close() .

Echo Client :

The client program sets up its socket differently from the way a server does. Instead of binding to a port and listening, it uses connect() to attach the socket directly to the remote address.

After the connection is established, data can be sent through the socket with send() and received with recv(), just as in the server.

When the entire message is sent and a copy received, the socket is closed to free up the port.

So it is clear that in the given question recv() and send() functions are used to recieve and send data which is an important characteristic of " TCP " , hence it uses TCP .

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote