C Board  

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

Reply
 
LinkBack Thread Tools Display Modes
Old 11-03-2008, 05:56 AM   #1
Registered User
 
Join Date: Feb 2008
Posts: 21
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
ferenczi is offline   Reply With Quote
Old 11-17-2008, 02:50 PM   #2
Registered User
 
Join Date: Apr 2008
Posts: 282
Quote:
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.
root4 is offline   Reply With Quote
Old 11-17-2008, 03:00 PM   #3
Registered User
 
valaris's Avatar
 
Join Date: Jun 2008
Location: RING 0
Posts: 468
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.
valaris is offline   Reply With Quote
Old 11-18-2008, 07:38 AM   #4
critical genius
 
MK27's Avatar
 
Join Date: Jul 2008
Location: SE Queens
Posts: 5,158
"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
__________________

"A man can't just sit around." -- Larry Walters
MK27 is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
send and recv are cpu intensive? Rune Hunter Networking/Device Communication 14 09-04-2007 09:24 AM
send() and recv() functions kris.c Networking/Device Communication 9 06-24-2006 09:41 PM
Socket or send() problem? Achy C Programming 5 06-09-2006 01:09 PM
Using recv() and send() Sam_bham C Programming 3 06-08-2004 04:36 PM
recv() problem Massive C Programming 5 06-26-2003 04:58 AM


All times are GMT -6. The time now is 04:31 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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