Following are few wrappers used for networking code.. Mainly for sockets
int nRead(int fd, char* ptr, int nBytes)
{
int nleft = nbytes ;
int nread ;
while (nleft>0)
{
nread = read(fd, ptr, nleft) ;
if (nread == 0)
break ;
if (nread<0)
return nread ;
nleft = nleft - nread ;
ptr = ptr+nread ;
}
return nbytes - nleft ;
}
int nWrite(int fd, const char* ptr, int nBytes)
{
int nleft = nBytes ;
int nwrite ;
while (nleft >= 0)
{
nwrite = write(fd, ptr, nleft) ;
if (nwrite <=0)
nleft = nleft - nwrite ;
ptr = ptr + nwrite ;
}
return nBytes - nleft ;
}
int setNonBlocking(int fd)
{
int flags ;
if (flags = fcntl(fd, F_GETFL) < 0)
{
perror("GETFL") ;
return -1 ;
}
if (fcntl(fd, F_SETFL | flags) < 0)
{
perror("SETFL") ;
return -1 ;
}
return 0 ;
}
int nRead(int fd, char* ptr, int nBytes)
{
int nleft = nbytes ;
int nread ;
while (nleft>0)
{
nread = read(fd, ptr, nleft) ;
if (nread == 0)
break ;
if (nread<0)
return nread ;
nleft = nleft - nread ;
ptr = ptr+nread ;
}
return nbytes - nleft ;
}
int nWrite(int fd, const char* ptr, int nBytes)
{
int nleft = nBytes ;
int nwrite ;
while (nleft >= 0)
{
nwrite = write(fd, ptr, nleft) ;
if (nwrite <=0)
nleft = nleft - nwrite ;
ptr = ptr + nwrite ;
}
return nBytes - nleft ;
}
int setNonBlocking(int fd)
{
int flags ;
if (flags = fcntl(fd, F_GETFL) < 0)
{
perror("GETFL") ;
return -1 ;
}
if (fcntl(fd, F_SETFL | flags) < 0)
{
perror("SETFL") ;
return -1 ;
}
return 0 ;
}
No comments:
Post a Comment