-
Notifications
You must be signed in to change notification settings - Fork 0
/
chatApp.py
180 lines (159 loc) · 6.54 KB
/
chatApp.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
from Tkinter import *
from ttk import *
import socket
import thread
class ChatClient(Frame):
def __init__(self, root):
Frame.__init__(self, root)
self.root = root
self.initUI()
self.serverSoc = None
self.serverStatus = 0
self.buffsize = 1024
self.allClients = {}
self.counter = 0
def initUI(self):
self.root.title("P2P chatting application with signature verification")
ScreenSizeX = self.root.winfo_screenwidth()
ScreenSizeY = self.root.winfo_screenheight()
self.FrameSizeX = 800
self.FrameSizeY = 800
FramePosX = (ScreenSizeX - self.FrameSizeX)/2
FramePosY = (ScreenSizeY - self.FrameSizeY)/2
self.root.geometry("%sx%s+%s+%s" % (self.FrameSizeX,self.FrameSizeY,FramePosX,FramePosY))
self.root.resizable(width=False, height=False)
padX = 10
padY = 10
parentFrame = Frame(self.root)
parentFrame.grid(padx=padX, pady=padY, stick=E+W+N+S)
ipGroup = Frame(parentFrame)
serverLabel = Label(ipGroup, text="Set: ")
self.nameVar = StringVar()
self.nameVar.set(" ") #to set the name of the user
nameField = Entry(ipGroup, width=10, textvariable=self.nameVar)
self.serverIPVar = StringVar()
self.serverIPVar.set("127.0.0.1")
serverIPField = Entry(ipGroup, width=15, textvariable=self.serverIPVar)
self.serverPortVar = StringVar()
self.serverPortVar.set("8090")
serverPortField = Entry(ipGroup, width=5, textvariable=self.serverPortVar)
serverSetButton = Button(ipGroup, text="Set", width=10, command=self.set_server)
addClientLabel = Label(ipGroup, text="IP to connect: ")
self.clientIPVar = StringVar()
self.clientIPVar.set("127.0.0.1")
clientIPField = Entry(ipGroup, width=15, textvariable=self.clientIPVar)
self.clientPortVar = StringVar()
self.clientPortVar.set("8091")
clientPortField = Entry(ipGroup, width=5, textvariable=self.clientPortVar)
clientSetButton = Button(ipGroup, text="Add", width=10, command=self.add_client)
serverLabel.grid(row=0, column=0)
nameField.grid(row=0, column=1)
serverIPField.grid(row=0, column=2)
serverPortField.grid(row=0, column=3)
serverSetButton.grid(row=0, column=4, padx=5)
addClientLabel.grid(row=0, column=5)
clientIPField.grid(row=0, column=6)
clientPortField.grid(row=0, column=7)
clientSetButton.grid(row=0, column=8, padx=5)
readChatGroup = Frame(parentFrame)
self.receivedChats = Text(readChatGroup, bg="white", width=60, height=30, state=DISABLED)
self.receivedChats.grid(row=0, column=0, sticky=N+S, padx = (0,10))
writeChatGroup = Frame(parentFrame)
self.chatVar = StringVar()
self.chatField = Entry(writeChatGroup, width=80, textvariable=self.chatVar)
sendChatButton = Button(writeChatGroup, text="Send", width=10, command=self.handleSendChat)
self.chatField.grid(row=0, column=0, sticky=W)
sendChatButton.grid(row=0, column=1, padx=5)
friendsInfo = Frame(parentFrame)
self.friends = Listbox(friendsInfo, bg="white", width=30, height=4)
self.friends.grid(row=0, column=0, sticky=N+S)
self.statusLabel = Label(parentFrame)
bottomLabel = Label(parentFrame, text="Messages are verified using digital signatures so we know when someone is tampering te message packets")
ipGroup.grid(row=0, column=0)
readChatGroup.grid(row=1, column=0)
writeChatGroup.grid(row=2, column=0, pady=10)
friendsInfo.grid(row=3,column=0,pady=10)
self.statusLabel.grid(row=4, column=0)
bottomLabel.grid(row=5, column=0, pady=10)
def set_server(self):
if self.serverSoc != None:
self.serverSoc.close()
self.serverSoc = None
self.serverStatus = 0
serveraddr = (self.serverIPVar.get().replace(' ',''), int(self.serverPortVar.get().replace(' ','')))
try:
self.serverSoc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.serverSoc.bind(serveraddr)
self.serverSoc.listen(5)
self.set_status("Server listening on %s:%s" % serveraddr)
thread.start_new_thread(self.listenClients,())
self.serverStatus = 1
self.name = self.nameVar.get().replace(' ','')
if self.name == '':
self.name = "%s:%s" % serveraddr
except:
self.set_status("Error setting up server")
def listenClients(self):
while 1:
clientsoc, clientaddr = self.serverSoc.accept()
self.set_status("Client connected from %s:%s" % clientaddr)
self.add_client(clientsoc, clientaddr)
thread.start_new_thread(self.handleClientMessages, (clientsoc, clientaddr))
self.serverSoc.close()
def add_client(self):
if self.serverStatus == 0:
self.set_status("Set server address first")
return
clientaddr = (self.clientIPVar.get().replace(' ',''), int(self.clientPortVar.get().replace(' ','')))
try:
clientsoc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
clientsoc.connect(clientaddr)
self.set_status("Connected to client on %s:%s" % clientaddr)
self.add_client(clientsoc, clientaddr)
thread.start_new_thread(self.handleClientMessages, (clientsoc, clientaddr))
except:
self.set_status("Error connecting to client")
def handleClientMessages(self, clientsoc, clientaddr):
while 1:
try:
data = clientsoc.recv(self.buffsize)
if not data:
break
self.addChat("%s:%s" % clientaddr, data)
except:
break
self.removeClient(clientsoc, clientaddr)
clientsoc.close()
self.set_status("Client disconnected from %s:%s" % clientaddr)
def handleSendChat(self):
if self.serverStatus == 0:
self.set_status("Set server address first")
return
msg = self.chatVar.get().replace(' ','')
if msg == '':
return
self.addChat("me", msg)
for client in self.allClients.keys():
client.send(msg)
def addChat(self, client, msg):
self.receivedChats.config(state=NORMAL)
self.receivedChats.insert("end",client+": "+msg+"\n")
self.receivedChats.config(state=DISABLED)
def add_client(self, clientsoc, clientaddr):
self.allClients[clientsoc]=self.counter
self.counter += 1
self.friends.insert(self.counter,"%s:%s" % clientaddr)
def removeClient(self, clientsoc, clientaddr):
print self.allClients
self.friends.delete(self.allClients[clientsoc])
del self.allClients[clientsoc]
print self.allClients
def set_status(self, msg):
self.statusLabel.config(text=msg)
print msg
def main():
root = Tk()
app = ChatClient(root)
root.mainloop()
if __name__ == '__main__':
main()