Page 2 of 3 FirstFirst 123 LastLast
Results 11 to 20 of 21

Thread: My new sendCommand function with libcod (windows)

  1. #11
    Global Mossaderator Mitch's Avatar
    Join Date
    Nov 2012
    Posts
    654
    Thanks
    204
    Thanked 450 Times in 305 Posts
    Quote Originally Posted by Ni3ls View Post
    If have been trying it for a while now, but I cant get it working. What I did.

    It compiles with a lot of errors and I cant start a server
    Code:
    gsc_utils.cpp:1163: error: ‘BOOL’ does not name a type
    gsc_utils.cpp: In function ‘int isValidAddress(const char*)’:
    gsc_utils.cpp:1206: error: ‘isValidRange’ was not declared in this scope
    gsc_utils.cpp: In function ‘char* sendCommand(char*, int, char*)’:
    gsc_utils.cpp:1217: error: ‘SOCKET’ was not declared in this scope
    gsc_utils.cpp:1217: error: expected ‘;’ before ‘Socket’
    gsc_utils.cpp:1218: error: ‘WSADATA’ was not declared in this scope
    gsc_utils.cpp:1218: error: expected ‘;’ before ‘WSAData’
    gsc_utils.cpp:1219: error: ‘SOCKADDR_IN’ was not declared in this scope
    gsc_utils.cpp:1219: error: expected ‘;’ before ‘server’
    gsc_utils.cpp:1235: error: ‘server’ was not declared in this scope
    with a lot more "not declared" errors
    Those errors are because they are only used by Windows.

    You should look at examples how to setup a UDP client on Linux.

    http://www.linuxhowtos.org/C_C++/socket.htm
    http://www.binarytides.com/programmi...ckets-c-linux/

    If you need more help try googling 'linux c udp socket'.

    Edit: there is also a simpler way to validate the IP address:
    http://stackoverflow.com/questions/7...p-address-in-c
    PHP Code:
    #include <arpa/inet.h>

    bool isValidIpAddress(char *ipAddress)
    {
        
    struct sockaddr_in sa;
        
    int result inet_pton(AF_INETipAddress, &(sa.sin_addr));
        return 
    result != 0;

    Last edited by Mitch; 9th January 2016 at 15:35.

  2. The Following User Says Thank You to Mitch For This Useful Post:

    Ni3ls (9th January 2016)

  3. #12
    Corporal voron00's Avatar
    Join Date
    Nov 2014
    Posts
    248
    Thanks
    64
    Thanked 216 Times in 116 Posts
    PHP Code:
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <arpa/inet.h>

    bool isValidNumber(const char *charnumint **number)
    {
        
    int inum 0;

        if(
    charnum == NULL)
        {
            return 
    0;
        }

        for(
    0strlen(charnum); i++)
        {
            if(
    charnum[i] >= '0' && charnum[i] <= '9')
            {
                
    num += pow(10.0, (int)(strlen(charnum) - 1)) * (charnum[i] - '0');
            }
            else
            {
                return 
    0;
            }
        }

        *
    number =  (int *)num;

        return 
    1;
    }

    bool isValidRange(const char *range)
    {
        
    int *num;

        if(!
    isValidNumber(range, &num))
        {
            return 
    0;
        }

        if((int)
    num >= && (int)num <= 255)
        {
            return 
    1;
        }

        return 
    0;
    }

    int isValidAddress(const char *ip)
    {
        
    int idots 0;
        
    char *token;

        for(
    0strlen(ip); i++)
        {
            if(
    ip[i] == '.')
                
    dots++;
        }

        if(
    dots != 3)
        {
            return 
    0;
        }

        for(
    0dots 1i++)
        {
            if(
    == 0)
            {
                
    token strtok(strdup(ip), ".");
            }
            else
            {
                
    token strtok(NULL".");
            }
            if(!
    isValidRange(token))
            {
                return 
    0;
            }
        }

        return 
    1;
    }

    charsendCommand(char *ipint portchar *message)
    {
        
    int Socket;
        
    struct sockaddr_in server;
        
    struct timeval timeout;

        
    int i;
        
    char *data;
        
    char *fixed;
        
    char response[16384];

        
    data strcat(strdup("00000"), message);

        for(
    04i++)
        {
            
    data[i] = 0xFF;
        }
        
    data[i] = 0x02;

        
    server.sin_family AF_INET;
        
    server.sin_port htons(port);
        
    server.sin_addr.s_addr inet_addr(ip);

        
    timeout.tv_sec 2;
        
    timeout.tv_usec 2;

        
    Socket socket(AF_INETSOCK_DGRAMIPPROTO_UDP);
        if(
    Socket == -1)
        {
            return 
    "0";
        }

        if(
    setsockopt(SocketSOL_SOCKETSO_RCVTIMEO, (char *)&timeoutsizeof(timeout)) == -1)
        {
            
    close(Socket);
            return 
    "0";
        }

        if(
    setsockopt(SocketSOL_SOCKETSO_SNDTIMEO, (char *)&timeoutsizeof(timeout)) == -1)
        {
            
    close(Socket);
            return 
    "0";
        }

        if(
    connect(Socket, (struct sockaddr *)&serversizeof(server)) == -1)
        {
            
    close(Socket);
            return 
    "0";
        }

        
    sendto(Socketdatastrlen(data), 0, (struct sockaddr *)&serversizeof(server));
        
    recvfrom(Socketresponsesizeof(response), 000);

        if(
    == -1)
        {
            
    close(Socket);
            return 
    "0";
        }

        
    response[i] = '\0';

        
    close(Socket);

        
    fixed strtok(response"\\");
        
    fixed strtok(NULL"\0");

        for(
    0strlen(fixed); i++)
        {
            if(
    fixed[i] == '\n')
            {
                
    fixed[i] = '\t';
            }
            if(
    fixed[i] == '"')
            {
                
    fixed[i] = '\t';
            }
            if(
    fixed[i] == '\\')
            {
                
    fixed[i] = '\t';
            }
        }

        return 
    fixed;
    }

    void gsc_utils_sendCommand()
    {
        
    char *ip;
        
    char *data;
        
    int port 0;

        if(!
    stackGetParams((char *)"sis", &ip, &port, &data) || strlen(ip) == || port == || strlen(data) == 0) {
            
    stackPushUndefined();
            return;
        }

        if(
    isValidAddress(ip))
            
    stackPushString(sendCommand(ipportdata));
        else
            
    stackPushInt(0);

    PHP Code:
    resp sendCommand("185.58.205.123"28960"getstatus"); 
    array = 
    StrTok(resp"\t"); 
    for (
    i=0;i<array.size;i++)
    {
        
    printfline("[%]: %"i, array[i]);

    Works:
    Click image for larger version. 

Name:	Снимок экрана (8).png 
Views:	49 
Size:	42.7 KB 
ID:	1010

    But not sure about stability since i only done a quick-test, you may go ahead and improve this code if you need this
    Last edited by voron00; 9th January 2016 at 15:52.