Thread: little program confusion

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    29

    little program confusion

    I'm having a bit of trouble letting the user supply the port number as an argument. I have a feeling it's a simple fix.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    
    #define BUFFER 1024
    #define TESTPORT 9999
    
    int i;
    void display_usage(void);
    char *TARGETIP,the_buffer[BUFFER],return_data[BUFFER];
    
    int main( int argc, char *argv[] ) {
    
             if( argc < 2 )
             {
             display_usage();
             exit(1);
             }
            
             //printf("You entered %s\n", argv[2]);
             TARGETIP = argv[1]; // TARGETIP had to be a pointer
             //TESTPORT = argv[2];  
    
             //fill the buffer up with 1024 A's
             for( i = 0; i < BUFFER; i++)
             the_buffer[i] = 'A';    
          
             //setup sockets
             int testsocket;        
             struct sockaddr_in thetarget;  
             testsocket = socket(AF_INET, SOCK_STREAM, 0);  
    
             thetarget.sin_family = AF_INET; //always AF_INET        
             thetarget.sin_port = htons(TESTPORT); // *TESTPORT is argv[2]         
             thetarget.sin_addr.s_addr = inet_addr(TARGETIP); // TARGETIP is argv[1]
            
             connect(testsocket, (struct sockaddr *)&thetarget, sizeof(struct sockaddr)); // make the connection
             send(testsocket, the_buffer, strlen(the_buffer), 0);  // send the buffer
             
             while (1) {
             recv(testsocket, return_data, strlen(the_buffer), 0); // put the length of he_buffer 1024 into return_data     
             printf("%s\n", return_data); 
            }
    
             perror("connect"); 
    }
    
    void display_usage(void)
    {
           fprintf(stderr, "Usage: ./connect 127.0.0.1 9999\n" );
    }
    -Thanks in advance

  2. #2
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235
    Code:
    #define TESTPORT 9999
    The above does not define a variable -- it defines a pre-processor symbol. Any attempt to change the value, such as your commented out
    Code:
    TESTPORT = argv[2];
    will, in fact, be treated as if you had typed
    Code:
    9999 = argv[2];
    which is not a valid C assignment. You need to declare a variable to hold the value of TESTPORT.

    As an aside, you shouldn't use all-caps for variable names. It's considered bad style. #define symbols are not variables, and are traditionally done in all-caps.

    edit: added the word "pre-processor"
    Last edited by filker0; 11-09-2005 at 11:53 AM.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    A fix....
    Code:
    int iPort;
    //Assuming argv[2] is port number
    iPort = atoi(argv[2]);
     thetarget.sin_port = htons(iPort);
    htons needs an integer to convert from host byte order to network byte order. Any type of character array won't work.

    Have fun

    Bob

  4. #4
    Registered User
    Join Date
    May 2005
    Posts
    29

    whooo

    Thanks a million, got it working now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  2. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  3. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  4. Big Confusion on an easy Program
    By cookie in forum C Programming
    Replies: 11
    Last Post: 06-15-2007, 05:11 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM