is it a socket or is it not?

Jim Frost madd at bu-cs.BU.EDU
Sat Aug 12 15:15:22 AEST 1989


In article <8 at dgis.daitc.mil> generous at dgis.daitc.mil (Curtis Generous) writes:
|What's a good method of determining whether a file descriptor
|is a socket or not?  The fstat(2) call does not work well on sockets
|(and it even says so in the man page :-)

Probably the easiest way is:

int isASocket(s)
	int s;
{ struct sockaddr name;
  int             len;

  if ((getsockname(s, &name, &len) < 0) && (errno == ENOTSOCK))
    return(0);
  else
    return(1);
}

You could probably use a variety of other functions, but this seems
safe enough.

jim frost
software tool & die
madd at std.com



More information about the Comp.unix.questions mailing list