예제 #1
0
파일: hbinet.c 프로젝트: diegopego/core
static void hb_inetConnectInternal( HB_BOOL fResolve )
{
   const char * szHost = hb_parc( 1 );
   char * szAddr = NULL;
   PHB_SOCKET_STRUCT socket = HB_PARSOCKET( 3 );
   PHB_ITEM pSocket = NULL;
   int iPort = hb_parni( 2 );

   if( szHost == NULL || iPort == 0 || ( socket == NULL && ! HB_ISNIL( 3 ) ) )
      hb_inetErrRT();
   else
   {
      if( ! socket )
         HB_SOCKET_INIT( socket, pSocket );
      else if( socket->sd != HB_NO_SOCKET )
         hb_inetCloseSocket( socket, HB_FALSE );

      if( fResolve )
         szHost = szAddr = hb_socketResolveAddr( szHost, HB_SOCKET_AF_INET );

      if( fResolve && ! szAddr )
      {
         hb_inetGetError( socket );
         if( socket->iError == 0 )
            socket->iError = HB_SOCKET_ERR_WRONGADDR;
      }
      else
      {
         /* Creates comm socket */
         socket->sd = hb_socketOpen( HB_SOCKET_PF_INET, HB_SOCKET_PT_STREAM, 0 );
         if( socket->sd == HB_NO_SOCKET )
            hb_inetGetError( socket );
         else
         {
            if( socket->remote )
               hb_xfree( socket->remote );
            if( hb_socketInetAddr( &socket->remote, &socket->remotelen,
                                   szHost, iPort ) )
            {
               hb_socketSetKeepAlive( socket->sd, HB_TRUE );
               if( hb_socketConnect( socket->sd, socket->remote, socket->remotelen,
                                     socket->iTimeout ) != 0 )
                  hb_inetGetError( socket );
               else
                  socket->iError = HB_INET_ERR_OK;
            }
            else
               hb_inetGetError( socket );
         }
         if( szAddr )
            hb_xfree( szAddr );
      }
      if( pSocket )
         hb_itemReturnRelease( pSocket );
      else
         hb_itemReturn( hb_param( 3, HB_IT_ANY ) );
   }
}
예제 #2
0
파일: hbip.c 프로젝트: alkresin/letofc
HB_SOCKET_T hb_ipConnect( char * szHost, int iPort, int timeout )
{
   HB_SOCKET_T hSocket = -1;
   ULONG ulAddr;
   struct sockaddr_in remote;

   HB_SOCKET_ZERO_ERROR();

   ulAddr = hb_getAddr( szHost );

   /* error had been set by get hosts */
   if( ulAddr != INADDR_NONE )
   {
      /* Creates comm socket */
#if defined(HB_OS_WIN_32)
      hSocket = socket( AF_INET, SOCK_STREAM, 0);
#else
      hSocket = socket( PF_INET, SOCK_STREAM, 0);
#endif

      if( hSocket == ( HB_SOCKET_T ) -1 )
      {
         HB_SOCKET_SET_ERROR();
      }
      else
      {
         remote.sin_family = AF_INET;
         remote.sin_port= iPort;
         remote.sin_addr.s_addr = ulAddr;


         /* Set internal socket send buffer to 64k,
         * this should fix the speed problems some users have reported
         */
         hb_ipSetBufSize( hSocket, 65536, 65536 );

         hb_socketConnect( hSocket, &remote, timeout );       
      }
   }
   return hSocket;
}
예제 #3
0
파일: tcpio.c 프로젝트: jiangxilong/core
static PHB_FILE s_fileOpen( PHB_FILE_FUNCS pFuncs, const char * pszName,
                            const char * pszDefExt, HB_FATTR nExFlags,
                            const char * pPaths, PHB_ITEM pError )
{
   const char * pszHost = pszName + FILE_PREFIX_LEN, * ptr;
   PHB_FILE pFile = NULL;
   HB_ERRCODE errcode = 0;
   HB_SIZE nLen = 0;
   int iPort = 0;
   HB_MAXINT timeout = -1;

   HB_SYMBOL_UNUSED( pFuncs );
   HB_SYMBOL_UNUSED( pszDefExt );
   HB_SYMBOL_UNUSED( pPaths );

   if( ( ptr = strchr( pszHost, ':' ) ) != NULL && ptr != pszHost )
   {
      nLen = ptr - pszHost;
      ++ptr;
      while( HB_ISDIGIT( * ptr ) )
         iPort = iPort * 10 + ( * ptr++ - '0' );

      if( * ptr == ':' )
      {
         ++ptr;
         while( HB_ISDIGIT( * ptr ) )
            timeout = HB_MAX( timeout, 0 ) * 10 + ( * ptr++ - '0' );
      }

      if( * ptr != 0 && * ptr != ':' )
         iPort = 0;
   }

   if( iPort > 0 )
   {
      char * pszAddr, * pszIpAddr;

      hb_socketAutoInit();

      pszAddr = hb_strndup( pszHost, nLen );
      pszIpAddr = hb_socketResolveAddr( pszAddr, HB_SOCKET_AF_INET );
      hb_xfree( pszAddr );

      if( pszIpAddr )
      {
         HB_SOCKET sd = hb_socketOpen( HB_SOCKET_PF_INET, HB_SOCKET_PT_STREAM, 0 );
         if( sd != HB_NO_SOCKET )
         {
            void * pSockAddr;
            unsigned uiLen;

            if( hb_socketInetAddr( &pSockAddr, &uiLen, pszIpAddr, iPort ) )
            {
               hb_socketSetKeepAlive( sd, HB_TRUE );
               if( hb_socketConnect( sd, pSockAddr, uiLen, timeout ) == 0 )
               {
                  PHB_SOCKEX sock;

                  switch( nExFlags & ( FO_READ | FO_WRITE | FO_READWRITE ) )
                  {
                     case FO_READ:
                        hb_socketShutdown( sd, HB_SOCKET_SHUT_WR );
                        break;
                     case FO_WRITE:
                        hb_socketShutdown( sd, HB_SOCKET_SHUT_RD );
                        break;
                  }
                  sock = hb_sockexNew( sd, NULL, NULL );
                  if( sock )
                  {
                     hb_sockexSetShutDown( sock, HB_TRUE );
                     hb_sockexSetAutoFlush( sock, HB_TRUE );
                     pFile = s_fileNew( sock, timeout );
                     sd = HB_NO_SOCKET;
                  }
               }
               hb_xfree( pSockAddr );
            }
            if( sd != HB_NO_SOCKET )
            {
               errcode = hb_socketGetError();
               hb_socketClose( sd );
            }
         }
         hb_xfree( pszIpAddr );
      }
      if( errcode == 0 && pFile == NULL )
         errcode = hb_socketGetError();
   }
   else
      errcode = HB_SOCKET_ERR_WRONGADDR;

   hb_fsSetError( errcode );

   if( pError )
   {
      hb_errPutFileName( pError, pszName );
      if( pFile == NULL )
      {
         hb_errPutOsCode( pError, errcode );
         hb_errPutGenCode( pError, ( HB_ERRCODE ) EG_OPEN );
      }
   }

   return pFile;
}