完成房號,server可接入
This commit is contained in:
parent
57ab28c1d2
commit
9b6edbbc49
61
main.py
61
main.py
@ -1,8 +1,9 @@
|
|||||||
import socket,threading,sys
|
import socket,threading,sys,random,multiprocessing,time
|
||||||
|
|
||||||
MAX = 1024
|
MAX = 1024
|
||||||
|
|
||||||
class Server:
|
class Server:
|
||||||
|
roomList = []
|
||||||
def __init__(self,ip,port):
|
def __init__(self,ip,port):
|
||||||
self.ip = ip
|
self.ip = ip
|
||||||
self.port = port
|
self.port = port
|
||||||
@ -18,18 +19,61 @@ class Server:
|
|||||||
setTypeThread.start()
|
setTypeThread.start()
|
||||||
|
|
||||||
def selectType(self,sock):
|
def selectType(self,sock):
|
||||||
print("SelectType() function started")
|
|
||||||
data = sock.recv(MAX).decode('utf-8')
|
data = sock.recv(MAX).decode('utf-8')
|
||||||
|
|
||||||
print(data)
|
print(data)
|
||||||
if data == "MAIN":
|
if data == "MAIN":
|
||||||
sock.send("OK.SERVER".encode('utf-8'))
|
sock.send("OK.SERVER".encode('utf-8'))
|
||||||
|
self.main(sock)
|
||||||
elif data == "CLIENT":
|
elif data == "CLIENT":
|
||||||
sock.send("OK.CLIENT".encode('utf-8'))
|
sock.send("OK.CLIENT".encode('utf-8'))
|
||||||
else:
|
else:
|
||||||
sock.send("FAIL".encode('utf-8'))
|
sock.send("FAIL".encode('utf-8'))
|
||||||
|
|
||||||
print("SelectType() function closed")
|
def main(self,sock):
|
||||||
|
portNum = random.randint(1025,65535) #決定port
|
||||||
|
roomNum = chr(random.randint(65,90)) + chr(random.randint(65,90))
|
||||||
|
|
||||||
|
flag = 1
|
||||||
|
while flag:
|
||||||
|
portNum = random.randint(1025,65535) #決定port
|
||||||
|
roomNum = chr(random.randint(65,90)) + chr(random.randint(65,90))
|
||||||
|
flag = 0
|
||||||
|
for i in self.roomList:
|
||||||
|
if i['portNum'] == portNum or i['roomNum'] == roomNum:
|
||||||
|
flag =1
|
||||||
|
room = Room(self.ip,portNum)
|
||||||
|
roomProcess = multiprocessing.Process(target=room.start)
|
||||||
|
roomProcess.start()
|
||||||
|
self.roomList.append(
|
||||||
|
{
|
||||||
|
'portNum' : portNum,
|
||||||
|
'roomNum' : roomNum,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
print(self.roomList)
|
||||||
|
sock.send("{} {}".format(roomNum,portNum).encode('utf-8'))
|
||||||
|
# def getPort(self):
|
||||||
|
# sock
|
||||||
|
class Room:
|
||||||
|
startFlag = False
|
||||||
|
def __init__(self,ip,portNum):
|
||||||
|
self.ip = ip
|
||||||
|
self.portNum = portNum
|
||||||
|
def start(self):
|
||||||
|
listensock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
|
||||||
|
listensock.bind((self.ip,self.portNum))
|
||||||
|
print("\t{}:{}".format(self.ip,self.portNum))
|
||||||
|
listensock.listen(5)
|
||||||
|
while True:
|
||||||
|
sock,sockname = listensock.accept()
|
||||||
|
print("[ {} ]{} has connected.".format(self.portNum,sockname))
|
||||||
|
sock.send('WELCOME'.encode('utf-8'))
|
||||||
|
def connect(self):
|
||||||
|
time.sleep(0.5)
|
||||||
|
sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
|
||||||
|
sock.connect((self.ip,self.portNum))
|
||||||
|
print(sock.recv(MAX).decode('utf-8'))
|
||||||
|
|
||||||
class Client:
|
class Client:
|
||||||
def __init__(self,ip,port):
|
def __init__(self,ip,port):
|
||||||
@ -46,7 +90,16 @@ class Client:
|
|||||||
msg = input("> ")
|
msg = input("> ")
|
||||||
sock.send(msg.encode('utf-8'))
|
sock.send(msg.encode('utf-8'))
|
||||||
receiveMsg = sock.recv(MAX).decode('utf-8')
|
receiveMsg = sock.recv(MAX).decode('utf-8')
|
||||||
print(receiveMsg)
|
if receiveMsg=="OK.SERVER":
|
||||||
|
receiveMsg = sock.recv(MAX).decode('utf-8')
|
||||||
|
print(receiveMsg)
|
||||||
|
connectData = receiveMsg.split(' ')
|
||||||
|
room = Room(self.ip,int(connectData[1]))
|
||||||
|
room.connect()
|
||||||
|
elif receiveMsg=="OK.CLIENT":
|
||||||
|
print("CLIENT")
|
||||||
|
else:
|
||||||
|
print("ERROR TYPE")
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
ip = sys.argv[2]
|
ip = sys.argv[2]
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user