Thread: TCP/IP Sockets in C (problem with send() and recv(): how to loop them)

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

    TCP/IP Sockets in C (problem with send() and recv(): how to loop them)

    Hello!

    I'm creating two smalls programs with Eclipse (compiler MinGW).
    There are: a server and a client; they have to exchange strings.

    After the creations of socket, the bind() and listen(), the server start an infinite loop to 'chat' with its client.

    The client uses a getc() to ask the user for a int number (taken as string). Then it sends with a send the string to the server:

    Code:
    if (send(MySocket, strNumber, sizeof(strNumber), 0) != sizeof(strNumber)) {
    			ErrorHandler("send() failed");
    			closesocket (MySocket);
    			ClearWinSock();
    			return 0;}
    The server gets the string and converts it to a long integer:

    Code:
    if ((bytesRcvd = recv(clientSocket, buf02, BUFFERSIZE - 1, 0)) <= 0) {
    		ErrorHandler("recv() failed");
    		closesocket(clientSocket);
    		ClearWinSock();
    		return 0;}
    
    temp = strtol(buf02, &end, 10);
    Now I have to check with some if, if this number is the highest till now. If so the server should send "Ok, maximum" and the client should stop. If the number is negative, the maximum should be resetted to 0 and the client has to ask the user for another number.

    The check the server should do:

    Code:
    if (temp < 0)
    	{
    	massimo = 0;
    	strcpy(buf03, "Reset.");
    	}
    else if (temp > massimo)
    	{
    	massimo = temp;
    	strcpy(buf03, "New max = ");
    	strcat(buf03, buf02);
    	}
    else if ((temp <= massimo) && (temp > -1))
    	{
    	strcpy(buf03, "Nothing to do.");
    	}
    
    if ( send(clientSocket, buf03, sizeof(buf03), 0) != sizeof(buf03)) {
    		ErrorHandler("send() failed");
    		closesocket (clientSocket);
    		ClearWinSock();
    		return 0;}
    The client receive the string 'buf03':

    Code:
    if ((bytesRcvd = recv(MySocket, bufmax, BUFFERSIZE - 1, 0)) <= 0) {
    			ErrorHandler("recv() failed");
    			closesocket(MySocket);
    			ClearWinSock();
    			return 0;}
    
    	printf("%s\n", bufmax);
    If I try with a positive number is works. But if I try with a negative number it doesn't work because I don't know got to set a loop to cycle the asking of the number, to send() to the server and to wait for a response. Should I set a loop in the server too in order to loop cycle the recv() and the send() the string?

    I've tried with this:
    Into the client:

    Code:
    do {
    - ask for a number
    - send with a send()
    - receive the answer with a recv()
    - print it
    } while (strcmp(the_received_string, "Reset.") == 0);
    And into the server
    Code:
    do {
    - receive the string with a recv() and convert it to long int
    - check if it is the max
    - send the response to the client
    } while (max == 0);
    It doesn't work. Is correct to loop, into the server, the recev() and send() functions?

    Any help?

    Sorry for my bad English! Many thanks

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    It doesn't work.
    please be more explicit: what are you expecting, what did you get?
    Your pseudo code seems correct (last 2 blocks, except the condition max==0 which seems weird, but I may miss the point of the program), and there is no reason send() and recv() could not be used in a loop.

  3. #3
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    From skimming over what it sounds like you want to do, it would seem to be a good idea to in your server continually receive into a buffer until you receive a null terminator which would indicate a full string sent. Then in your client do whatever processing you need to do. IE see if its the maxiumum you have set. If it is, send a string back and receive into a buffer on the server until a null terminator.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    "But if I try with a negative number it doesn't work because I don't know got to set a loop to cycle the asking of the number, to send() to the server and to wait for a response. Should I set a loop in the server too in order to loop cycle the recv() and the send() the string?"

    What would be the purpose of creating a loop if it doesn't work the first time? There won't be anything different about it the second or third time.

    But, as for a send/recieve loop in the "client server model", I think you will be barking up the wrong tree. The norm, available in numerous on-line examples, is to fork() or thread recv seperate from send, and logically there is no other way for a server to work. If you can use socket(), you can use fork(). However, since what you want to do is a fairly simple linear process, you can probably get away without an honest server. In fact, I don't think your problem has anything to do with networking. You could clarify the issue by writing a simple program which accepts and parses line input from the keyboard -- since the input is unidirectional anyway:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main() {
    	int num, maximo=0;
    	while (1) {
    		scanf("%d",&num);
    		if (num==66) exit (0);		// finish
    		if (num > maximo) {
    			maximo=num;
    			puts("New Max");	// the server  replies
    		}
    	}
    }
    I understand your program involves something slightly more complicated, but not much.
    If you are confident with the way you parse input, you're ready to try to learn networking. If not, you are adding a level of a complication that will only make EVERYTHING more difficult
    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. send and recv are cpu intensive?
    By Rune Hunter in forum Networking/Device Communication
    Replies: 14
    Last Post: 09-04-2007, 09:24 AM
  2. send() and recv() functions
    By kris.c in forum Networking/Device Communication
    Replies: 9
    Last Post: 06-24-2006, 09:41 PM
  3. Socket or send() problem?
    By Achy in forum C Programming
    Replies: 5
    Last Post: 06-09-2006, 01:09 PM
  4. Using recv() and send()
    By Sam_bham in forum C Programming
    Replies: 3
    Last Post: 06-08-2004, 04:36 PM
  5. recv() problem
    By Massive in forum C Programming
    Replies: 5
    Last Post: 06-26-2003, 04:58 AM