最近小弟正研究BT下载代码,在网上找到了比较易懂的BitZam程序代码,调用的BT库是FTKernel_API,由于本人程序不需要MFC的界面,故简化了代码,没想到程序下载后却一直没速度,查找日志发现这样的语句:
3 20110330213630296 Using random peer ID...<
1 20110330213630296 10093:Failed to create socket handle!<
3 20110330213943781 Releasing context...<
3 20110330213943781 Released...<
不知各位大虾有没有用过FTKernel_API代码的,上述错误是什么意思呢?create socket handle调用的是什么函数呢?
附上相关代码
void BTdownLoad(char pTorrentPathNamef[256], char pFilePathNamef[256])
{
CString m_strKeyValue;
QWORD m_qwTotalFileSize;
DWORD m_dwPieceCount;
HDownloader m_hDownloader;
CString strValue1;
CString strValue2;
CString strValue3;
int i,k;
int t7 = 0;
time_t t1,t2,t3,t4,t5,t6;
CString m_strPassword = "";
//ASSERT( NULL == m_hDownloader );
    CString strPath;
    ::GetModuleFileName( NULL,
                         strPath.GetBuffer( _MAX_PATH ), _MAX_PATH );
    strPath.ReleaseBuffer();
int nPos = strPath.ReverseFind( _T('\\') );
ASSERT( -1 != nPos );
if ( !FTK_Context_Init( strPath + _T( "Config.ini" ) ) )
{
printf("初始化失败!");
return ;
}
ASSERT( FTK_Context_IsRunning() == FALSE );
FTK_Context_Run();
CString strStartupPath = strPath.Left( nPos + 1 );
HTorrentFile hTorrentFile = FTK_Torrent_Open( pTorrentPathNamef, CP_ACP, m_strPassword.IsEmpty() ? NULL : (LPCTSTR)m_strPassword );
        if ( NULL == hTorrentFile )//执行从文件中读取数据并初始化对象操作
{
printf("下载结果:fail\n");
//return FALSE;
}
CByteArray arryPreAllocFile;
arryPreAllocFile.SetSize( FTK_Torrent_GetFilesCount( hTorrentFile ) );
memset( arryPreAllocFile.GetData(), 0x00, arryPreAllocFile.GetSize() );
m_strKeyValue = FTK_Torrent_GetHexInfoHash( hTorrentFile );//获取Torrent文件的InfoHash值的hex编码后的字符串表示,返回的字符串中包含%字符
m_strKeyValue.Replace( _T("%"), _T("") );
        m_hDownloader = FTK_Downloader_Open();
        time(&t1);
        printf("FTK Downloader Open\n");
        BOOL bRet = FTK_Downloader_Init( m_hDownloader,
           pTorrentPathNamef,
                   arryPreAllocFile.GetData(),
   arryPreAllocFile.GetSize(),
           FALSE,
                   pFilePathNamef,
strStartupPath + _T( "Config.INI" ),
strStartupPath + _T( "Log\\" ) + _T( "Downloader.log" ),
strStartupPath + m_strKeyValue + _T( ".status" ),
m_strPassword.IsEmpty() ? NULL : (LPCTSTR)m_strPassword,
CP_ACP );
    if ( bRet == FALSE )
    {
        FTK_Downloader_Close( m_hDownloader );//释放一个下载句柄
        m_hDownloader = NULL;
printf("下载结果:fail\n");
return;
    }
    bRet = FTK_Downloader_Execute( m_hDownloader );//执行下载线程
    if ( bRet == FALSE )
    {
        FTK_Downloader_Close( m_hDownloader );//释放一个下载句柄
        m_hDownloader = NULL;
printf("下载结果:fail\n");
return;
    }
cout<<bRet<<endl;
FTK_GlobalVar_AddTorrentSHA1( m_strKeyValue, m_hDownloader );//将某个Torrent文件的SHA1值加入队列
printf("将Torrent文件的SHA1值加入队列\n");
m_qwTotalFileSize = FTK_Torrent_GetFileSize( hTorrentFile );//获取文件的大小
printf("获取文件的大小\n");
int m_qwTotalFileSizeM = m_qwTotalFileSize/(1024*1024); printf ("%d", m_qwTotalFileSizeM);
printf ("MB\n");
m_dwPieceCount = FTK_Torrent_GetPieceCount( hTorrentFile );//Piece块的个数
printf("Piece块的个数\n");
cout<<m_dwPieceCount<<endl;
for(i=1; i<=36000000; i++)
{
      time(&t4);
      delay(1); QWORD qwTotalFileHaveSize = FTK_Stat_GetTotalFileHaveSize( m_hDownloader );//获取目前所有已经下载完成的数据的大小(字节)
int nPos = PERCENT( (INT64)qwTotalFileHaveSize, (INT64)m_qwTotalFileSize );//百分比
int qwTotalFileHaveSizeM = qwTotalFileHaveSize/(1024*1024); if ( nPos >= 100 )
{
FTK_GlobalVar_RemoveTorrentSHA1( m_strKeyValue );//将某个Torrent文件的SHA1值从队列中删除
        FTK_Downloader_Close( m_hDownloader );//释放一个下载句柄
        m_hDownloader = NULL;
printf("下载结果:success\n");
printf("下载文件的大小:");
printf ("%d", m_qwTotalFileSizeM);
        printf ("MB\n");
time(&t2);
t3 = t2 - t1;
int timeh = t3 / ( 60 * 60 );
    int timem = t3 % ( 60 * 60 ) / 60;
    int times = t3 % ( 60 * 60 ) % 60;
printf("文件的总下载时间为:");
cout<<timeh<<"h"<<timem<<"m"<<times<<"s"<<endl;
//cout<<i;

return;
} strValue1 = AfxFormatBytes( FTK_Stat_GetDownloadRate( m_hDownloader ) );//获取当前的下载率 int time0 = ( UINT32 )FTK_Stat_GetLeftTime( m_hDownloader );
int time1 = time0 / ( 60 * 60 );
int time2 = time0 % ( 60 * 60 ) / 60;
int time3 = time0 % ( 60 * 60 ) % 60;
/*printf("剩余下载时间:");
cout<<time1<<"h"<<time2<<"m"<<time3<<"s"<<endl;*/ time(&t5);
t6 = t5 - t4;
t7 = t7 + t6;
int timehh = t7 / ( 60 * 60 );
int timemm = t7 % ( 60 * 60 ) / 60;
int timess = t7 % ( 60 * 60 ) % 60; k = i % 10;
//cout<<k<<endl; if ( k == 0 )
{
cout<<strValue1<<endl;//下载速率
printf("已经下载完成的数据的大小:");
    //int qwTotalFileHaveSizeM = qwTotalFileHaveSize/(1024*1024);
    //int qwTotalFileHaveSizeM = qwTotalFileHaveSize;
    printf ("%d", qwTotalFileHaveSizeM);
      printf ("MB\n");
     printf("已经下载完成的数据的百分比:");
     cout<<nPos<<"%"<<endl;
//cout<<strValue1<<endl;//下载速率
printf("剩余下载时间:");
     cout<<time1<<"h"<<time2<<"m"<<time3<<"s"<<endl;
printf("已经下载的时间为:");
cout<<timehh<<"h"<<timemm<<"m"<<timess<<"s"<<endl;
}
}}

解决方案 »

  1.   

    各位大侠,怎么没人回答呀 现在问题就集中在10093:Failed to create socket handle!这里,为什么创建SOCKET失败了呢?
      

  2.   

    去网上查过了,没找到完全一致的问题,大概说法是创建SOCKET出问题了,但是我的程序关于SOCKET都是封装起来的,不知道具体在哪个函数中处理的,所以看看有没有人用过BitZam程序的,帮忙看一下。