1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
|
void
openAsync(long *plFd)
{
const char func[] = "openAsync()";
long lStatus;
long lFlags;
struct sockaddr_in6 serv_addr;
long lFd = socket(AF_INET6, SOCK_STREAM, 0); // open an IPv6 socket
if (lFd == -1)
{
throw runtime_error(getErrStr(errno, func, "Failure to open socket."));
}
lFlags = fcntl(lFd, F_GETFL);
if (lFlags == -1)
{
throw runtime_error(getErrStr(errno, func, "Failure to get socket flags"));
}
lStatus = fcntl(lFd, F_SETFL, lFlags | FNDELAY | FASYNC);
if (lStatus == -1)
{
throw runtime_error(getErrStr(errno, func, "Failure to set socket flags"));
}
lStatus = fcntl(lFd, F_SETOWN, getpid());
if (lStatus == -1)
{
throw runtime_error(getErrStr(errno, func, "Failure to set SIGIO destination"));
}
serv_addr.sin6_family = AF_INET6;
serv_addr.sin6_addr = in6addr_any;
serv_addr.sin6_port = htons(9000); // set to network byte order
lStatus = bind(lFd, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
if (lStatus == -1)
{
throw runtime_error(getErrStr(errno, func, "Failure to bind socket"));
}
*plFd = lFd;
}
|