クライアント/サーバーを一緒に実行してメッセージを送信しようとすると、どちらも何も受信していないようです。なんで?
client.py:
import socket, sys
import threading
# Client for WutChat #
PORT = 5000
queue = []
def exitClient(s):
s.close()
print "Exiting..."
sys.exit()
def connect(host, port):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try: s.connect((host, port))
except: return "error"
return s
def recieveData(s):
while True:
data = s.recv(512)
if not data: continue
if data in queue:
continue
queue.append(data)
print data
def sendData(s):
while True:
try: data = raw_input(">")
except EOFError: exitClient(s)
if data == "": print "no msg"; continue;
if data.startswith("/"):
print "Command detected"
continue
s.send(data)
if __name__ == "__main__":
s = connect(sys.argv[1], int(sys.argv[2]))
threading.Thread(target=recieveData, args=(s,)).start()
threading.Thread(target=sendData, args=(s,)).start()
server.py:
import socket, sys
import threading
# Server for WutChat #
PORT = 5000
DEBUG = True
conns = {}
msgqueue = []
def connect():
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try: s.bind(("", int(sys.argv[1])))
except IndexError: s.bind(("", PORT))
s.listen(1)
while True:
addr, obj = s.accept()
conns[addr] = obj
print str(addr)+" connected!"
threading.Thread(target=handle, args=(addr, obj, s,)).start()
def handle(addr, obj, s):
while True:
try: msg = obj.recv(512)
except:
continue
print msg
msg = addr + msg
msgqueue.append(msg)
if DEBUG: print msgqueue
for conn in conns:
for message in msgqueue:
conns[conn].send(message)
del message
if __name__ == "__main__":
connect()