Luigi Auriemma

aluigi.org (ARCHIVE-ONLY FORUM!)
It is currently 19 Jul 2012 19:06

All times are UTC [ DST ]





Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: is online function
PostPosted: 18 Sep 2009 15:08 

Joined: 05 Jan 2009 13:01
Posts: 6
Hey Luigi i got a new request. Can you please write a C function that looks if a Gameserver is online or not and returns 1 if online or 0 if offline?

int IsServerOnline(char *ip, int port)


Top
 Profile  
 
 
 Post subject: Re: is online function
PostPosted: 18 Sep 2009 15:32 

Joined: 13 Aug 2007 21:44
Posts: 4068
Location: http://aluigi.org
each game uses its own protocol so usually you must send the query request to the server IP:port and if you receive no reply it's probably not online, exactly like I did in all my proof-of-concepts till the end of 2008 (so depending by the game you can check the methods I used in those codes).

exists also another simpler way to check if a server is online but it's not guarantee at 100%, practically:
Code:
   if(sendto(sd, "", 0, 0, (struct sockaddr *)&peer, sizeof(struct sockaddr_in)) < 0) {
     printf("offline\n");
   } else {
     printf("online\n");
   }
it works because if the server offline your client will receive the icmp port unreachable error but it doesn't work if the server (the system, not the server program) is offline or if you filter the incoming icmp packets.
so although it's useles I wanted to show also this possibility.


Top
 Profile  
 
 Post subject: Re: is online function
PostPosted: 18 Sep 2009 17:03 

Joined: 05 Jan 2009 13:01
Posts: 6
Doesn't work at me, it always shows offline, no matter if server is online or not. Did I do something wrong? Here's my code:

Code:
int IsServerOnline(char *ip, int port) {
    struct sockaddr_in  peer;
    int     sd;

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

    if(sendto(sd, "", 0, 0, (struct sockaddr *)&peer, sizeof(struct sockaddr_in)) < 0) {
    return 0;
   } else {
    return 1;
   }
}


Also, if theres no simple possibility for all engines, I want it for q3.


Top
 Profile  
 
 Post subject: Re: is online function
PostPosted: 18 Sep 2009 18:05 

Joined: 03 May 2009 04:22
Posts: 33
What your program says is: Your sendto fails. It cant send a packet.
So try to see what is the last error whit this function: http://msdn.microsoft.com/en-us/library/ms741580(VS.85).aspx

Maybe it???s your firewall which blocks packets.

ps: You send a packet and dont await for for servers respons. offcourse you must recvfrom


Top
 Profile  
 
 Post subject: Re: is online function
PostPosted: 18 Sep 2009 18:48 

Joined: 13 Aug 2007 21:44
Posts: 4068
Location: http://aluigi.org
Code:
int timeout(int sock, int secs) {
    struct  timeval tout;
    fd_set  fdr;
    int     err;

    tout.tv_sec  = secs;
    tout.tv_usec = 0;
    FD_ZERO(&fdr);
    FD_SET(sock, &fdr);
    err = select(sock + 1, &fdr, NULL, NULL, &tout);
    if(err <= 0) return(-1);
    return(0);
}

int IsServerOnline(char *ip, int port) {
    struct sockaddr_in  peer;
    int     sd,
            ret = 0;

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

    sd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
    if(
      (sd >= 0) &&  // valid socket + packet sent + reply received
      (sendto(sd, "\xff\xff\xff\xff" "getinfo xxx\n", 16, 0, (struct sockaddr *)&peer, sizeof(struct sockaddr_in)) >= 0) &&
      !timeout(sd, 2)) {
        ret = 1;
    }
    closesocket(sd);
    return(ret);
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for: