-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat_client_ec.py
More file actions
103 lines (89 loc) · 2.99 KB
/
chat_client_ec.py
File metadata and controls
103 lines (89 loc) · 2.99 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
#!env python
"""Chat client for CST311 Programming Assignment 3"""
__author__ = "Team 2"
__credits__ = [
"Henry Garkanian",
"Ivan Soria",
"Kyle Stefun",
"Bryan Zanoli"
]
# Import statements.
import socket as s
import _thread
import threading
import time
# Configure logging.
import logging
logging.basicConfig()
log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG)
# Set global variables.
server_name = "10.0.0.1"
server_port = 12000
complete = 0
def client_input(client_socket):
try:
# Get input from user
while True:
user_input = input()
# Set data across socket to server.
# Note: encode() converts the string to UTF-8 for transmission.
client_socket.send(user_input.encode())
if (user_input == 'bye'):
complete = 1
break
finally:
print()
def client_receive(client_socket):
try:
while True:
# Read response from server.
server_response = client_socket.recv(1024)
# Decode server response from UTF-8 bytestream.
server_response_decoded = server_response.decode()
# Print output from server - split on null character to avoid concatenation.
split_response = server_response_decoded.strip('\0').split('\0')
for x in split_response:
print(x)
except OSError as e:
log.info("Client socket no longer open")
finally:
print()
def input_clientname(client_socket):
print("Welcome to the chat! Please enter your username:")
user_input = input()
# Send client name to server.
client_socket.send(user_input.encode())
# Get server response.
server_response = client_socket.recv(1024)
# If returned name from server does not match, log an error.
if(user_input != server_response.decode()):
log.error("username could not be set")
def main():
# Create socket.
client_socket = s.socket(s.AF_INET, s.SOCK_STREAM)
try:
# Establish TCP connection.
client_socket.connect((server_name,server_port))
except Exception as e:
log.exception(e)
log.error("***Advice:***")
if isinstance(e, s.gaierror):
log.error("\tCheck that server_name and server_port are set correctly.")
elif isinstance(e, ConnectionRefusedError):
log.error("\tCheck that server is running and the address is correct")
else:
log.error("\tNo specific advice, please contact teaching staff and include text of error and code.")
exit(8)
# Get input from user.
input_clientname(client_socket)
_thread.start_new_thread(client_input, (client_socket,))
_thread.start_new_thread(client_receive, (client_socket,))
while True:
if (complete == 1):
break
time.sleep(.5)
client_socket.close()
# This helps shield code from running when we import the module.
if __name__ == "__main__":
main()