Server status utility | |
JavaGuy - Student ![]() |
I'm working on a Linux command-line utility that will show the server status. This is a first draft and does not format the output or anything, just gives you a straight screen dump of the server status, but that will change. For now the IP and port number of the Academy server are hard-coded into it. Eventually you'll be able to use it to check on other servers, though why anybody would want to do that I can't imagine. It compiles with gcc and needs nothing beyond the standard Linux environment. This might be the JA's first open-source software project, so anybody who wants to tinker with it, feel free, and post source code for any improvements you make. [i]#include <stdio.h> #include <string.h> #include <netdb.h> #include <sys/socket.h> #include <unistd.h> // #define buflen 512 #define buflen 4096 //unsigned int portno = 27960; unsigned int portno = 29068; char hostname[] = "198.247.172.102"; char request[] = "xxxxgetstatus\n"; char *buf[buflen]; /* declare global to avoid stack */ main() { //First four chars must be hex FF request[0] = 255; request[1] = 255; request[2] = 255; request[3] = 255; int sd = socket(AF_INET, SOCK_DGRAM, 0); /* init socket descriptor */ struct sockaddr_in sin; struct hostent *host = gethostbyname(hostname); char buf[buflen]; int len; int addr_len = sizeof(sin); /*** PLACE DATA IN sockaddr_in struct ***/ memcpy(&sin.sin_addr.s_addr, host->h_addr, host->h_length); sin.sin_family = AF_INET; sin.sin_port = htons(portno); /*** CONNECT SOCKET TO THE SERVICE DESCRIBED BY sockaddr_in struct ***/ if (connect(sd, (struct sockaddr *)&sin, sizeof(sin)) < 0) { perror("connecting" ![]() exit(1); } sleep(1); /* give server time to reply */ sendto(sd,request,strlen(request), 0, (struct sockaddr *) &sin, addr_len); write(1,buf,len); if ((len=recvfrom(sd,buf, buflen-1, 0, (struct sockaddr *)&sin, &addr_len)) == -1) { perror("recvfrom" ![]() exit(1); } buf[buflen-1]='\0'; puts(buf); close(sd); } [/i] _______________ My signature is only one line. You're welcome. This post was edited by JavaGuy on Mar 04 2004 04:29am. |
Login and add your comment! |
Comments |
_cmad_ - Ex-Student ![]() |
yeah JavaGuy check qstat. Now i'm off to school so I'll give you feedback on your program in about 9 hrs ![]() _______________ Your friends of today, are your enemies of tomorrow. |
DJ Sith - Jedi Council ![]() |
Doesn't qstat already do this? I'd hate for you to reinvent the wheel. ![]() Although a custom PHP script thats more geared to our use than livestats would be nice. ![]() _______________ My car is made of Nerf. This comment was edited by DJ Sith on Mar 04 2004 05:03am. |
JavaGuy - Student ![]() |
Oh, the sleep(1) line isn't necessary, and it actually works better without it. _______________ My signature is only one line. You're welcome. |
Login and add your comment! |