Thread: how do I know if a server (on the box) is running?

  1. #1
    Registered User jaro's Avatar
    Join Date
    Mar 2006
    Location
    In my Parent House
    Posts
    34

    Question how do I know if a server (on the box) is running?

    Hi again,

    Lately I'm doing socket programming, as far I can tell it works fine.
    the problem is that how do I know if a server (on the box) is running.
    The gethostbyname() will only detect if a IP address exist or is currently running.

    The only idea that I have is, after gethostbyname() and a successful connect() my program will send some sort of message, then the (workspace)server will then send a valid response. If no valid response is recieve then the process will start all over again.

    is this the way to go, just to know if a (workspace)server is running or there is another way ?

    regards,
    jaro

  2. #2
    Registered User jaro's Avatar
    Join Date
    Mar 2006
    Location
    In my Parent House
    Posts
    34
    here is the code that I've done so far.

    Code:
    #include <stdio.h>
    #include <winsock.h> 
    
    #define STRSIZE 16536
    
    void handle_error(void);
    void receiveMessage(void);
    void connectToWorkSpace();
    
    
    char recvbuf[60000] = "";
    SOCKET ConnectSocket;
    struct sockaddr_in clientService; 
    
    int bytesSent;
    int bytesRecv = 0;
    char sendbuf[500] = "";
     
    struct hostent *h; 
    char HOSTNME[20]="";
    char hostname[20] = "localhost";			// for testing purposes
    char hostnamebackup[20] = "local-blahblah";	// for testing purposes
    
    int chker=0;
    
    int main() 
    {
    	connectToWorkSpace();		
    		
    		while(1){
    			receiveMessage();
    		}
    
    }
    
    
    void receiveMessage()
    {
    	int bytesRecv = 0;
    
    
    	bytesRecv = recv( ConnectSocket, recvbuf, 60000, 0 );
    
    	// check if server is still up
    	if (bytesRecv == 0 ||  
    		bytesRecv == SOCKET_ERROR ||
    		WSAGetLastError() == WSAEINTR ||		/*(has been closed)*/
    		WSAGetLastError() == WSAENOTCONN ||		/*(not connected)*/
    		WSAGetLastError() == WSAENETRESET ||	/*(kepp-alive service failed and broke connection)*/
    		WSAGetLastError() == WSAESHUTDOWN ||	/*(the socket was shutdown() peacefully)*/
    		WSAGetLastError() == WSAECONNABORTED ||	/*(something died and the connection was broken)*/
    		WSAGetLastError() == WSAETIMEDOUT ||	/*(the network died or the other computer stopped responding)*/
    		WSAGetLastError() == WSAECONNRESET		/*(something died on the remote computer and broke the connection)*/
    		){
    			printf("Reconnect!\n");
    			connectToWorkSpace();
    	}else{
    		recvbuf[bytesRecv]='\0';
    			if(strlen(recvbuf)>0){
    				printf("received message: %s \n", recvbuf);
    			}
    	}
    
    }
    
    
    
    void connectToWorkSpace()
    {
    
    int chkHost=0;
    int loopHost=0;
    int conn = 1;
    
    	WSADATA wsaData;
    	WORD wVersionRequested;               
    	wVersionRequested = MAKEWORD( 1, 1 );
    
    
    	if ( WSAStartup( wVersionRequested, &wsaData ) != 0 ){
    		fprintf(stderr,"WSAStartup Error..."); 
    		handle_error();
    	}
    
    	// will loop until connect() is successful
    	while(conn != 0){
    
    		chkHost =chker%2;
    
    		if(chkHost == 0){
    			printf("Connect Using Primary Host\n");
    			strcpy(HOSTNME,hostname);
    		}
    		else {
    			printf("Connect Using Backup Host\n");
    			strcpy(HOSTNME,hostnamebackup);
    		}
    
    		// will loop until HOSTNME is reachable
    		while(loopHost==0){
    			chkHost =chker%2;
    			if((h=gethostbyname(HOSTNME))==NULL)
    			{ 
    					printf("%s not reachable\n",HOSTNME);
    					
    					if(chkHost == 1){
    						printf("Connect Using Primary Host\n");
    						strcpy(HOSTNME,hostname);
    					}
    					else {
    						printf("Connect Using Backup Host\n");
    						strcpy(HOSTNME,hostnamebackup);
    					}
    					
    			}else{
    				loopHost=1;
    			}
    			Sleep(1000);
    			chker++;
    		}
    
    			
    
    		clientService.sin_addr=*((struct in_addr*)h->h_addr); 
    		clientService.sin_port = htons(8770);
    		clientService.sin_family = AF_INET;
    
    
    			
    		// Create a SOCKET for connecting to server
    		ConnectSocket = socket(AF_INET, SOCK_STREAM, 0);
    		if (ConnectSocket == -1) {
    			handle_error();
    			printf("Error at socket(): \n");
    			//return -1;
    		}
    
    		// Connect 
    		if( connect( ConnectSocket, (struct sockaddr*) &clientService, sizeof(struct sockaddr) ) == -1) {
    			printf("Failed to connect. Workspaceserver is down.\n" );
    			printf("Reconnecting........\n" );										
    			loopHost=0;
    			Sleep(5000);	
    			conn = 1;
    		}
    		else {
    			conn = 0;
    			printf("Connection Success!\n");
    		}
    
    	}
    
    	strcpy(sendbuf,"jaro");
    	strcat(sendbuf, " :username");
    	strcat(sendbuf, "\n");
    
    	/* Send */
    	bytesSent = send( ConnectSocket, sendbuf, strlen(sendbuf), 0 );
    	printf( "\n messages Sent: %s\n", sendbuf );				
    }
    
    
    void handle_error(void)
    {
      /*
       * Errors are handled by calling the WSAGetLastError routine which 
       * will return the last error as one of the following. As we develop
       * this tutorial, we will go into much more detail on what they mean
       * and what caused them.
       */
    	int x;
    	x= WSAGetLastError();
    
      switch ( x )
      {
        case WSANOTINITIALISED :
          printf("Unable to initialise socket.\n");
        break;
        case WSAEAFNOSUPPORT :
          printf("The specified address family is not supported.\n");
        break;
        case WSAEADDRNOTAVAIL :
          printf("Specified address is not available from the local machine.\n");
        break;
        case WSAECONNREFUSED :
          printf("The attempt to connect was forcefully rejected.\n"); 
          break; 
        case WSAEDESTADDRREQ : 
          printf("address destination address is required.\n");
        break;
        case WSAEFAULT :
          printf("The namelen argument is incorrect.\n");
        break;
        case WSAEINVAL :
          printf("The socket is not already bound to an address.\n");
        break;
        case WSAEISCONN :
          printf("The socket is already connected.\n");
        break;
        case WSAEADDRINUSE :
          printf("The specified address is already in use.\n");
        break;
        case WSAEMFILE : 
          printf("No more file descriptors are available.\n");
        break;
        case WSAENOBUFS :
          printf("No buffer space available. The socket cannot be created.\n");
        break;
        case WSAEPROTONOSUPPORT :
          printf("The specified protocol is not supported.\n");
          break; 
        case WSAEPROTOTYPE :
          printf("The specified protocol is the wrong type for this socket.\n");
        break;
        case WSAENETUNREACH : 
          printf("The network can't be reached from this host at this time.\n");
        break; 
        case WSAENOTSOCK :
           printf("The descriptor is not a socket.\n");
        break;
        case WSAETIMEDOUT :
          printf("Attempt timed out without establishing a connection.\n");
        break;
        case WSAESOCKTNOSUPPORT :
           printf("Socket type is not supported in this address family.\n");
        break;
        case WSAENETDOWN :
          printf("Network subsystem failure.\n");
        break;
        case WSAHOST_NOT_FOUND :
          printf("Authoritative Answer Host not found.\n");
        break;
        case WSATRY_AGAIN :
          printf("Non-Authoritative Host not found or SERVERFAIL.\n");
         break;
        case WSANO_RECOVERY :
           printf("Non recoverable errors, FORMERR, REFUSED, NOTIMP.\n");
        break;
        case WSANO_DATA :
          printf("Valid name, no data record of requested type.\n");
        break;
          case WSAEINPROGRESS :
          printf("address blocking Windows Sockets operation is in progress.\n");
        break;
        case WSAEINTR :
          printf("The (blocking) call was canceled via WSACancelBlockingCall().\n");
        break;
        default :
          printf("Unknown error.\n");
         break;
      }
    
    }
    Just need some comment (good or bad) on the program that I've written. Just need to know if I'm on the right track.

    regards,
    jaro

  3. #3
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    Let me understand, you have some kind of a remote server, running some sort of protocol on a defined port; And you want to check if the server application is really running and not just the computer pluged in? Am I right?

    Then yeah, sure the best way is to attempt to connect the server.
    Connect the server's port just to see if it allows you to establish a connection, I think this should be enough.


    ----------
    please move the topic to Network Programming


    thank you.
    Last edited by Devil Panther; 05-12-2006 at 10:31 AM.
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Server and Client process
    By wise_ron in forum Networking/Device Communication
    Replies: 1
    Last Post: 10-07-2006, 01:11 AM
  2. Unicode vurses Non Unicode client server application with winsock2 query?
    By dp_76 in forum Networking/Device Communication
    Replies: 0
    Last Post: 05-16-2005, 07:26 AM
  3. SWEBS Web Server
    By nickname_changed in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 09-22-2003, 02:46 AM
  4. Visual Studio and FTP Server...
    By Grayson_Peddie in forum Networking/Device Communication
    Replies: 0
    Last Post: 09-03-2003, 12:31 PM
  5. IE 6 status bar
    By DavidP in forum Tech Board
    Replies: 15
    Last Post: 10-23-2002, 05:31 PM