Question

In: Computer Science

Write and test a python program to demonstrate TCP server and client connections. The client sends...

Write and test a python program to demonstrate TCP server and client connections. The client sends a Message containing the 10 float numbers (FloatNumber[]) , and then server replies with a message containing maximum, minimum, and average of these numbers.

Solutions

Expert Solution

Server.py

import socket

HOST = '127.0.0.1'  # Standard loopback interface address (localhost)
PORT = 65432        # Port to listen on (non-privileged ports are > 1023)

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.bind((HOST, PORT))
    s.listen()
    conn, addr = s.accept()
    with conn:
        # Recieve data from client.
        data = conn.recv(4096)

         # Decode the data.  
        data_decoded = data.decode("utf-8")
        array_strings = data_decoded.split(" ")
        # Convert the recieved data to float array.
        array = [float(i) for i in array_strings]
        # Find max, min, average of data.
        max_number = max(array)
        min_number = min(array)
        average = (sum(array))/(len(array))

        # Convert result into string and encode it into bytes to send to client.
        result = "Max : " + str(max_number) + " Min: " + str(min_number) + " Average: " + str(average)
        result_encoded = result.encode()
        # Send encoded result to client.
        conn.sendall(result_encoded) 

Client.py

import socket

HOST = '127.0.0.1'  # The server's hostname or IP address
PORT = 65432        # The port used by the server

print("Enter 10 float numbers")

array = [x for x in input().split()]

# Convert the FloatNumber[] to string, encode it and sent it to server.
array_string = " ".join(array)
array_encoded = array_string.encode()

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    # Connect to server.
    s.connect((HOST, PORT))
    # Send data to server.
    s.sendall(array_encoded)
    # Receive data from server.
    data = s.recv(4096)

print(data.decode())

Output -

Start both server and client in two separate terminals.

I have added comments for your understanding

I would love to resolve any queries in the comments. Please consider dropping an upvote to help out a struggling college kid :)

Happy Coding !!


Related Solutions

Write two python codes for a TCP client and a server using a Socket/Server Socket. The...
Write two python codes for a TCP client and a server using a Socket/Server Socket. The client sends a file name to the server. The server checks if there is any integer value in the file content. If there is an integer number, the server will send a message to the client “Integer exists” otherwise, the message “Free of Integers”
A client sends a TCP segment to the server with Sequence Number 1400 and the payload...
A client sends a TCP segment to the server with Sequence Number 1400 and the payload included in the segment is 1399 bytes long. - A. What is the ACK Number in the acknowledgement that is returned from the server? - B. Assume this packet is lost but the following packet is received. What is the ACK Number in the acknowledgement that is returned from the server for this packet? - C. Provide a detailed explanation for part B.
The client connects to the server. The server accepts the connection. The client sends a line...
The client connects to the server. The server accepts the connection. The client sends a line of text containing only SHAKESPEARE_COUNTS. The server sends back a sequence of integers. The number of integers in that list specifies the number of words in each insult; the numbers themselves specify how many possible words there are in each position. So, for example, if you received the output 15 20 30, it would indicate that insults are three words long, with 15 possible...
Greetings, Consider a client server model.The server sends the message 'I am the server' to client....
Greetings, Consider a client server model.The server sends the message 'I am the server' to client. Describe and compare in details how the client and server exchange these messages using internet domain in the following two modes. a) connection-oriented modes b) connectionless-oriented modes
Write a Python Client/Server Socket Program that will allow you to send text messages between the...
Write a Python Client/Server Socket Program that will allow you to send text messages between the server and client, terminating when the exit is typed on the client. Build the program on your 2-VM’s and get it working. Cut and paste your code below along with screen shots of your program working.
Suppose a TCP client needs to send 3 packets to the TCP server. Before sending the...
Suppose a TCP client needs to send 3 packets to the TCP server. Before sending the first packet, the estimated RTT is 50 ms, and the estimated deviation of the sample RTT is 10 ms. The parameters α= 0.1, and β = 0.2. The measured sample RTT for the three packets are 60ms, 70 ms, and 40 ms, respectively. Please compute the time out value that was set for each packet right after it is being transmitted out.
Assume a client uses TCP to send data to a server. The TCP header has 12...
Assume a client uses TCP to send data to a server. The TCP header has 12 bytes of options and 99 bytes of data. [Please don't forget Ethernet Type/Len field is 2 bytes in size.] 1. Calculate the total number of bytes passed to the IP layer by the TCP layer. 2. Calculate the total number of bytes passed to the network layer by the IP layer, assuming the IP layer has no options. 3. Calculate the total number of...
Write a Java program that establishes a TCP connection with a mail server through the socket...
Write a Java program that establishes a TCP connection with a mail server through the socket interface, and sends an email message. Your program sends SMTP commands to local mail server, receives and processes SMTP commands from local mail server. It sends email message to one recipient at a time. The email message contains a typical message header and a message body. Here is a skeleton of the code you'll need to write: import java.io.*; import java.net.*; public class EmailSender...
Write a program to implement a distributed chat server using TCP sockets in ‘C’.
Write a program to implement a distributed chat server using TCP sockets in ‘C’.
How do I make a simple TCP python web client and web server using only "import...
How do I make a simple TCP python web client and web server using only "import socket"? Basically, the client connects to the server, and sends a HTTP GET request for a specific file (like a text file, HTML page, jpeg, png etc), the server checks for the file and sends a copy of the data to the client along with the response headers (like 404 if not found, or 200 if okay etc). The process would be: You first...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT