Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In Python I am trying to run a server and a client seperately and get them to communicate. I have the server running in Terminal

In Python I am trying to run a server and a client seperately and get them to communicate. I have the server running in Terminal on a mac and I am running the client on a seperate instance of terminal. I can't figure out how to get them to communicate. Here is the problem, the client asks me to "Enter the host address:" No matter what I put in there it won't work. I have tried localhost. I have tried the name of my computer, 'Homer.local' I have tried IP addresses. Nothing works. I need you to get it working on your computer and tell me what it wants me to input.

#Server

#!/usr/bin/python import socket import sys

def main(): port = 10000 # Reserve a port for your service. s = socket.socket() # Create a socket object host = socket.gethostname() # Get local machine name #s.bind((host, port)) # Bind to the port s.listen(5) my_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) if len(sys.argv) == 2: port = sys.argv[1] else: port = input("Enter port number:") server_address = ('localhost', int(port)) print(sys.stderr, 'starting up on %s port %s' % server_address) my_socket.bind(server_address) print(host, ' The host is %s ' % host) # I added this line message_count = 0 message = ["Mongo only pawn in the game of life --Mongo",\ "Your mother was a hamster and your father smelt of elderberries! -The Holy Grail French", \ "A common mistake that people make when trying to design something completely foolproof is to underestimate the ingenuity of complete fools. - The Hitchhiker's Guide to the Galaxy", \ "No matter where you go, there you are - Buckaroo Banzai"]

while True: print (sys.stderr, ' waiting to receive message') data, address = my_socket.recvfrom(4096)

print(sys.stderr, 'received %s bytes from %s' % (len(data), address)) print(sys.stderr, data)

if data: sent = my_socket.sendto(message[message_count].encode(), address) message_count += 1 if message_count > len(message) - 1: message_count = 0 print(sys.stderr, 'sent %s bytes back to %s' % (sent, address))

if __name__== "__main__": main()

#Client

#!/usr/bin/python import socket import sys

def main(): my_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) if len(sys.argv) == 3: host = sys.argv[1] port = sys.argv[2] else: host = input("Enter the host address:") port = input("Enter port number:")

if host == 'localhost': host = '127.0.0.1'

dest_address = (host, int(port)) count = 0 rcvdQOTD = False while count < 3: try: print(sys.stderr, 'Sending ') my_socket.settimeout(10) sent = my_socket.sendto(' '.encode(), dest_address)

print(sys.stderr, 'Waiting to receive') data, server = my_socket.recvfrom(4096) my_socket.settimeout(None) if host in server: print(sys.stderr, 'received "%s"' % data) rcvdQOTD = True count = 3 else: print('Wrong server')

except socket.timeout: print ("Timeout from server") count += 1

my_socket.close()

if rcvdQOTD == False: print("Tried to connect once and retried twice. Connection timed out") finished = input("Finished?")

if __name__== "__main__": main()

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Larry Ellison Database Genius Of Oracle

Authors: Craig Peters

1st Edition

0766019748, 978-0766019744

More Books

Students also viewed these Databases questions