-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
273 lines (249 loc) · 9.37 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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
"""
Python 3
Usage: python3 TCPClient3.py localhost 12000
coding: utf-8
Author: Wei Song (Tutor for COMP3331/9331)
"""
import os
import threading
import time
from socket import *
import sys
import random
import help
from threading import Thread
lock = threading.Lock()
privateMessage = False
privateChatSocket = {}
privateContact = []
existedPort = []
user1 = ""
user2 = ""
host = ""
port = ""
class InputThread(Thread):
def __init__(self, clientsocket, userName):
Thread.__init__(self)
self.clientsocket = clientsocket
self.username = userName
def run(self):
global lock
# with lock:
# clientSocket.send("broadcast join the chat\n".encode())
while 1:
message = input(
"===== Please type any messsage you want to send to server: =====\n")
for word in message.split(" "):
if not help.checkText(word):
print("invalid format")
continue
words = message.split(" ")
# print(words)
if words[0] == "private":
print("if words[0] == 'private':")
if len(words) < 3:
print("[error], private <user> <message> ")
continue
targetuser = words[1]
if targetuser not in privateContact:
print("[error] please startprivate first")
outMessage = ""
for i in range(2, len(words)):
outMessage = outMessage + " " + words[i]
# find the corresponding socket
try:
targetSocket = privateChatSocket[targetuser]
print(targetSocket)
with lock:
targetSocket.send(
f"[{self.username}][private] {outMessage}".encode())
print("send successfully")
except:
print("[error] don't have p2p connection")
continue
elif words[0] == "stopprivate":
if len(words) < 2:
print("[error], stopprivate <user>")
continue
targetuser = words[1]
if targetuser not in privateContact:
print(f"[error], no p2p with {targetuser}")
else:
privateChatSocket.pop(targetuser, None)
privateContact.remove(targetuser)
print("[stop private] success")
# receive the notice from the receive thread
# give the server port number
global privateMessage
global user1
global user2
while (privateMessage):
if ("yes") in message:
with lock:
self.clientsocket.send(
f"[responseY] {user1} {user2} {host}".encode())
time.sleep(0.1)
# privateContact.append(user1)
privateMessage = False
elif ("no") in message:
with lock:
self.clientsocket.send("[responseN]".encode())
time.sleep(0.1)
privateMessage = False
else:
message = input("====please type yes or no====\n")
time.sleep(0.1)
with lock:
self.clientsocket.sendall(message.encode())
time.sleep(0.1)
class ReceiveServer(Thread):
def __init__(self, clientsocket):
Thread.__init__(self)
self.clientsocket = clientsocket
def run(self):
while 1:
try:
data = clientSocket.recv(1024)
except:
print("sorry, you have timeout")
os._exit(1)
receivedMessage = data.decode()
print(receivedMessage + "\n")
# parse the message received from server and take corresponding actions
if receivedMessage == "":
print("[recv] Message from server is empty!")
elif receivedMessage == "successfully logout":
clientSocket.close()
os._exit(1)
elif receivedMessage == "sorry you are timeout":
clientSocket.close()
os._exit(1)
# get the first request
# tell the input thread
elif "[private request]" in receivedMessage:
# reached
print("========input yes or no========")
global privateMessage
privateMessage = True
global user1
global user2
global host
# global port
messageWords = receivedMessage.split(" ")
index = messageWords.index("request]")
user1 = messageWords[index + 1]
host = messageWords[index + 2]
user2 = messageWords[index + 4]
elif "[portCreate]" in receivedMessage:
messageWords = receivedMessage.split(" ")
index = messageWords.index("[portCreate]")
privateport = messageWords[index + 1]
privatehost = messageWords[index + 2]
targetUserName = messageWords[index + 3]
privateContact.append(targetUserName)
# TODO
privateSocket = socket(AF_INET, SOCK_STREAM)
address = (privatehost, int(privateport))
# print(address)
privateSocket.bind(address)
privateSocket.listen(1)
csocket, clientAddress = privateSocket.accept()
privateChatSocket[targetUserName] = csocket
# while 1:
# data = csocket.recv(1024).decode()
# print(data)
newThread = PrivateReceiveServer(csocket)
newThread.start()
elif "[portConnect]" in receivedMessage:
time.sleep(0.1)
messageWords = receivedMessage.split(" ")
index = messageWords.index("[portConnect]")
privateport = messageWords[index + 1]
privatehost = messageWords[index + 2]
targetUserName = messageWords[index + 3]
privateContact.append(targetUserName)
privateSocket = socket(AF_INET, SOCK_STREAM)
address = (privatehost, int(privateport))
# print(address)
privateSocket.connect(address)
privateChatSocket[targetUserName] = privateSocket
# while 1:
# data = privateSocket.recv(1024).decode()
# print(data)
newThread = PrivateReceiveServer(privateSocket)
newThread.start()
elif "[block]" in receivedMessage:
os._exit(1)
# privateChatAddress[privateUserName] = priva
# delete the duplicate
# print(privateChatAddress)
# else:
# print(receivedMessage)
class PrivateReceiveServer(Thread):
def __init__(self, privateSocket):
Thread.__init__(self)
self.privateSocket = privateSocket
def run(self):
while 1:
try:
data = self.privateSocket.recv(1024).decode()
print(data)
except:
print("the source socket is timeout")
break
# Server would be running on the same host as Client
if len(sys.argv) != 3:
print("\n===== Error usage, python3 TCPClient3.py SERVER_IP SERVER_PORT ======\n")
exit(0)
serverHost = sys.argv[1]
serverPort = int(sys.argv[2])
serverAddress = (serverHost, serverPort)
# define a socket for the client side, it would be used to communicate with the server
clientSocket = socket(AF_INET, SOCK_STREAM)
# build connection with the server and send message to it
clientSocket.connect(serverAddress)
# User Authentication
while 1:
userName = input("Username: ")
if not help.checkText(userName):
print("invalid userName")
else:
clientSocket.send(userName.encode())
break
onlineCheck = clientSocket.recv(1024).decode()
print(f"{onlineCheck}")
if ("[error]" in onlineCheck):
exit()
# receive the comfirmation message of the user name
nameCheck = clientSocket.recv(1024).decode()
print(nameCheck)
# get the password not, create a new password or check the password is done by server
passwordNote = clientSocket.recv(1024).decode()
# send the correct format password to server
while 1:
password = input(passwordNote)
if not help.checkText(password):
print("invalid password format")
else:
clientSocket.send(password.encode())
break
# get the response of the password
for i in range(3):
response = clientSocket.recv(1024).decode()
print(response)
if (response == "welcome"):
break
else:
if (i == 2):
exit()
# break
password = input("password: ")
clientSocket.send(password.encode())
# # get the offline message
# clientSocket.send("offline")
inputThread = InputThread(clientSocket, userName)
receiveServer = ReceiveServer(clientSocket)
inputThread.start()
receiveServer.start()
with lock:
clientSocket.send("broadcast join the chat\n".encode())