Results 1 to 3 of 3

Thread: socket & thread

  1. #1
    Join Date
    Sep 2009
    Posts
    49
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default socket & thread

    Related to : PyQt 4.6.1
    This is regarding a simple socket reading in my application.
    I am using QAbstractSocket.UdpSocket for the purpose.
    Once the socket is connected to given host and port, it should constantly watch for data.

    Question 1:
    Do I have to use a thread to watch socket for data being available or signal "readyRead" will
    do this job?

    Question 2:
    Code for sending data on socket:
    Qt Code:
    1. socket = QtNetwork.QAbstractSocket(QtNetwork.QAbstractSocket.UdpSocket, self)
    2. socket.connectToHost(host, port)
    3. socket.writeData("This is the test string.")
    4. socket.disconnectFromHost()
    5. socket.close()
    To copy to clipboard, switch view to plain text mode 
    The line:
    Qt Code:
    1. socket.writeData("This is the test string.")
    To copy to clipboard, switch view to plain text mode 
    is crashing the application. why?

    Question 3:
    What's the proper way to close a socket?
    I am using:
    Qt Code:
    1. self.socket.disconnectFromHost()
    2. self.socket.waitForDisconnected()
    3. self.socket.close()
    To copy to clipboard, switch view to plain text mode 
    When I am closing the socket using above code, no error or warning is produced, but when I close the dialog box where I am implementing my tests, message is printed to stdout:
    Qt Code:
    1. QWaitCondition: Destroyed while threads are still waiting
    To copy to clipboard, switch view to plain text mode 

    Please be patient, I have a limited knowledge on threads and sockets.
    Prashant


    qt-sdk-win-opensource-2009.03.1.exe
    Python 2.6.3
    PyQt-Py2.6-gpl-4.6-1
    Win XP, 32 Bit

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: socket & thread

    The socket will never be connected to a given host and port because UDP is a connectionless protocol. connectToHost() only states a default destination for write operations. Furthermore even if it was otherwise, connectToHost() and disconnectFromHost() are asynchronous operations - the methods return before the functionality it represents is executed thus writeData() will execute before connectToHost() even starts doing its job (at least in case of connection oriented protocols like TCP).

    As for the first question - you don't need threads.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Sep 2009
    Posts
    49
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: socket & thread

    Here are the two version of my Receiver & Sender scripts:
    Receiver
    Qt Code:
    1. # Start Communication.
    2. if status:
    3. # Create socket
    4. self.socket = QtNetwork.QUdpSocket(self)
    5. self.socket.setReadBufferSize(buffer)
    6. self.socket.bind(QHostAddress.LocalHost, port, QtNetwork.QUdpSocket.ShareAddress)
    7. self.socket.readyRead.connect(self.dataArrived)
    8. msg=("Socket opened at host=%s, port=%d." % (host, port))
    9. # Stop Communication.
    10. else:
    11. # Close socket
    12. self.socket.disconnectFromHost()
    13. self.socket.waitForDisconnected()
    14. self.socket.close()
    15. msg=("Socket closed at host=%s, port=%d." % (host, port))
    To copy to clipboard, switch view to plain text mode 
    Sender:
    Qt Code:
    1. import socket
    2.  
    3. # Set the socket parameters
    4. host = "localhost"
    5. port = 6268
    6. addr = (host, port)
    7.  
    8. service = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    9.  
    10. data = "This is a test string."
    11. try:
    12. service.connect(addr)
    13. service.send(data)
    14. except Exception, (value, message):
    15. if service:
    16. service.close()
    17. print message
    18. service.close()
    To copy to clipboard, switch view to plain text mode 
    The problem is in Receiver script
    Prashant


    qt-sdk-win-opensource-2009.03.1.exe
    Python 2.6.3
    PyQt-Py2.6-gpl-4.6-1
    Win XP, 32 Bit

Similar Threads

  1. socket notifiers cannot be enabled from another thread
    By babu198649 in forum Qt Programming
    Replies: 5
    Last Post: 4th April 2009, 15:15
  2. thread with socket?
    By triperzonak in forum Qt Programming
    Replies: 6
    Last Post: 25th September 2008, 16:21
  3. Thread, Timer and Socket. Comuication problem
    By ^NyAw^ in forum Qt Programming
    Replies: 6
    Last Post: 17th January 2008, 16:48
  4. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 06:13
  5. How to write on a socket in another thread?
    By Valheru in forum Qt Programming
    Replies: 7
    Last Post: 12th October 2006, 10:52

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.