Question

In: Computer Science

Program description is given below!! Client and Server Online Game Jumble: In this assignment, you need...

Program description is given below!!

Client and Server Online Game Jumble: In this assignment, you need to use socket programming to implement an online game Jumble. Generate a random work in server and send it to client and ask for input and then client send the guessed word to server and server check whether its in a word.txt file and reply Win or lose.

Note:

  • Reading content of a text file to a list and generating random number, and game logic is already implemented in jumble.py
  • ONLY need modification in echo-client.py and thread-server.py to implement socket programming.

Test your code and make sure the server can serve multiple players simultaneously. Note the words should be generated independently for each client, so that normally different clients are guessing different words

-----------------------Code--------------------

"""-----------------thread-server.py----------------------
Server side: open a socket on a port, listen for a message from a client,
and send an echo reply; echoes lines until eof when client closes socket;
spawns a thread to handle each client connection; threads share global
memory space with main thread; this is more portable than fork: threads
work on standard Windows systems, but process forks do not;
"""

import time, _thread as thread # or use threading.Thread().start()
from socket import * # get socket constructor and constants
myHost = '' # server machine, '' means local host
myPort = 50007 # listen on a non-reserved port number

sockobj = socket(AF_INET, SOCK_STREAM) # make a TCP socket object
sockobj.bind((myHost, myPort)) # bind it to server port number
sockobj.listen(5) # allow up to 5 pending connects

def now():
return time.ctime(time.time()) # current time on the server

def handleClient(connection): # in spawned thread: reply
time.sleep(5) # simulate a blocking activity
while True: # read, write a client socket
data = connection.recv(1024)
if not data: break
reply = 'Echo=>%s at %s' % (data, now())
connection.send(reply.encode())
connection.close()

def dispatcher(): # listen until process killed
while True: # wait for next connection,
connection, address = sockobj.accept() # pass to thread for service
print('Server connected by', address, end=' ')
print('at', now())
thread.start_new_thread(handleClient, (connection,))

dispatcher()

"""------------------echo-client.py-------------------------
Client side: use sockets to send data to the server, and print server's
reply to each message line; 'localhost' means that the server is running
on the same machine as the client, which lets us test client and server
on one machine; to test over the Internet, run a server on a remote
machine, and set serverHost or argv[1] to machine's domain name or IP addr;
Python sockets are a portable BSD socket interface, with object methods
for the standard socket calls available in the system's C library;
"""

import sys
from socket import * # portable socket interface plus constants
serverHost = 'localhost' # server name, or: 'starship.python.net'
serverPort = 50007 # non-reserved port used by the server

message = [b'Hello network world'] # default text to send to server
# requires bytes: b'' or str,encode()
if len(sys.argv) > 1:   
serverHost = sys.argv[1] # server from cmd line arg 1
if len(sys.argv) > 2: # text from cmd line args 2..n
message = (x.encode() for x in sys.argv[2:])

sockobj = socket(AF_INET, SOCK_STREAM) # make a TCP/IP socket object
sockobj.connect((serverHost, serverPort)) # connect to server machine + port

for line in message:
sockobj.send(line) # send line to server over socket
data = sockobj.recv(1024) # receive line from server: up to 1k
print('Client received:', data) # bytes are quoted, was `x`, repr(x)

sockobj.close() # close socket to send eof to server

--------------------------jumble.py----------------------

import random
F = open('wordlist.txt')
words = F.readlines()
F.close()
while True:
word = words[random.randrange(len(words))]
while len(word) > 5 or len(word) == 0:
word = words[random.randrange(0, len(words))]
word = word.rstrip()
old_word = word
word = list(word)
while word:
print(word.pop(random.randrange(len(word))), end = ' ')
print('\nType your answer')
match_word = input()
new_word = match_word + '\n'
if new_word in words and set(match_word) == set(old_word):
print('You win.')
else:
print('The answer is ' + old_word)

-------------------wordlist.txt---------------------

ab
aba
abaca
abacist
aback
abactinal
abacus
abaddon
abaft
abalienate
abalienation
abalone
abampere
abandon
abandoned
abandonment
abarticulation
abase
abased
abasement
abash
abashed
abashment
abasia
abasic
abate
abatement
abating
abatis
abatjour
abattis
abattoir
abaxial
abba

------------------------------------------------

Thank in advance! Please help me soon. I will UPVOTE!!

Solutions

Expert Solution

According to the question the code for python files client.py and server.py is attached below with socket programming implemented along with screenshots of running code and examples.

1) server.py

import time, _thread as thread # or use threading.Thread().start()
from socket import * # get socket constructor and constants
import random
# socket = socket.socket()
myHost = '127.0.0.1' # server machine, '' means local host
myPort = 50007 # listen on a non-reserved port number

sockobj = socket(AF_INET, SOCK_STREAM) # make a TCP socket object
sockobj.bind((myHost, myPort)) # bind it to server port number
sockobj.listen(5) # allow up to 5 pending connects

F = open('wordlist.txt')
words = F.read()
F.close()


def now():
    return time.ctime(time.time()) # current time on the server

def handleClient(connection): # in spawned thread: reply
    time.sleep(5) # simulate a blocking activity
    while True: # read, write a client socket
        data = connection.recv(1024).decode()
        print(data)
        
        if data in words:
            reply = "you win"        
            connection.send(reply.encode())
        else: 
            reply = "you lose"
            connection.send(reply.encode())
    if not data:
        return 0
    connection.close()

def dispatcher(): # listen until process killed
    while True: # wait for next connection,
        connection, address = sockobj.accept() # pass to thread for service
        print('Server connected by', address, end=' ')
        print('at', now())
        thread.start_new_thread(handleClient, (connection,))

dispatcher()

2) client.py

import sys
from socket import * # portable socket interface plus constants
serverHost = 'localhost' # server name, or: 'starship.python.net'
serverPort = 50007 # non-reserved port used by the server

# message = [b'Hello network world'] # default text to send to server
message = input('Enter the word')
# message = newmessage.encode()
print(message)
# requires bytes: b'' or str,encode()
if len(sys.argv) > 1:   
    serverHost = sys.argv[1] # server from cmd line arg 1
if len(sys.argv) > 2: # text from cmd line args 2..n
    message = (x.encode() for x in sys.argv[2:])

sockobj = socket(AF_INET, SOCK_STREAM) # make a TCP/IP socket object
sockobj.connect((serverHost, serverPort)) # connect to server machine + port

# for line in message:
sockobj.send(message.encode()) # send line to server over socket
data = sockobj.recv(1024) # receive line from server: up to 1k
print('Client received:', data.decode()) # bytes are quoted, was `x`, repr(x)

sockobj.close() # close socket to send eof to server

SOME SCREENSHOTS OF WORKING EXAMPLES:

Multiple clients can send input to the server and the server checks if the word exists in the file and responds with answer you win or you lose.


Related Solutions

For this assignment you are to write both the server and client applications for a Knock-Knock...
For this assignment you are to write both the server and client applications for a Knock-Knock joke system based on the Java TCP socket client-server example that has been discussed in lectures. The joke protocol goes like this: Client: "Tell me a joke." Server: "Knock knock!" Client: "Who's there?" Server: "Witches." Client: "Witches who?" Server: "Witches the way to the store." Client: "Groan." The Client The client code should connect to the joke server and allow the user to type...
Develop a client /server talk application in C or C++. You should run your server program...
Develop a client /server talk application in C or C++. You should run your server program first, and then open another window if you use loopback to connect the local host again, or you may go to another machine to run the client program that will connect to the server program. In this case you must find the server’s IP address first, and then connect to it. If you use loopback, your procedure may look like: ➢ Server Server is...
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.
You need to design a Web Server, Database Server and a Backup server. If you had...
You need to design a Web Server, Database Server and a Backup server. If you had to choose from the following list of resources which ones would you place a priority on and state why you would do so. List these for each server type. Hint: You need to think about the functionality of the server. Based on this information, which resource would you emphasize on the most to increase the performance of the server. CPU utilization and speed Multiprocessing...
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.
Client AND server using names pipes (mkfifo) in C/C++ Write and client program that will talk...
Client AND server using names pipes (mkfifo) in C/C++ Write and client program that will talk to a server program in two separate terminals. Write the server program that can handle multiple clients (so threads will be needed) and with fork() and exec()
You are asking to develop a “Triangle Guessing” game in Python for the assignment. The program...
You are asking to develop a “Triangle Guessing” game in Python for the assignment. The program accepts the lengths of three (3) sides of a triangle as input from a player. The program output should indicate whether the triangle is a right triangle, an acute triangle, or an obtuse triangle. 1.Tips to help you for this assignment: Make sure each side of the triangle is a positive integer. Use try/except for none positive integer input. 2. Validating the triangle. That...
To complete each assignment, you will need to read the case study. Assume that the client...
To complete each assignment, you will need to read the case study. Assume that the client described in each case study meets criteria for a DSM-5 disorder. You will then consult the DSM-5 criteria and write 1-2 detailed paragraphs in which you assign a primary diagnosis for the client and justify that diagnosis. In this section you should reference EACH of the DSM-5 criteria for the diagnosis you are assigning and provide evidence from the case study for or against...
To complete each assignment, you will need to read the case study. Assume that the client...
To complete each assignment, you will need to read the case study. Assume that the client described in each case study meets criteria for a DSM-5 disorder. You will then consult the DSM-5 criteria and write 1-2 detailed paragraphs in which you assign a primary diagnosis for the client and justify that diagnosis. In this section you should reference EACH of the DSM-5 criteria for the diagnosis you are assigning and provide evidence from the case study for or against...
To complete each assignment, you will need to read the case study. Assume that the client...
To complete each assignment, you will need to read the case study. Assume that the client described in each case study meets criteria for a DSM-5 disorder. You will then consult the DSM-5 criteria and write 1-2 detailed paragraphs in which you assign a primary diagnosis for the client and justify that diagnosis. In this section you should reference EACH of the DSM-5 criteria for the diagnosis you are assigning and provide evidence from the case study for or against...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT