In: Computer Science
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:
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!!
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.