-
Notifications
You must be signed in to change notification settings - Fork 1
/
Client.py
187 lines (159 loc) · 6.32 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
#!/usr/bin/env python
import sys
import socket
import time
import threading
from threading import Thread
import tkinter as tk
from tkinter import ttk
import queue
import Credentialsmatch as cm
# from SocketServer import ThreadingMixIn
class ServerThread(Thread):
def __init__(self, socket):
Thread.__init__(self)
self.socket = socket
def run(self):
print("send")
while True:
#starttime = time.time()
#command = input(" Enter command: ")
command=q.get()
self.socket.send(command.encode())
ack = self.socket.recv(BUFFER_SIZE)
print(ack.decode())
class ServerThreadread(Thread):
def __init__(self, socket):
Thread.__init__(self)
self.socket = socket
# print "New thread started for chat display"
def run(self):
s2 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s2.connect((TCP_IP, TCP_PORT2))
welcomemsg = s2.recv(BUFFER_SIZE)
chat = "initial"
print(welcomemsg.decode())
while True:
chat = s2.recv(BUFFER_SIZE)
print(chat.decode())
time.sleep(5)
TCP_IP = '54.152.237.120' # sys.argv[1]
TCP_PORT = 5002 # int(sys.argv[2])
TCP_PORT2 = 50000
BUFFER_SIZE = 1024
threads = []
q = queue.Queue()
global log
log = 0
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.connect((TCP_IP, TCP_PORT))
TIME_OUT = s.recv(BUFFER_SIZE) # Server exchanges tmeout details with client at the start of every socket
count = [1, 2, 3]
status = 0
class clientchatwindows():
def code(self,usernamein):
self.deleteofflineuser=usernamein
print("runfunction")
self.clientwin = tk.Tk()
self.clientwin.title("Chat")
# self.clientwin.mainloop()
# chatwindowgui = windows()
print("self.clientwin")
print("afterchatreeive")
chatreceive = tk.StringVar()
print("aftertkstringintvar")
ttk.Label(self.clientwin, text="Chat").grid(column=0, row=0)
self.chatreceive = ttk.Entry(self.clientwin, width=20, textvariable=chatreceive)
self.chatreceive.grid(column=0, row=2)
sendchat = ttk.Button(self.clientwin, text="Send", command=self.gettext) # button calls the sendchat function
sendchat.grid(column=1, row=2)
self.clientwin.protocol('WM_DELETE_WINDOW', self.closesecond)
# -----------------------------------------------------------------------------
# scroll bar code that is going to display
loginscroll = tk.Scrollbar(self.clientwin)
self.listbox = tk.Listbox(self.clientwin)
self.listbox.grid(column=0, row=1)
# function that adds users who are online
getonlineusers=cm.matchcredentials()
v=getonlineusers.getonlineusers() # function return list of users who are online
for i in v:
print(i[0])
self.listbox.insert(tk.END, i)
self.listbox.config(yscrollcommand=loginscroll.set)
loginscroll.config(command=self.listbox.yview)
sendto=tk.StringVar()
self.sendtowindow=ttk.Entry(self.clientwin, textvariable=sendto) # entry widget getting the name to send chat to
self.sendtowindow.grid(column=2, row=1)
# ----------------------------------------------------------------------
sendto = ttk.Button(self.clientwin, text="-->>", command=self.get)
sendto.grid(column=1, row=1)
# print "New thread started for write"
print("mainloop")
self.clientwin.mainloop()
self.clientwin.protocol('WM_DELETE_WINDOW', self.closesecond)
def closesecond(self):
CLOSEWINDOW=cm.matchcredentials()
CLOSEWINDOW.deleteofflineusers(self.deleteofflineuser) # self.deleteofflineuser is passed in this class when stating this client gui thread
self.clientwin.destroy()
self.clientwin.quit()
def get(self):
printlistbox = self.listbox.get('active')
self.sendtowindow.insert(0,printlistbox)
print(printlistbox)
def gettext(self):
gettextchatreceive=self.chatreceive.get()
getsendtowindow=self.sendtowindow.get()
q.put(gettextchatreceive) # putting data in queue to send data to the threading sending to server
#print(gettextchatreceive)
def _quit(self):
self.clientwin.quit()
self.clientwin.destroy()
exit()
class clientcode(): # t
def clientcodemain(self,usernamein,passwordin): #username and password received from Login after the credentials matched by credentialsmatch
#status = 0
#while status == 0:
number = 0
username = usernamein
#username = input("Enter username: ")
s.send(username.encode())
#password = passwordin
#password = input("Enter password: ")
#s.send(password.encode())
#passwordcheck = s.recv(BUFFER_SIZE)
#passwordcheck=passwordcheck.decode()
#if (passwordcheck == "invalid password"):
#status = 0
#number = number + 1
#if number == 3:
#status = 2
#break
#else:
#print(" Invalid password , enter details again ")
#continue
#else:
#status = 1
#if ( status == 1 ):
try:
newthread = ServerThread(s)
newthread.daemon = True
newthread2 = ServerThreadread(s)
newthread2.daemon = True
newthread.start()
newthread2.start()
threads.append(newthread)
threads.append(newthread2)
newmethod = clientchatwindows()
t = Thread(target=newmethod.code(username))
t.start()
while True:
for t in threads:
t.join(600)
if not t.isAlive():
break
break
except KeyboardInterrupt:
command = "logout"
s.send(command.encode())
sys.exit()