C Board  

Go Back   C Board > General Programming Boards > Networking/Device Communication

Reply
 
LinkBack Thread Tools Display Modes
Old 11-27-2008, 04:36 PM   #1
Registered User
 
Join Date: Feb 2008
Posts: 17
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.
ferenczi is offline   Reply With Quote
Old 11-27-2008, 04:58 PM   #2
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
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?)
tabstop is offline   Reply With Quote
Old 11-27-2008, 05:15 PM   #3
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
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.
matsp is offline   Reply With Quote
Old 11-28-2008, 12:12 AM   #4
Registered User
 
Join Date: Feb 2008
Posts: 17
!

@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.
ferenczi is offline   Reply With Quote
Reply

Tags
recvfrom(), sendto(), strings

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Hook sendto and recvfrom brietje698 Networking/Device Communication 4 12-10-2007 03:58 AM
Socket Datagram info sendto(), recvfrom() siluro C Programming 1 02-20-2005 12:10 PM


All times are GMT -6. The time now is 11:17 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22