![]() |
| |||||||
![]() |
| | LinkBack | Thread Tools | Display Modes |
| | #1 |
| Registered User Join Date: Feb 2008
Posts: 21
| TCP/IP Sockets in C (problem with send() and recv(): how to loop them) 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;}
Code: if ((bytesRcvd = recv(clientSocket, buf02, BUFFERSIZE - 1, 0)) <= 0) {
ErrorHandler("recv() failed");
closesocket(clientSocket);
ClearWinSock();
return 0;}
temp = strtol(buf02, &end, 10);
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;}
Code: if ((bytesRcvd = recv(MySocket, bufmax, BUFFERSIZE - 1, 0)) <= 0) {
ErrorHandler("recv() failed");
closesocket(MySocket);
ClearWinSock();
return 0;}
printf("%s\n", bufmax);
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);
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);
Any help? Sorry for my bad English! Many thanks |
| ferenczi is offline | |
| | #2 | |
| Registered User Join Date: Apr 2008
Posts: 282
| Quote:
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 | |
| | #3 |
| Registered User 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 | |
| | #4 |
| critical genius 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
}
}
}
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 |
| MK27 is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |