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. I need to know the correct host address, port number and what I am doing wrong.

#Server

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

def main(): 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) 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, '/nwaiting 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

Advances In Knowledge Discovery In Databases

Authors: Animesh Adhikari, Jhimli Adhikari

1st Edition

3319132121, 9783319132129

More Books

Students also viewed these Databases questions

Question

8. Provide recommendations for how to manage knowledge.

Answered: 1 week ago