While not ended

N

nick048

Hi,

I a my server program I have in the main this code:

int main(int argc, char *argv[])
{
char ctrl_terminator[3];
char terminator[3]={'.','.','\n'};
char msg[BUFLEN];
/* Other variables and the header of main */

while (ctrl_terminator != terminator)
{
i=0;
char_recv = recv(newsocketfd, &c, 1, 0);
while (c !='\n')
{
msg[i++]=c;
char_recv = recv(newsocketfd, &c, 1, 0);
} /* End while (c !='\n') */
msg='\0';
printf("This message is arrived: %s\n",msg);
if (i > 2)
{
strncpy(ctrl_terminator, &msg[i-3], 2);
ctrl_terminator[3] = '\0';
} /* End if */
} /* while (ctrl_terminator != terminator) */

/* The rest of Main */

The program work fine; when the telnet establish a connection with
server, I can send to server many msg. But if I close my last message
with the sequence "..", the "while (ctrl_terminator !=
terminator)" is not verified and I don't can exit from loop.

Sure, I mistake something; but I don't know how to resolve the
problem .

Can You help me ?

Best Regards
Nick
 
G

Gunvant Patil

Hi,

I a my server program I have in the main this code:

int main(int argc, char *argv[])
{
char ctrl_terminator[3];
char terminator[3]={'.','.','\n'};
char msg[BUFLEN];
/* Other variables and the header of main */

while (ctrl_terminator != terminator)

Thats infinite loop cause you are trying to compare base addresses of
two different char arrays. You can't compare two char arrays like this
use memcmp() instead.
{
i=0;
char_recv = recv(newsocketfd, &c, 1, 0);
while (c !='\n')
{
msg[i++]=c;
char_recv = recv(newsocketfd, &c, 1, 0);
} /* End while (c !='\n') */
msg='\0';
printf("This message is arrived: %s\n",msg);
if (i > 2)
{
strncpy(ctrl_terminator, &msg[i-3], 2);
ctrl_terminator[3] = '\0';
} /* End if */
} /* while (ctrl_terminator != terminator) */

/* The rest of Main */

The program work fine; when the telnet establish a connection with
server, I can send to server many msg. But if I close my last message
with the sequence "..", the "while (ctrl_terminator !=
terminator)" is not verified and I don't can exit from loop.

Sure, I mistake something; but I don't know how to resolve the
problem .

Can You help me ?

Best Regards
Nick


-Cheers,
Gunvant
~~~~~~~~
No trees were killed in the sending of this message. However a large
number of electrons were terribly inconvenienced.
 
R

Richard Heathfield

nick048 said:
Hi,

I a my server program I have in the main this code:

int main(int argc, char *argv[])
{
char ctrl_terminator[3];
char terminator[3]={'.','.','\n'};
char msg[BUFLEN];
/* Other variables and the header of main */

while (ctrl_terminator != terminator)

ctrl_terminator is an array, and terminator is an array. When you use
the name of an array (except during declaration or as an operand of &
or sizeof) it is treated as if it were a pointer to the array's first
element. Since ctrl_terminator's and terminator's first elements are
never going to be in the same place, the comparison will always fail.

Instead, add <string.h> to your list of includes (which you appear to
have failed to reproduce here), and use:

while(memcmp(ctrl_terminator, terminator, sizeof terminator) != 0)
{
i=0;
char_recv = recv(newsocketfd, &c, 1, 0);
while (c !='\n')
{
msg[i++]=c;

If i becomes or exceeds BUFLEN, you're in trouble.
char_recv = recv(newsocketfd, &c, 1, 0);
} /* End while (c !='\n') */
msg='\0';
printf("This message is arrived: %s\n",msg);
if (i > 2)
{
strncpy(ctrl_terminator, &msg[i-3], 2);
ctrl_terminator[3] = '\0';


You mean ctrl_terminator[2] = '\0';

There are three elements in ctrl_terminator. They are:

ctrl_terminator[0] (the first one);
ctrl_terminator[1] (the second one);
ctrl_terminator[2] (the third and last one).
The program work fine; when the telnet establish a connection with
server, I can send to server many msg.

I have a bad feeling about this.
 
J

Jonas

nick048 said:
Hi,

I a my server program I have in the main this code:

Is uppose you have omitted includes & defines?
int main(int argc, char *argv[])
{
char ctrl_terminator[3];

You probably want to initialize ctrl_terminator.
char terminator[3]={'.','.','\n'};
char msg[BUFLEN];
/* Other variables and the header of main */

while (ctrl_terminator != terminator)

Here you are comparing pointers, not the contents of the memory they point
to. Try memcmp in string.h.
{
i=0;
char_recv = recv(newsocketfd, &c, 1, 0);
while (c !='\n')
{
msg[i++]=c;
char_recv = recv(newsocketfd, &c, 1, 0);
} /* End while (c !='\n') */
msg='\0';
printf("This message is arrived: %s\n",msg);
if (i > 2)
{
strncpy(ctrl_terminator, &msg[i-3], 2);


msg is always NUL-terminated, so there is no need to use strncpy and then
add the NUL.
ctrl_terminator[3] = '\0';

Writing out of array bounds, see definition of ctrl_terminator above. Also,
the array you intend to use for comparison, terminator, contains
{'.','.','\n'}. Assuming that msg here ends in {'.','.','\0'}, and that you
really meant ctrl_terminator[2] = '\0', ctrl_terminator will contain
{'.','.','\0'}. The initialization of terminator should be changed
accordingly.

Introducing a variable done (initialized to 0) would probably be beneficial
here:

if (i > 2 && memcmp(&msg[i - 3], terminator, 2) == 0) {
done = 1;
}

Then your while-stmt would simply read

while (!done) {
/*... */
}
 
N

nick048

I a my server program I have in the main this code:

Is uppose you have omitted includes & defines?
int main(int argc, char *argv[])
{
char ctrl_terminator[3];

You probably want to initialize ctrl_terminator.
char terminator[3]={'.','.','\n'};
char msg[BUFLEN];
/* Other variables and the header of main */
while (ctrl_terminator != terminator)

Here you are comparing pointers, not the contents of the memory they point
to. Try memcmp in string.h.
{
i=0;
char_recv = recv(newsocketfd, &c, 1, 0);
while (c !='\n')
{
msg[i++]=c;
char_recv = recv(newsocketfd, &c, 1, 0);
} /* End while (c !='\n') */
msg='\0';
printf("This message is arrived: %s\n",msg);
if (i > 2)
{
strncpy(ctrl_terminator, &msg[i-3], 2);


msg is always NUL-terminated, so there is no need to use strncpy and then
add the NUL.
ctrl_terminator[3] = '\0';

Writing out of array bounds, see definition of ctrl_terminator above. Also,
the array you intend to use for comparison, terminator, contains
{'.','.','\n'}. Assuming that msg here ends in {'.','.','\0'}, and that you
really meant ctrl_terminator[2] = '\0', ctrl_terminator will contain
{'.','.','\0'}. The initialization of terminator should be changed
accordingly.

Introducing a variable done (initialized to 0) would probably be beneficial
here:

if (i > 2 && memcmp(&msg[i - 3], terminator, 2) == 0) {
done = 1;

}

Then your while-stmt would simply read

while (!done) {
/*... */

}


Thank You.
It's work.
Nick
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,781
Messages
2,569,615
Members
45,293
Latest member
Hue Tran

Latest Threads

Top