-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.py
More file actions
122 lines (100 loc) · 4.6 KB
/
Client.py
File metadata and controls
122 lines (100 loc) · 4.6 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import socket, select, string, sys
import msvcrt
def prompt() :
sys.stdout.write('<You> ')
sys.stdout.flush()
def run_client(buffer=1024):
# Require the passing of server address and port for connection
assert len(sys.argv) == 3, "Server address and port are required for connection"
ADDRESS = sys.argv[1]
PORT = int(sys.argv[2])
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(2)
# The client attempts to connect to the sever
try :
s.connect((ADDRESS, PORT))
except :
print(f'Connection Not Established on PORT: {PORT}, ADDRESS: {ADDRESS}\n Exiting Client')
sys.exit()
# Client prompts user for a name to be used in the chat room
client_name = "Name:" + input("Client name: ")
# Communication is sent to the server to indicate the user's name
s.send(client_name.encode())
print(f'Connection established to {ADDRESS} on port {PORT}')
prompt()
# Run client until interruption or disconnection
while 1:
sockets = [s]
# Find compatible sockets for communications
read_sockets, write_sockets, error_sockets = select.select(sockets , [], [], 1)
# Check for any user keyboard input indicating a message
# In case of input, append this input onto the read sockets
if msvcrt.kbhit():
read_sockets.append(sys.stdin)
# Iterate over sockets checking for incoming and outgoing messages
for sock in read_sockets:
# Data from socket connected to server indicates an incoming communication
if sock == s:
data = sock.recv(buffer).decode()
if not data :
# No data indicates a missed handshake with server signalling a disconnection
print('\nConnection to server lost')
sys.exit()
else :
# Other data indicates a message, this is decoded and printed
sys.stdout.write(data)
prompt()
# Otherwise, the next input is that of the user, this is then endoced and sent to the server
else :
msg = sys.stdin.readline()
s.send(msg.encode())
prompt()
#main function
if __name__ == "__main__":
buffer = 1024
# Require the passing of server address and port for connection
assert len(sys.argv) == 3, "Server address and port are required for connection"
ADDRESS = sys.argv[1]
PORT = int(sys.argv[2])
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(2)
# The client attempts to connect to the sever
try :
s.connect((ADDRESS, PORT))
except :
print(f'Connection Not Established on PORT: {PORT}, ADDRESS: {ADDRESS}\n Exiting Client')
sys.exit()
# Client prompts user for a name to be used in the chat room
client_name = "Name:" + input("Client name: ")
# Communication is sent to the server to indicate the user's name
s.send(client_name.encode())
print(f'Connection established to {ADDRESS} on port {PORT}')
prompt()
# Run client until interruption or disconnection
while 1:
sockets = [s]
# Find compatible sockets for communications
read_sockets, write_sockets, error_sockets = select.select(sockets , [], [], 1)
# Check for any user keyboard input indicating a message
# In case of input, append this input onto the read sockets
if msvcrt.kbhit():
read_sockets.append(sys.stdin)
# Iterate over sockets checking for incoming and outgoing messages
for sock in read_sockets:
# Data from socket connected to server indicates an incoming communication
if sock == s:
data = sock.recv(buffer).decode()
print(data)
if not data :
# No data indicates a missed handshake with server signalling a disconnection
print('\nConnection to server lost')
sys.exit()
else :
# Other data indicates a message, this is decoded and printed
sys.stdout.write(data)
prompt()
# Otherwise, the next input is that of the user, this is then endoced and sent to the server
else :
msg = sys.stdin.readline()
s.send(msg.encode())
prompt()