Thread: Send() not "sending" whole message

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    51

    Send() not "sending" whole message

    Code:
    printf("\nPlease enter username : ");
    					scanf( "%s", &username);
    					printf("Please enter password : ");
    					scanf( "%s", &password);
    					send(sockfd, &username, strlen(&username), 0);
    					break;
    Can anyone see why this code is sending all but the first character?

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    How is "username" declared? Your use of & in all three cases here is not appropriate if these are just normal char pointers.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You don't need the & for scanning into an array / pointer. Also, you probably want to print your input just to be sure it's what you think it is:
    Code:
    scanf( ... )
    printf( "debug: \'%s\'\n", username );

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    If your compiler is not complaining about strlen(&username), you've got your types wrong (or you forgot to #include <string.h>). If your compiler is complaining about strlen(&username), you should fix the problem; don't ignore warnings from a compiler, as they tend to be useful.

    At the very least, you should remove all those ampersands. They cannot be right (except possibly the &username in send() is acceptable, but even then you can drop it). Without seeing all of your code, though, I can't definitively say what's going wrong. It'd be really helpful to see the declarations of username and password, at least.

  5. #5
    Registered User
    Join Date
    Nov 2009
    Posts
    51
    Code:
         char *username;
        char *password;
    Code:
         printf("\nPlease enter your new username : ");
    					scanf( "%s", username);
    					printf("Please enter  your password : ");
    					scanf( "%s", password);
    					send(sockfd, username, strlen(username), 0);
    					break;
    When run like this, as soon as i press enter after inputting the user name I get a segmentation fault.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You didn't actually allocate space for either pointer, did you?


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Nov 2009
    Posts
    51
    Sorry, I am quite new to C and have not quite got to grips with the pointer stuff.

    Originally I had

    Code:
         char username;
        char password;
    Code:
         printf("\nPlease enter your new username : ");
    					scanf( "%s", &username);
    					printf("Please enter  your password : ");
    					scanf( "%s", &password);
    					send(sockfd, &username, strlen(&username), 0);
    					break;
    But that read in all the text, however long just missing of the first character.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    char justonecharacter;
    char *pointertoacharacterbutnoactualallocatedmemory;
    char arrayofXelementsofactualallocatedspace[ X ];
    If you're using a pointer, then you need to make it point some place. That means you need something like malloc.


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Nov 2009
    Posts
    51
    Thanks! I've now declared them, for example :
    Code:
         char string[50];
    is this bad practice?

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Martin_T View Post
    is this bad practice?
    No, but then you will want to restrict the amount of input in the scanf line so you cannot overflow -- scanf("%49s") or something.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Desperate Over Ecs!
    By cookie in forum C Programming
    Replies: 17
    Last Post: 07-16-2008, 01:25 PM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  3. Making a script language?
    By Blackroot in forum Game Programming
    Replies: 10
    Last Post: 02-16-2006, 02:22 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Sending CChildView a Message :: MFC
    By kuphryn in forum Windows Programming
    Replies: 0
    Last Post: 04-06-2002, 03:00 PM