in reply to Could not create socket: Bad file number

To narrow down on the issue, I would run the program under strace to figure out which system call is actually failing...

On my machine (where your (slightly adapted) code does work), the last lines I get are

... socket(PF_INET, SOCK_STREAM, IPPROTO_TCP) = 3 ioctl(3, SNDCTL_TMR_TIMEBASE or TCGETS, 0xbfa99308) = -1 EINVAL (Inval +id argument) _llseek(3, 0, 0xbfa99350, SEEK_CUR) = -1 ESPIPE (Illegal seek) ioctl(3, SNDCTL_TMR_TIMEBASE or TCGETS, 0xbfa99308) = -1 EINVAL (Inval +id argument) _llseek(3, 0, 0xbfa99350, SEEK_CUR) = -1 ESPIPE (Illegal seek) fcntl64(3, F_SETFD, FD_CLOEXEC) = 0 setsockopt(3, SOL_SOCKET, SO_REUSEADDR, [1], 4) = 0 bind(3, {sa_family=AF_INET, sin_port=htons(7070), sin_addr=inet_addr(" +0.0.0.0")}, 16) = 0 listen(3, 1) = 0 accept(3,

It could be one of fcntl, setsockopt, bind or listen failing (all may potentially set EBADF). Then, depending on which one it is, look in the respective man page. If you're lucky, you might find something resembling an explanation...

Replies are listed 'Best First'.
Re^2: Could not create socket: Bad file number
by perlofwisdom (Pilgrim) on Aug 30, 2007 at 23:27 UTC
    Thanks for the "strace" suggestion. I've never used it before, and there's no man-page for it, so I probably got the syntax wrong. Here's what I tried:
    -> strace perl receive.pl ERROR: unable to open /dev/log
    I also tried a 'set -x', but that returned the same results I was getting before, no detail.

      Which platform are you on? The strace I was referring to is a Linux tool — on some other unixes, the program named strace is an entirely different tool...

      [ Update: actually, that's exactly the error message you get when you try to run strace (STREAMS trace) on Solaris as a regular user (because the device that /dev/log points to is owned by root:sys). On HP-UX and AIX I'm getting different error messages — but YMMV.  So I supppose what you want is truss (usage for simple things is essentially the same as strace under Linux). ]

      A couple of weeks ago, I posted a list of corresponding tools for other platforms... (if the respective tool you want is not installed already, you should be able to download it from the net).

      (BTW, you called it correctly.)