2012-01-14 107 views
2

我已經寫了一個程序,在客戶端部分經常發生錯誤,我認爲錯誤來自client.pysocket函數。我該怎麼辦?使用PyQt和Socket聊天編程[標準庫]

server.py:

# This is my server code , this code has no problems 
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.py:

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_()) 
+0

發生了什麼錯誤?你能解釋一下你期望發生什麼,你有什麼問題? – Chris 2012-01-14 16:23:48

+0

是的:)錯誤是:未啓用的線程異常在 2012-01-14 16:37:49

回答

5

我的建議是

1)使用的QThread

2)不要直接修改小部件你的主線程從另一個線程。相反,每次有數據時從QThread發出一個信號。

也爲一些快速的信息,並瞭解當前線程設置崩潰的嘗試只是包裝和打印異常:

def loop0(): 
     while 1: 
      print '1' 
      try: 
       data = tcpCliSock.recv(BUFSIZE) 
       if data: self.add(data) 
      except Exception, e: 
       print "ERROR:", e 
       raise 
+0

先生非常感謝,它已完成:) – 2012-01-15 09:29:09

+0

@irajjelodari - 確保點擊綠色複選標記:-) – jdi 2012-01-15 15:24:32

+3

@irajjelodari can you發佈你的最終結果。有點我也是在同一個問題上 – Rao 2012-12-11 05:25:02