www.libssh2.org | Daily snapshots | Mailing list archive | Docs | Examples

Archive Index This month's Index

Subject: [libssh2] Failing to create multiple channels in windows

[libssh2] Failing to create multiple channels in windows

From: Sabyasachi Ruj <ruj.sabya_at_gmail.com>
Date: Fri, 25 Jan 2008 13:27:54 +0530

I am using windows XP and visual studio 2003.
Here I am having a problem in windows only.

If I am creating to 'tunneled' channels by using
The second channel creation fails 'channel2'.

But the first one is successful.

But in Linux the same program is able to create two channels!

My guess, it is a problem since libssh2 is using select(HAVE_SELECT) in

I am pasting the source code below:-

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <time.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>

#ifndef _WIN32
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#endif //_WIN32

#include "libssh2.h"

 * This function reports the error and
 * exits back to the shell :
static void bail(const char *on_what)
    if ( errno != 0 ) {
        fputs(": ",stderr);

int main(int argc,char **argv)
    char *srvr_addr = NULL;
    char *srvr_port = "9099";
    SOCKET sock;
    struct sockaddr_in sin;
    LIBSSH2_SESSION *session;
    LIBSSH2_CHANNEL *channel1, *channel2;

#ifdef WIN32
    WSADATA wsadata;
    WSAStartup(WINSOCK_VERSION, &wsadata);

    sock = socket(AF_INET, SOCK_STREAM, 0);
    sin.sin_family = AF_INET;
    sin.sin_port = htons(22);
    sin.sin_addr.s_addr = inet_addr("");

    printf("TCPIP connection\n");
    if (connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in))
!= 0)
        fprintf(stderr, "failed to connect!\n");
        return -1;

    printf("SSH2 session\n");
    session = libssh2_session_init();

    if (libssh2_session_startup(session, sock))
        fprintf(stderr, "Failure establishing SSH session\n");
        return -1;

    printf("SSH2 authentication\n");
    /* We could authenticate via password */
    if (libssh2_userauth_password(session, "root", "yourpassword"))
        printf("Authentication by password failed.\n");

    channel1 = libssh2_channel_direct_tcpip(session, "", 3306);
    if (channel1 == 0)
        bail("Failed channel forwarding: channel1\n");
        printf("channel connected (%x)\n",channel1);

    HERE THE ERROR OCCURS: In Windows it fails.
    In linux we can create the channel successfully

    channel2 = libssh2_channel_direct_tcpip(session, "", 3306);

    if (channel2 == 0)
        bail("Failed channel forwarding: channel2\n");
        printf("channel connected (%x)\n",channel2);



    /* Control never gets here */
    return 0;

This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.

libssh2-devel mailing list
Received on 2008-01-25

the libssh2 team