Skip to content

Instantly share code, notes, and snippets.

@johntiger1
Forked from anonymous/auto.py
Created October 25, 2017 19:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johntiger1/1983baca2a8847571b23b432b30eb146 to your computer and use it in GitHub Desktop.
Save johntiger1/1983baca2a8847571b23b432b30eb146 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()
@johntiger1
Copy link
Author

this mi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment