Code working but not after 1 day

This is a discussion on Code working but not after 1 day within the C Programming forums, part of the General Programming Boards category; Hi all, First, I am new to this forum I've just started playing with the C language. And I wanted ...

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    2

    Code working but not after 1 day

    Hi all,

    First, I am new to this forum
    I've just started playing with the C language. And I wanted to create a script which will keep my game server online. The script is working fine. It checks if the port is in use and then sends a players status command to the server. If the server doesnt respond the process will get killed and restarted.
    After a while, lets say 1 day the script doesnt know anymore if the server is online it keeps killing the process over and over even when it is online.
    I thought that it may be had to do with the sockets which I need to close or something, to many sockets opened?
    Here is a piece of code:

    Code:
    peer.sin_family      = AF_INET;
    from = atoi("****");
    
    check = 0;
    peer.sin_addr.s_addr = resolv("***.***.***.***");
    peer.sin_port = htons(from);
    printf("All done, Please sit back while I'll keep an eye on your server.\n");
    while( 1 < 3) {
    	sleep(PLENTYOFSEC);
    	sd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
    	if(sd < 0) std_err();
    	if(sendto(sd, "\\status\\", 8, 0, (struct sockaddr *)&peer, sizeof(peer))< 0) std_err();
    	if(timeout(sd, TIMEOUT) < 0 ) {
    		printf("\nServer IS NOT running!!! Count: %hu \n\n ", check);
    		system("killall process.exe");
    		system("sh restart.sh");
    		check ++;
    	    } else {
    		if (scan == check) {
    			fputs(".", stdout);
    		}
    	}
    close(sd);
    }
    Thanks in advance.
    Last edited by sheepilja; 12-12-2010 at 09:35 AM. Reason: Code edit.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    You don't close the socket if scan is not equal to check, whatever scan may be. Also, UDP is unreliable; if it doesn't reply it doesn't mean it's down. You should at least try it several times before you assume it's not up.

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    2
    Okay thats right yes not in this piece, but wouldnt it be the best way to put
    Code:
    close(sd);
    in the end of the loop?

    Edit:
    Okay ive changed the code now. Putted the socket close in the end of the loop.
    But isnt it a bit strange that it works the first couple of hours well and after it then not? I will try to check the udp connection multiple times thanks.
    Last edited by sheepilja; 12-12-2010 at 09:36 AM.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by sheepilja View Post
    Okay thats right yes not in this piece, but wouldnt it be the best way to put
    Code:
    close(sd);
    in the end of the loop?

    Okay ive changed the code now. Putted the socket close in the end of the loop.
    But isnt it a bit strange that it works the first couple of hours well and after it then not? I will try to check it multiple times thanks.
    No that's not strange. At one point, you will have so many sockets open that the operating system will refuse to provide you more. "socket()" should fail, then, though.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. annoying bugs on working code
    By sammyfallows in forum C++ Programming
    Replies: 5
    Last Post: 11-20-2010, 08:05 PM
  2. Code for deleting a tree node not working
    By Mini in forum C Programming
    Replies: 2
    Last Post: 10-09-2010, 05:16 AM
  3. Replies: 8
    Last Post: 03-29-2010, 04:38 AM
  4. my code is not working
    By turbot in forum C Programming
    Replies: 3
    Last Post: 01-28-2009, 04:23 AM
  5. Code to find the day type of day in the year?
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 04-01-2002, 07:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21