cmdish/src/cmdish.cxx
//---------------------------------------------------------------------------
// Find the given service in name server.
// Check if this service exists and is accessible.
//    Returns: -1 - no name server,
//              0 - service is not running or is not accessible,
//              1 - service exists and is accessible.
//---------------------------------------------------------------------------
Find the given service in name server.
Check if this service exists and is accessible.
Returns: -1 - no name server,
0 - service is not running or is not accessible,
1 - service exists and is accessible.
Find the given service in name server . Check if this service exists and is accessible . Returns : - 1 - no name server 0 - service is not running or is not accessible 1 - service exists and is accessible .
int findService(const char *service_name, int &service_port, int &tcp_addr) {
    _DBG(fprintf(log_fd, "  Checking service '%s'...\n", service_name));
    int res = NameServerGetService(service_name, service_port, tcp_addr);
    if(res == 0 || res == -1) {
        _DBG(fprintf(log_fd, "    Service '%s' doesn't exist.\n",
                     service_name));
        return res;
    }
    _DBG(fprintf(log_fd, "    Service '%s' exists.\n", service_name));
    _DBG(fprintf(log_fd, "    Creating socket to connect through...\n"));
    int sock = socket(PF_INET, SOCK_STREAM, 0);
    if(sock < 0) {
        _DBG(fprintf(log_fd, "    Unable to create socket!\n"));
        return 0;
    }
    struct sockaddr_in serv_addr;
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port = htons(service_port);
#if 1
    serv_addr.sin_addr.s_addr = htonl(tcp_addr);
#else
    serv_addr.sin_addr.S_un.S_addr = htonl(tcp_addr);
#endif
    int e = connect(sock, (struct sockaddr *)&serv_addr, sizeof(sockaddr_in));
    closesocket(sock);
    if(e < 0) {
        _DBG(fprintf(log_fd, 
                     "    Unable to connect to '%s' trough socket!\n",
                     service_name));
        return 0;
    }
    return 1;
}
int findService ( const char * service_name , int & service_port , int & tcp_addr ) { _DBG ( fprintf ( log_fd , " \n " , service_name ) ) ; int res = NameServerGetService ( service_name , service_port , tcp_addr ) ; if ( res == 0 || res == -1 ) { _DBG ( fprintf ( log_fd , " \n " , service_name ) ) ; return res ; } _DBG ( fprintf ( log_fd , " \n " , service_name ) ) ; _DBG ( fprintf ( log_fd , " \n " ) ) ; int sock = socket ( PF_INET , SOCK_STREAM , 0 ) ; if ( sock < 0 ) { _DBG ( fprintf ( log_fd , " \n " ) ) ; return 0 ; } struct sockaddr_in serv_addr ; serv_addr . sin_family = AF_INET ; serv_addr . sin_port = htons ( service_port ) ; #if 1 serv_addr . sin_addr . s_addr = htonl ( tcp_addr ) ; #else serv_addr . sin_addr . S_un . S_addr = htonl ( tcp_addr ) ; #endif int e = connect ( sock , ( struct sockaddr * ) & serv_addr , sizeof ( sockaddr_in ) ) ; closesocket ( sock ) ; if ( e < 0 ) { _DBG ( fprintf ( log_fd , " \n " , service_name ) ) ; return 0 ; } return 1 ; }
Find the given service in name server.
Find the given service in name server .
// Ask name server about this service... // Create socket... // Open socket... // Service exist and is accessible.