-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayerclient.py
51 lines (35 loc) · 1.03 KB
/
playerclient.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import socket
import threading
import pygame
HEADER=64
PORT=5050
SERVER=socket.gethostbyname(socket.gethostname())
ADDR=(SERVER, PORT)
FORMAT='utf-8'
clients=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
clients.connect(ADDR)
nickname=input("Enter your name: ")
def recieve():
while True:
try:
msg=clients.recv(1024).decode(FORMAT)
if msg=='Enter your gamename: ':
clients.send(nickname.encode(FORMAT))
print('You joined the server.')
else:
print(msg)
except:
print('Error happened')
clients.close()
break
def send():
while True:
typemsg=input()
msg_len=len(f'{nickname}: {typemsg}')
send_len=str(msg_len).encode(FORMAT)
clients.send(send_len)
clients.send(f'{nickname}: {typemsg}'.encode(FORMAT))
recievethread=threading.Thread(target=recieve)
recievethread.start()
typemsgthread=threading.Thread(target=send)
typemsgthread.start()