-
Notifications
You must be signed in to change notification settings - Fork 2
/
chat.py
216 lines (180 loc) · 6.62 KB
/
chat.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
import sys, time
from PyQt5 import QtGui
from PyQt5 import QtCore
from PyQt5.QtWidgets import QScrollBar, QSplitter, QTableWidgetItem, QTableWidget, QComboBox, QVBoxLayout, QGridLayout, \
QDialog, QWidget, QPushButton, QApplication, QMainWindow, QAction, QMessageBox, QLabel, QTextEdit, QProgressBar, \
QLineEdit, QInputDialog
from PyQt5.QtCore import QCoreApplication
import socket
from threading import Thread
from socketserver import ThreadingMixIn
conn = None
class App1(QWidget):
def __init__(self):
super().__init__()
self.title = 'server or client'
self.left = 10
self.top = 10
self.width = 720
self.height = 640
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
self.getText1()
def getText1(self):
self.text, okPressed = QInputDialog.getText(self, "server or client", "you are server or client??!! :", QLineEdit.Normal, "")
return self.text
class Window(QDialog):
def __init__(self):
super().__init__()
self.flag = 0
self.chatTextField = QLineEdit(self)
self.chatTextField.resize(480, 100)
self.chatTextField.move(10, 350)
self.btnSend = QPushButton("Send", self)
self.btnSend.resize(480, 30)
self.btnSendFont = self.btnSend.font()
self.btnSendFont.setPointSize(15)
self.btnSend.setFont(self.btnSendFont)
self.btnSend.move(10, 460)
self.btnSend.setStyleSheet("background-color: #F7CE16")
self.btnSend.clicked.connect(self.send)
self.chatBody = QVBoxLayout(self)
# self.chatBody.addWidget(self.chatTextField)
# self.chatBody.addWidget(self.btnSend)
# self.chatWidget.setLayout(self.chatBody)
splitter = QSplitter(QtCore.Qt.Vertical)
self.chat = QTextEdit()
self.chat.setReadOnly(True)
# self.chatLayout=QVBoxLayout()
# self.scrollBar=QScrollBar(self.chat)
# self.chat.setLayout(self.chatLayout)
splitter.addWidget(self.chat)
splitter.addWidget(self.chatTextField)
splitter.setSizes([400, 100])
splitter2 = QSplitter(QtCore.Qt.Vertical)
splitter2.addWidget(splitter)
splitter2.addWidget(self.btnSend)
splitter2.setSizes([200, 10])
self.chatBody.addWidget(splitter2)
self.setWindowTitle("Chat Application")
self.resize(500, 500)
def send(self):
text = self.chatTextField.text()
font = self.chat.font()
font.setPointSize(13)
self.chat.setFont(font)
textFormatted = '{:>80}'.format(text)
self.chat.append(textFormatted)
global conn
conn.send(text.encode("utf-8"))
self.chatTextField.setText("")
class ServerThread(Thread):
def __init__(self, window):
Thread.__init__(self)
self.window = window
def run(self):
TCP_IP = '0.0.0.0'
TCP_PORT = 80
BUFFER_SIZE = 20
tcpServer = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcpServer.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
tcpServer.bind((TCP_IP, TCP_PORT))
threads = []
tcpServer.listen(4)
while True:
print("Multithreaded Python server : Waiting for connections from TCP clients...")
global conn
(conn, (ip, port)) = tcpServer.accept()
newthread = ClientThread(ip, port, window)
newthread.start()
threads.append(newthread)
for t in threads:
t.join()
class ClientThread(Thread):
def __init__(self, ip, port, window):
Thread.__init__(self)
self.window = window
self.ip = ip
self.port = port
print("[+] New server socket thread started for " + ip + ":" + str(port))
def run(self):
while True:
# (conn, (self.ip,self.port)) = serverThread.tcpServer.accept()
global conn
data = conn.recv(2048)
window.chat.append(data.decode("utf-8"))
print(data)
class Window1(QDialog):
def __init__(self):
super().__init__()
self.flag = 0
self.chatTextField = QLineEdit(self)
self.chatTextField.resize(480, 100)
self.chatTextField.move(10, 350)
self.btnSend = QPushButton("Send", self)
self.btnSend.resize(480, 30)
self.btnSendFont = self.btnSend.font()
self.btnSendFont.setPointSize(15)
self.btnSend.setFont(self.btnSendFont)
self.btnSend.move(10, 460)
self.btnSend.setStyleSheet("background-color: #F7CE16")
self.btnSend.clicked.connect(self.send)
self.chatBody = QVBoxLayout(self)
# self.chatBody.addWidget(self.chatTextField)
# self.chatBody.addWidget(self.btnSend)
# self.chatWidget.setLayout(self.chatBody)
splitter = QSplitter(QtCore.Qt.Vertical)
self.chat = QTextEdit()
self.chat.setReadOnly(True)
splitter.addWidget(self.chat)
splitter.addWidget(self.chatTextField)
splitter.setSizes([400, 100])
splitter2 = QSplitter(QtCore.Qt.Vertical)
splitter2.addWidget(splitter)
splitter2.addWidget(self.btnSend)
splitter2.setSizes([200, 10])
self.chatBody.addWidget(splitter2)
self.setWindowTitle("Chat Application")
self.resize(500, 500)
def send(self):
text = self.chatTextField.text()
font = self.chat.font()
font.setPointSize(13)
self.chat.setFont(font)
textFormatted = '{:>80}'.format(text)
self.chat.append(textFormatted)
tcpClientA.send(text.encode())
self.chatTextField.setText("")
class ClientThread1(Thread):
def __init__(self, window):
Thread.__init__(self)
self.window = window
def run(self):
host = socket.gethostname()
port = 80
BUFFER_SIZE = 2000
global tcpClientA
tcpClientA = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcpClientA.connect((host, port))
while True:
data = tcpClientA.recv(BUFFER_SIZE)
window.chat.append(data.decode("utf-8"))
tcpClientA.close()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App1()
serorcli = ex.getText1()
print(serorcli)
if serorcli == "server":
window = Window()
serverThread = ServerThread(window)
serverThread.start()
window.exec()
sys.exit(app.exec_())
else:
window = Window1()
clientThread = ClientThread1(window)
clientThread.start()
window.exec()
sys.exit(app.exec_())