Results 1 to 18 of 18

Thread: QTcpSocket connection problem

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    valdemar593 Guest

    Default QTcpSocket connection problem

    Hello everyone. I'm trying to write a chat using QTcpSocket and QTcpServer, but strange things are happening. In the first example QTcpServer emits newConnection(), but QTcpSocket remains in connecting state without emiting error(...). In the second example I receive SIGSEGV when trying connectToHost() . What is more, FortuneClient/Server (single threaded) example runs just fine. In both examples network sessiong is not required. Being discouraged I examined the Fortune code and discovored that it is just the same concerning establishing connection. Here are both examples.

    FIRST

    Qt Code:
    1. //CLIENT
    2. ChatClient::ChatClient(QObject *parent)
    3. : QObject(parent) {
    4. tcpSocket = new QTcpSocket(this);
    5. QNetworkConfigurationManager manager;
    6. if (QNetworkConfigurationManager::NetworkSessionRequired
    7. & manager.capabilities()) {
    8. qDebug() << "Network session required";
    9. }
    10. connect(tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)),
    11. this, SLOT(error(QAbstractSocket::SocketError)));
    12. connect(tcpSocket, SIGNAL(connected()),
    13. this, SLOT(requestForID()));
    14. connect(tcpSocket, SIGNAL(readyRead()),
    15. this, SLOT(receiveMessage()));
    16. tcpSocket->connectToHost("192.168.0.100", PORT);
    17. }
    18. ...
    19. void ChatClient::error(QAbstractSocket::SocketError error) {
    20. qDebug() << "Socket error" << error;
    21. }
    22.  
    23. void ChatClient::requestForID() {
    24. qDebug() << "Connected, requesting for ID";
    25. QByteArray segment;
    26. QDataStream out(&segment, QIODevice::WriteOnly);
    27. out.setVersion(QDataStream::Qt_4_7);
    28. out << (quint16)0 << ID;
    29. out.device()->seek(0);
    30. out << (quint16)(segment.size() - sizeof(quint16));
    31. tcpSocket->write(segment);
    32. }
    33.  
    34. //SERVER
    35. ChatServer::ChatServer(QObject *parent)
    36. : QObject(parent) {
    37. tcpServer = new QTcpServer(this);
    38. if (!tcpServer->listen(/*QHostAddress::Any, PORT*/)) {
    39. qDebug() << "Unable to start the server"
    40. << tcpServer->errorString();
    41. }
    42. qDebug() << "Server port" << tcpServer->serverPort();
    43. connect(tcpServer, SIGNAL(newConnection()),
    44. this, SLOT(processConnection()));
    45. }
    46.  
    47. void ChatServer::processConnection() {
    48. qDebug() << "Incoming connection";
    49. QTcpSocket *clientSocket = tcpServer->nextPendingConnection();
    50. /*connect(clientSocket, SIGNAL(readyRead()),
    51.   this, SLOT(readData()));
    52.   readData(clientSocket);
    53.   connect(clientSocket, SIGNAL(disconnected()),
    54.   clientSocket, SLOT(deleteLater()));*/
    55. QByteArray segment;
    56. QDataStream out(&segment, QIODevice::WriteOnly);
    57. out.setVersion(QDataStream::Qt_4_7);
    58. out << (quint16)0 << (quint16)Message
    59. << "Successfully connected";
    60. out.device()->seek(0);
    61. out << (quint16)(segment.size() - sizeof(quint16));
    62. clientSocket->write(segment);
    63. clientSocket->disconnectFromHost();
    64. }
    To copy to clipboard, switch view to plain text mode 

    SECOND

    Qt Code:
    1. //CLIENT
    2. Client::Client() {
    3. QTcpSocket *clientSocket = new QTcpSocket(this);
    4. connect(clientSocket, SIGNAL(connected()),
    5. this, SLOT(connectionEstablished()));
    6. connect(clientSocket, SIGNAL(error(QAbstractSocket::SocketError)),
    7. this, SLOT(error()));
    8. }
    9.  
    10. void Client::establishConnection() {
    11. // throws SIGSEGV
    12. clientSocket->connectToHost(QHostAddress("192.168.0.100"), 6178);
    13. }
    14.  
    15. void Client::connectionEstablished() {
    16. qDebug() << "Connection established";
    17. }
    18.  
    19. void Client::error() {
    20. qDebug() << clientSocket->errorString();
    21. }
    22.  
    23. //SERVER
    24. Server::Server() {
    25. QNetworkConfigurationManager manager;
    26. if (manager.capabilities() &
    27. QNetworkConfigurationManager::NetworkSessionRequired) {
    28. qDebug() << "Network session required";
    29. } else {
    30. sessionOpened();
    31. }
    32. connect(server, SIGNAL(newConnection()),
    33. this, SLOT(processConnection()));
    34. }
    35.  
    36. void Server::processConnection() {
    37. qDebug() << "Incoming connection";
    38. }
    39.  
    40. void Server::sessionOpened() {
    41. server = new QTcpServer(this);
    42. if (!server->listen(QHostAddress::Any, 6178)) {
    43. qDebug() << QString("Unable to start the server: %1")
    44. .arg(server->errorString());
    45. return;
    46. }
    47. qDebug() << "The server is running...";
    48. }
    To copy to clipboard, switch view to plain text mode 

    Any ideas?
    Last edited by valdemar593; 27th May 2011 at 15:51.

Similar Threads

  1. Problem with connection
    By Adonitos in forum Qt Programming
    Replies: 4
    Last Post: 8th April 2011, 11:56
  2. Replies: 1
    Last Post: 15th November 2010, 11:36
  3. DB Connection Problem
    By ktmdwn in forum Qt Programming
    Replies: 13
    Last Post: 4th August 2010, 15:12
  4. SQL Connection Problem
    By Utku in forum Installation and Deployment
    Replies: 3
    Last Post: 25th October 2009, 13:21
  5. QTcpsocket - connection problem
    By vishesh in forum Qt Programming
    Replies: 1
    Last Post: 16th October 2007, 17:03

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
  •  
Qt is a trademark of The Qt Company.