-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathchat-server.py
114 lines (88 loc) · 3.27 KB
/
chat-server.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
#Developed By : Suryaveer from IIT INDORE
#UserName: @ayrusreev
#GitHub : http://www.github.com/surya-veer
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtNetwork import *
SIZEOF_UINT32 = 4
class ChatServer(QDialog):
def __init__(self, parent=None):
super(ChatServer,self).__init__(parent)
layout = QHBoxLayout
self.setWindowFlags(Qt.WindowStaysOnTopHint)
#height = 500, width = 800
self.setGeometry(300,100,300,300)
#label
self.l1 = QLabel(self)
self.l1.move(40+10,20)
self.l1.setText('Chat Server')
self.l1.setFont(QFont("Arial",30))
self.l2 = QLabel(self)
self.l2.move(45+10,100)
self.l2.setText('Is running on: localhost')
self.l2.setFont(QFont("Arial",15))
self.l2 = QLabel(self)
self.l2.move(90+10,150)
self.l2.setText('Port No: 8888')
self.l2.setFont(QFont("Arial",15))
self.exitBtn = QPushButton(self)
self.exitBtn.setText('Stop Server')
self.exitBtn.setGeometry(65+10,200,160,40)
self.exitBtn.setFont(QFont("Arial",20))
self.exitBtn.clicked.connect(self.StopServer)
#creating server
self.tcpServer = QTcpServer(self)
self.tcpServer.listen(QHostAddress("0.0.0.0"), 8888)
self.connect(self.tcpServer, SIGNAL("newConnection()"),
self.add)
self.connections = []
self.setWindowTitle("Chat Server")
def StopServer(self):
quit()
def add(self):
clientConnection = self.tcpServer.nextPendingConnection()
clientConnection.nextBlockSize = 0
self.connections.append(clientConnection)
self.connect(clientConnection, SIGNAL("readyRead()"),
self.receiveData)
def receiveData(self):
for s in self.connections:
if s.bytesAvailable() > 0:
stream = QDataStream(s)
stream.setVersion(QDataStream.Qt_4_2)
if s.nextBlockSize == 0:
if s.bytesAvailable() < SIZEOF_UINT32:
return
s.nextBlockSize = stream.readUInt32()
if s.bytesAvailable() < s.nextBlockSize:
return
textFromClient = stream.readQString()
s.nextBlockSize = 0
self.sendData(textFromClient,
s.socketDescriptor())
s.nextBlockSize = 0
def sendData(self, text, socketId):
for s in self.connections:
if s.socketDescriptor() == socketId:
pre, post = text.split(">:")
message = "\n<[Me]> {}".format(post)
print message
else:
message = "\n {}".format(text)
reply = QByteArray()
stream = QDataStream(reply, QIODevice.WriteOnly)
stream.setVersion(QDataStream.Qt_4_2)
stream.writeUInt32(0)
stream.writeQString(message)
stream.device().seek(0)
stream.writeUInt32(reply.size() - SIZEOF_UINT32)
s.write(reply)
def main():
app = QApplication(sys.argv)
form = ChatServer()
form.show()
form.move(650,460)
app.exec_()
if __name__ == '__main__':
main()