Question

I have to perform testing of socket related requirements for the software. As part of testing, I have to create a case for socket creation failure. My question is how do I make the C language socket() function return error value -1. The following are the lines of code to create the sockets in the software.

int socket = socket(AF_INET, SOCK_DGRAM, 0);

System configuration:

  • OS: Windriver VxWorks
  • Language: C
Was it helpful?

Solution

Socket creation rarely fails except for invalid parameters or extremely low-resources conditions (but in those cases generally nothing works so attempting recovery is an impossible mission).

What can fail at runtime is for example a listen call because another process already is listening on the same port.

In windows socket creation most commonly fails because the the winsock initialization call has not been performed. This is IMO an horrible design bug in the API (so bad that makes me think it's an intentional attempt at prevent portability for no real reason).

OTHER TIPS

The manual page says:

ERRORS

  • EACCES Permission to create a socket of the specified type and/or protocol is denied.

  • EAFNOSUPPORT The implementation does not support the specified address family.

  • EINVAL Unknown protocol, or protocol family not available.

  • EINVAL Invalid flags in type.

  • EMFILE Process file table overflow.

  • ENFILE The system limit on the total number of open files has been reached.

  • ENOBUFS or ENOMEM Insufficient memory is available. The socket cannot be created until sufficient resources are freed.

  • EPROTONOSUPPORT The protocol type or the specified protocol is not supported within this domain.

Other errors may be generated by the underlying protocol modules.

Pass in invalid values for the socket type. Or open up so many file handles that it can't create a new one for the socket (the number this takes differs by OS and version).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top