Thread: Error in sending/receiving strings while using sendto() and recvfrom()

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    26

    Question Error in sending/receiving strings while using sendto() and recvfrom()

    Hello!

    I have the following problem:
    My Client waits for a string and sends it to a Server:
    Code:
    memset(&echoString2, 0, sizeof(echoString2));
    
        fprintf(stdout,"\nInput a string: ");
    
        do {
    		fscanf(stdout, "%s", echoString2);
    		} while (strlen(echoString) > MAX);
    
        echoStringLen = strlen(echoString2);
    
        if (sendto(sock, echoString2, echoStringLen, 0, (struct sockaddr *)
                       &echoServAddr, sizeof(echoServAddr)) != echoStringLen)
                printError("\nError in sendto().");
    The Server receives this string and counts vowels:
    Code:
    memset(&echoBuffer, 0, sizeof(echoBuffer));
    
            if ((recvMsgSize = recvfrom(sock, echoBuffer, MAX, 0,
                (struct sockaddr *) &echoClntAddr, &cliAddrLen)) < 0)
                printError("\nrecvfrom() failed.");
    
            fprintf(stdout, "Received string: %s", echoBuffer);
    
            for (i = 0; i < strlen(echoBuffer); i++)
            {
            	switch(echoBuffer[i]) {
    				 case 'a': k++; break;
    				 case 'A': k++; break;
    				 case 'e': k++; break;
    				 case 'E': k++; break;
    				 case 'i': k++; break;
    				 case 'I': k++; break;
    				 case 'o': k++; break;
    				 case 'O': k++; break;
    				 case 'u': k++; break;
    				 case 'U': k++; break;
            	}
            }
    But I've noted, when I send a a string with blanks, the receivfrom() stops to the first word and counts the vowels of this one. I don't know why. Any idea?

    PS: I use Eclipse on Win.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    fscanf with %s cannot read a string with spaces in it. If you want to read a string with spaces in it, fscanf is not for you; maybe use fgets instead.

    (And do you really intend to read from stdout?)

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Not that it makes any difference to the question you are asking, but did you know that:
    Code:
    				 case 'a': k++; break;
    				 case 'A': k++; break;
    				 case 'e': k++; break;
    				 case 'E': k++; break;
    				 case 'i': k++; break;
    				 case 'I': k++; break;
    				 case 'o': k++; break;
    				 case 'O': k++; break;
    				 case 'u': k++; break;
    				 case 'U': k++; break;
    can be written as:
    Code:
    				 case 'a': 
    				 case 'A': 
    				 case 'e': 
    				 case 'E': 
    				 case 'i': 
    				 case 'I': 
    				 case 'o': 
    				 case 'O': 
    				 case 'u': 
    				 case 'U': k++; break;
    (Sorry, but I'm quite allergic to repeated code).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Feb 2008
    Posts
    26

    !

    @tabstop: fscan with stdout is a typing error (thanks for telling me). I've used fgets() and works fine (thanks).
    @matsp: thank you for your tip about writing code: I've corrected the switch.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hook sendto and recvfrom
    By brietje698 in forum Networking/Device Communication
    Replies: 4
    Last Post: 12-10-2007, 03:58 AM
  2. Socket Datagram info sendto(), recvfrom()
    By siluro in forum C Programming
    Replies: 1
    Last Post: 02-20-2005, 12:10 PM

Tags for this Thread