hi,all
When using multiple threads, in libssh2_session_handshake abnormal.
If you do not multi-threaded, no problem. How to use multi-thread calls libssh2?
source code:
#include "libssh2_config.h"
#include <libssh2.h>
#include <libssh2_sftp.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <fcntl.h>
#include <errno.h>
#include <stdio.h>
#include <ctype.h>
#include <pthread.h>
#ifndef __PRI64_PREFIX
#ifdef WIN32
#define __PRI64_PREFIX "I64"
#else
#if __WORDSIZE == 64
#define __PRI64_PREFIX "l"
#else
#define __PRI64_PREFIX "ll"
#endif
#endif
#endif
#ifndef PRIu64
#define PRIu64 __PRI64_PREFIX "u"
#endif
void* work_thread(void* arg)
{
unsigned long hostaddr;
int sock, i, auth_pw = 1;
struct sockaddr_in sin;
const char *fingerprint;
LIBSSH2_SESSION *session;
const char *username="ctst";
const char *password="ctst";
const char *sftppath="/home/ctst/xzm";
int rc;
LIBSSH2_SFTP *sftp_session;
#ifdef WIN32
WSADATA wsadata;
WSAStartup(MAKEWORD(2,0), &wsadata);
#endif
hostaddr = inet_addr("172.168.1.224");
rc = libssh2_init (0);
printf("libssh2_init..........\n") ;
if (rc != 0) {
fprintf (stderr, "libssh2 initialization failed (%d)\n", rc);
}
/*
* The application code is responsible for creating the socket
* and establishing the connection
*/
sock = socket(AF_INET, SOCK_STREAM, 0);
sin.sin_family = AF_INET;
sin.sin_port = htons(22);
sin.sin_addr.s_addr = hostaddr;
if (connect(sock, (struct sockaddr*)(&sin),
sizeof(struct sockaddr_in)) != 0) {
fprintf(stderr, "failed to connect!\n");
}
/* Create a session instance
*/
session = libssh2_session_init();
if(!session)
printf("libssh2_session_init..........\n") ;
/* ... start it up. This will trade welcome banners, exchange keys,
* and setup crypto, compression, and MAC layers
*/
printf("sock=%d\n", sock) ;
rc = libssh2_session_handshake(session, sock);
printf("libssh2_session_handshake..........\n") ;
if(rc) {
fprintf(stderr, "Failure establishing SSH session: %d\n", rc);
return NULL ;
}
printf("libssh2_session_handshake....success......\n") ;
libssh2_session_disconnect(session, "Normal Shutdown, Thank you for playing");
libssh2_session_free(session);
#ifdef WIN32
closesocket(sock);
#else
close(sock);
#endif
fprintf(stderr, "all done\n");
libssh2_exit();
return NULL ;
}
int main(int argc, char** argv)
{
int nthread = 1;
pthread_t tid;
printf("in main..........\n") ;
for(int i=0;i<nthread;i++)
{
pthread_create(&tid,NULL,work_thread,NULL);
}
for(int i=0; i<nthread;i++)
{
pthread_join(tid, NULL) ;
}
//work_thread(NULL) ;//normal
return 0 ;
}
2014-02-12 11:08:38
邢志明
BILLING产品中心
北京天源迪科信息技术有限公司
手机:18600578727
邮箱: xingzm_at_tydic.com
网址: http://www.tydic.com
地址: 北京市海淀区北四环西路56号辉煌时代大厦9F
邮编:100080
_______________________________________________
libssh2-devel http://cool.haxx.se/cgi-bin/mailman/listinfo/libssh2-devel
Received on 2014-02-12