-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClient.py
49 lines (40 loc) · 1.53 KB
/
Client.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
#importing stuffs -- sockets and threads
#connecting to the server
#listening the broadcast and sending your name to the server to join
#receiving messages form server
#sending messages to the server
#Finally to work with everything -- we'r starting the threads to read and write
import socket
import threading
# Choosing Nickname
nickname = input("Name: ")
# Connecting To Server
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(('127.0.0.1', 12345)) #Instead of connecting, we'r binding to the host's port(Port Number is Confidential)
# Listening to Server and Sending Nickname
def receive():
while True:
try:
# Receive Message From Server
# If 'NICK' Send Nickname
message = client.recv(1024).decode('ascii')
#cheching the message which is decoded to NICK and if yes were moving up!
if message == 'NICK':
client.send(nickname.encode('ascii'))
else:
print(message)
except:
# Close Connection When Error
print("An error occured!")
client.close()
break
# Sending Messages To Server
def write():
while True:
message = '{}: {}'.format(nickname, input(''))
client.send(message.encode('ascii'))
# Starting Threads For Listening And Writing
receive_thread = threading.Thread(target=receive)
receive_thread.start()
write_thread = threading.Thread(target=write)
write_thread.start()