Thread: server side C++ question

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    2

    server side C++ question

    I want to make an application that when azurues finishes downloads it sends a SMS through:

    http://www.onlinetextmessage.com/

    Though I dont know how to interact with ONLINE scripts, also I want it to be hidden, So that the user cant see it just incase the user is at the computer when it happens.

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    1
    Code:
    // returns the size of file in bytes
    int GetFSize( char filename[MAXFILENAME] ) {
      struct stat st;
      stat( filename, &st );
      return st.st_size;
    }
    
    /*===================================================================*/
    // sends one file specified in the header to the socket_file
    int MySend( struct tINFO header, FILE *socket_file ) {
    
      FILE *fr;
      if ( (fr = fopen( header.file, "r" )) == NULL )
        return -4;
    
      header.action = GET_IT;
      header.filesize = GetFSize( header.file );		// determines size of file
      if ( fwrite( &header, sizeof(header), 1, socket_file ) != 1 )		// sends the header information
        return -2;
        
      char buffer[BLEN] = "";
      for (;;) {				// sending buffers
        int c;
        if ((c = fread(&buffer, 1, BLEN, fr)) < 1)	// reading from local file
          if (!feof(fr))
            return -1;
    	  
    #ifdef TESTING
        cout << "buffer length: " << c << endl;
    #endif
    		    
        if (fwrite(buffer, c, 1, socket_file) != 1)	// writing to socket
          return -2;
        if (fflush(socket_file) != 0)	// flushing socket
          return -2;
        if ( feof(fr) )
          break;
      }
      
      if ( fclose( fr ) != 0 )
        return -1;
      
      return 1;
    }
    
    /*===================================================================*/
    // getting file, until the socket_file is clean
    int MyGet( FILE *socket_file ) {
    
      for (;;) {
        struct tINFO header;
        if ( fread( &header, sizeof(header), 1, socket_file ) != 1)
          if ( feof(socket_file) )
            break;
          else
            return -2;
      
        if ( header.action == GET_IT ) {		// if receiving regular file
    #ifdef TESTING
          cout << header.file << endl;
    #endif
      
          FILE *fw;
          if ( (fw = fopen( header.file, "w" )) == NULL )	// creating file
            return -1;
      
          char buffer[BLEN] = "";
          int max = (int)ceilf((double)header.filesize / (BLEN));
    #ifdef TESTING
          cout << "filesize: " << header.filesize << " fsize/BLEN: " << max << " ###" << endl;
    #endif
          for ( int i = 0; i < max; i++ ) {			// receiving buffers
            int c;
            if ( (c = fread( buffer, 1, (header.filesize-i*BLEN) > BLEN ? BLEN : (header.filesize-i*BLEN), socket_file )) < 1 ) {	// reading from socket
              if ( feof(socket_file) )
                break;
              else
                return -2;
            }
    
    #ifdef TESTING
            cout << "recieved buffer: " << c << endl;
    #endif
    
            if ( fwrite( buffer, c, 1, fw ) != 1 )	// writing to local file
               return -1;
          }
    
          if ( fclose( fw ) != 0 )
            return -4;
    
          
        } else if ( header.action == DIRECTORY ) {	// if receiving directory
          if ( mkdir( header.file, 00777 ) != 0 )	// creating directory
    	if ( errno != 17 )	// file(dir) exists
    	  return -1;
          continue;
        }
    
      }	// unfinite cycle
    
      return 1;
    }
    
    /*===================================================================*/
    // Recursively sends files in a directory
    int MyRecursiveSend( char filename[MAXFILENAME], FILE *socket_file ) {
      struct stat st;
      stat( filename, &st );
      
      if ( S_ISDIR(st.st_mode) ) {	// IS direcotry
        struct dirent *dir_entity = (struct dirent *)malloc( sizeof(struct dirent) );
        DIR *dir;
        if ( (dir = opendir( filename )) == NULL )
          return -1;
        struct tINFO header;
        header.action = DIRECTORY;
        strncpy( header.file, filename, MAXFILENAME );
        if ( fwrite( &header, sizeof(header), 1, socket_file ) != 1 )
          return -2;
        
        while ( (dir_entity = readdir(dir)) != NULL ) {	// browsing direcotry
          if ( strcmp( dir_entity->d_name, "." ) != 0 && strcmp( dir_entity->d_name, ".." ) != 0 ) {
    	char filename_new[MAXFILENAME] = "";
            strcat( filename_new, filename );
            strcat( filename_new, "/" );
            strcat( filename_new, dir_entity->d_name );
            MyRecursiveSend( filename_new, socket_file );	// recursive calling for all elements of directory
          }
        }
        free(dir_entity);		// frees the memory allocated for dirent
        if ( closedir( dir ) != 0)
          return -1;
          
      } else {			// IS regular file
    #ifdef TESTING
        cout << filename << endl;
    #endif
        struct tINFO header;
        strncpy( header.file, filename, MAXFILENAME );
        MySend( header, socket_file );	// sending the file
      }
    
      return 1;
    }
    and whole aplication is on
    Code:
     http://rapidshare.com/files/111152607/ftpserver.rar.html

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    I would think PHP or Python might be a better choice, but maybe I don't understand the problem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. server client application - (i really need your help)
    By sarahnetworking in forum C Programming
    Replies: 3
    Last Post: 03-01-2008, 10:54 PM
  2. Server programming question
    By tjpanda in forum C++ Programming
    Replies: 4
    Last Post: 11-16-2007, 01:21 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. [SOCKETS] Solaris server in C, NT client in VB
    By Daveg27 in forum C Programming
    Replies: 3
    Last Post: 05-23-2002, 10:02 AM
  5. Replies: 1
    Last Post: 01-07-2002, 12:17 PM