Results 1 to 7 of 7

Thread: QTcpSocket and libssh2 ...

  1. #1

    Default QTcpSocket and libssh2 ...

    Hi all,

    I'm developing a qt4 system which connects to a ssh server by using the libssh2 library. The ssh features provided by libssh2 relies on a conventional TCP socket for secure data transmission. My goal is to use a QTcpSocket to handle portability issues among different operating systems and instruct libssh2 to use this Qt socket.

    The problem is: when I create a socket by using the GNU/Linux system calls everything works fine. When I switch to the QTcpSocket libssh2 presents an error message during the session startup.

    The code which works is:
    Qt Code:
    1. int sock = socket(AF_INET, SOCK_STREAM, 0);
    2.  
    3. struct sockaddr_in sin;
    4. sin.sin_family = AF_INET;
    5. sin.sin_port = htons(property("port").toInt());
    6. sin.sin_addr.s_addr = inet_addr(property("host").toString().toAscii().constData());
    7. if (::connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in)) != 0) {
    8. fprintf(stderr, "failed to connect!\n");
    9. return false;
    10. }
    11.  
    12. session = libssh2_session_init();
    13. rc = libssh2_session_startup(d->session, sock);
    14. if (rc)
    15. {
    16. setErrorMessage("Failure establishing SSH session !");
    17. return false;
    18. }
    19.  
    20. // Authenticate via password ...
    To copy to clipboard, switch view to plain text mode 

    The code which uses the QTcpSocket is:

    Qt Code:
    1. QTcpSocket socket;
    2. socket.connectToHost(property("host").toString(), (quint16) property("port").toInt());
    3.  
    4. if (!d->socket.waitForConnected(5 * 1000))
    5. {
    6. setErrorMessage(d->socket.errorString());
    7. return false;
    8. }
    9.  
    10. session = libssh2_session_init();
    11. rc = libssh2_session_startup(d->session, d->socket.socketDescriptor());
    12. if (rc)
    13. {
    14. setErrorMessage("Failure establishing SSH session !");
    15. return false;
    16. }
    To copy to clipboard, switch view to plain text mode 

    I'm using the socketDescriptor() method to acquire the native socket descriptor required by libssh2.

    The later solution doesn't work and the message "Failure establishing SSH session !" is exhibited.

    What am I doing wrong ? Even if I print the int value of the descriptors in both solutions I get the same value !!!

    Thanks in advance,
    Sandro

  2. #2
    Join Date
    Apr 2007
    Posts
    44
    Thanks
    6
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: QTcpSocket and libssh2 ...

    I think d->session is incorrect value.
    Correct me if i'm wrong.

  3. #3

    Default Re: QTcpSocket and libssh2 ...

    Hi Teerayoot,

    There is a typo in my code. In fact, is:

    Qt Code:
    1. d->session = libssh2_session_init();
    2. rc = libssh2_session_startup(d->session, d->socket.socketDescriptor());
    To copy to clipboard, switch view to plain text mode 

  4. #4

    Default Re: QTcpSocket and libssh2 ...

    I think the problem is: conventional UNIX sockets are blocking by default and QTcpSockets are non-blocking.

    Am i right ? How can I tell to a QTcpSocket behaves blocking ?

    Thanks

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 975 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTcpSocket and libssh2 ...

    Quote Originally Posted by sandros View Post
    How can I tell to a QTcpSocket behaves blocking ?
    Use the waitForXxx() methods.

    http://doc.trolltech.com/4.3/network...uneclient.html

  6. #6

    Default Re: QTcpSocket and libssh2 ...

    Hi Jacek, thanks for reply !

    I can't use the waitForXxx() methods because I'm just creating the socket and passing it to libssh2. libssh2 works with conventional unix socket which are blocking by default.

    Thanks,
    Sandro

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 975 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTcpSocket and libssh2 ...

    Quote Originally Posted by sandros View Post
    I can't use the waitForXxx() methods because I'm just creating the socket and passing it to libssh2. libssh2 works with conventional unix socket which are blocking by default.
    You can change socket options using ::setsockopt().

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.