954,515 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

chat programming with PyQt and Socket [Standard Library]

Hi my friends , i have written a program that in client section in error occures frequntly , i think the error comes from socket function in client . what i have to do?

# This is my server code , this code has not problem
import asyncore
import socket

clients = {}

class MainServerSocket(asyncore.dispatcher):
    def __init__(self, port):
        asyncore.dispatcher.__init__(self)
        self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
        self.bind(('',port))
        self.listen(5)
    def handle_accept(self):
        newSocket, address = self.accept( )
        clients[address] = newSocket
        print "Connected from", address
        SecondaryServerSocket(newSocket)

class SecondaryServerSocket(asyncore.dispatcher_with_send):
    def handle_read(self):
        receivedData = self.recv(8192)
        if receivedData:
            every = clients.values()
            for one in every:
                one.send(receivedData+'\n')
        else: self.close( )
    def handle_close(self):
        print "Disconnected from", self.getpeername( )
        one = self.getpeername( )
        del clients[one]

MainServerSocket(21567)
asyncore.loop( )


client is here:

from PyQt4 import QtGui , QtCore
from socket import *
import thread
import sys

HOST = 'localhost'
PORT = 21567
BUFSIZE = 1024
ADDR = (HOST, PORT)

tcpCliSock = socket(AF_INET, SOCK_STREAM)
tcpCliSock.connect(ADDR)

class MainWindow(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)

        self.socket()
        
        roomLabel = QtGui.QLabel('room')

        self.browser = QtGui.QTextBrowser()
        self.browser.backwardAvailable
        
        self.textEdit = QtGui.QTextEdit()
        self.textEdit.setMaximumSize(QtCore.QSize(400,60))
        #4 edit line
        self.connect(self.browser, QtCore.SIGNAL("returnPressed()"),self.callback)
        
        SendButton = QtGui.QPushButton('Send')
        SendButton.setMaximumSize(QtCore.QSize(400,60))
        SendButton.clicked.connect(self.callback)




        layoutINlayout = QtGui.QHBoxLayout()
        layoutINlayout.addWidget(self.textEdit)
        layoutINlayout.addWidget(SendButton)


        widget = QtGui.QWidget()
        self.setCentralWidget(widget)
       
        self.layout = QtGui.QVBoxLayout()
        self.layout.addWidget(self.browser)

        mainwindow = QtGui.QVBoxLayout()
        mainwindow.addLayout (self.layout )
        mainwindow.addLayout (layoutINlayout )

        widget.setLayout(mainwindow)
        self.setWindowFlags(QtCore.Qt.WindowTitleHint )
        
    def callback(self, event):

        message = self.textEdit.toPlainText()
        tcpCliSock.send(message)



    def add(self, data):
        self.browser.setText(data)


    #i think the error comes from socket func:
    def socket(self):
        
        def loop0():
            while 1:
                print '1'
                data = tcpCliSock.recv(BUFSIZE)
                if data: self.add(data)

        thread.start_new_thread(loop0, ())
        
            
if __name__ == '__main__':
    
    app = QtGui.QApplication(sys.argv)
    app.setStyle('chat')
    

    window = MainWindow()
    window.setWindowTitle("pro IJ cracker v2")
    window.setWindowIcon(QtGui.QIcon("img/go.png"))
    window.show()
    sys.exit(app.exec_())
iraj.jelo
Newbie Poster
10 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

I don't know anything of PyQt, but usually GUI toolkits have special ways of handling threads. Also even in non gui programs, python threads are usually started with the threading module instead of the lower level thread module. Reading this tutorial http://diotavelli.net/PyQtWiki/Threading,_Signals_and_Slots , it seems that subclassing QThread would be a much safer alternative.

Gribouillis
Posting Maven
Moderator
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: