Advertisement

07.01.2008 at 07:11AM PDT, ID: 23530202
[x]
Attachment Details

[C, Cygwin and Leopard] Differences between read()

Asked by roccogalati in C Programming Language, CYGWIN, Leopard (MAC OS 10.5)

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
[+][-]07.01.2008 at 07:31AM PDT, ID: 21908200

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.01.2008 at 11:46AM PDT, ID: 21910398

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.01.2008 at 11:48AM PDT, ID: 21910408

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.01.2008 at 11:53AM PDT, ID: 21910457

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.01.2008 at 11:56AM PDT, ID: 21910475

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: C Programming Language, CYGWIN, Leopard (MAC OS 10.5)
Sign Up Now!
Solution Provided By: Infinity08
Participating Experts: 1
Solution Grade: A
 
 
[+][-]07.01.2008 at 11:57AM PDT, ID: 21910486

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628