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:
Code:
int sock = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(property("port").toInt());
sin.sin_addr.s_addr = inet_addr(property("host").toString().toAscii().constData());
if (::connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in)) != 0) {
fprintf(stderr, "failed to connect!\n");
return false;
}
session = libssh2_session_init();
rc = libssh2_session_startup(d->session, sock);
if (rc)
{
setErrorMessage("Failure establishing SSH session !");
return false;
}
// Authenticate via password ...
The code which uses the QTcpSocket is:
Code:
socket.connectToHost(property("host").toString(), (quint16) property("port").toInt());
if (!d->socket.waitForConnected(5 * 1000))
{
setErrorMessage(d->socket.errorString());
return false;
}
session = libssh2_session_init();
rc = libssh2_session_startup(d->session, d->socket.socketDescriptor());
if (rc)
{
setErrorMessage("Failure establishing SSH session !");
return false;
}
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
Re: QTcpSocket and libssh2 ...
I think d->session is incorrect value.
Correct me if i'm wrong.
Re: QTcpSocket and libssh2 ...
Hi Teerayoot,
There is a typo in my code. In fact, is:
Code:
d->session = libssh2_session_init();
rc = libssh2_session_startup(d->session, d->socket.socketDescriptor());
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
Re: QTcpSocket and libssh2 ...
Quote:
Originally Posted by
sandros
How can I tell to a QTcpSocket behaves blocking ?
Use the waitForXxx() methods.
http://doc.trolltech.com/4.3/network...uneclient.html
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
Re: QTcpSocket and libssh2 ...
Quote:
Originally Posted by
sandros
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().