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