Skip to content

Instantly share code, notes, and snippets.

Created October 25, 2017 14:53
Show Gist options
  • Save anonymous/f80cbc20c8274053ffb3e599fd8af0a9 to your computer and use it in GitHub Desktop.
Save anonymous/f80cbc20c8274053ffb3e599fd8af0a9 to your computer and use it in GitHub Desktop.
#Run this to spawn the server and clients
import os
os.system("start python serverTCP.py")
os.system("start python clientTCP.py")
os.system("start python clientTCP.py")
import socket
from thread import *
s = socket.socket()
host = socket.gethostname()
port = 12345
s.connect((host, port))
def recieve(conn):
while True:
message = conn.recv(2048)
print message
start_new_thread(recieve, (s,))
while True:
messageToSend = raw_input()
s.send(messageToSend)
s.close()
import socket
import random
from thread import *
s = socket.socket()
host = socket.gethostname()
port = 12345
s.bind((host, port))
s.listen(2)
clientConnList = []
wordList = ["University", "Of", "The", "Philippines", "Visayas", "Miagao", "Iloilo", "Region", "Six", "Southeast", "Asia", "Eastern", "Hemisphere", "Earth", "Planet", "Solar", "System", "Galaxy", "Universe"]
currentWord = wordList[random.randint(0, len(wordList)-1)]
print "Current word is " + currentWord
def listenToClient(conn, addr):
global currentWord, wordList, clientConnList
conn.send("Connecting....\nConnection Established\n\nYou are player " + str(len(clientConnList)))
while True:
try:
message = conn.recv(2048)
if(currentWord.upper() == message.upper()):
currentWord = wordList[random.randint(0, len(wordList)-1)]
print "New word is " + currentWord
for player in clientConnList:
if(player[0] == addr):
print "Found the player!"
player[1] += 1 #The problem is here
score = player[1]
message = "Congratulations! You got it right! Score: " + str(score)
else:
message = "Nope. Try again"
conn.send(message)
except:
continue
while True:
c, addr = s.accept()
print 'Got connection from', addr
clientConnList.append((addr, 0))
start_new_thread(listenToClient, (c, addr))
s.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment