Hi!
I'm just curious about a strange behavior which happens when i try to use a C program written on my Mac on some other pc...
I have an iMac 20" with an intel dual core and i'm writing a TCP server in C.
I'm using a function in order to read the commands sended by a client, and i use this:
while( (n = Readn(fd, buffer, MAX_LINE-1)) > 0)
{
// doing something...
}
where Readn() is this:
ssize_t readn (int fd, void *vptr, size_t n)
{
size_t nleft;
ssize_t nread;
char *ptr;
ptr = vptr;
nleft = n;
while (nleft > 0)
{
if ( (nread = read(fd, ptr, nleft)) < 0)
{
if (INTERRUPTED_BY_SIGNAL)
{
nread = 0;
continue; /* and call read() again */
}
else
return -1;
}
else
if (nread == 0)
break; /* EOF */
nleft -= nread;
ptr += nread;
}
return n - nleft;
}
ssize_t Readn (int fd, void *ptr, size_t nbytes)
{
ssize_t n;
if ( (n = readn(fd, ptr, nbytes)) < 0)
err_sys ("(%s) error - readn() failed", prog);
return n;
}
what happen is this:
If i'm on my Mac, the line:
while( (n = Readn(fd, buffer, MAX_LINE-1)) > 0)
works everytime fine... it reads whatever the client sends even if it is less than MAX_LINE-1 chars.
Even if i compile and use it on cygwin which is on my windows installed on my virtual machine (always on my mac)
but if i try to use it on a cygwin installed in a different pc, this line:
while( (n = Readn(fd, buffer, MAX_LINE-1)) > 0)
doesn't work!
it never enters in the while cicle because it works only if it receive a string with a lenght MAX_LINE-1, if it receives another string less than MAX_LINE-1 it doesn't work...
so if i'm on another computer i have to pass to the Readn the correct lenght of what the client sends.
It's just a curiosity, but why this happens?
Why it works fine on my Mac (also on my cygwin installed on my virtual machine) and not on a different pc?
thanks!
Thanks!
Start Free Trial