tcperror() — Print the error messages of a socket function

Standards

Standards / Extensions C or C++ Dependencies
z/OS® UNIX both  

Format

#define _OPEN_SYS_SOCK_EXT
#include <sys/socket.h>
#include <stdio.h>
#include <errno.h>

void tcperror(const char *s);

General description

When a socket call produces an error, the call returns a negative value and the variable errno is set to an error value found in ERRNO.H. The tcperror() call prints a short error message describing the last error that occurred. If s is non-NULL, tcperror() prints the string s followed by a colon, followed by a space, followed by the error message, and terminated with a newline character. If s is NULL or points to a NULL string, only the error message and the newline character are output.

The tcperror() function is equivalent to the perror() function in UNIX.
Parameter
Description
s
A NULL or NULL-terminated character string.

Returned value

tcperror() returns no values.

Example

The following are examples of the tcperror() call.

Example 1:
   if   ((s=socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
        tcperror("socket()");
        exit(2);
   }
If the socket() call produces the error ENOMEM, socket() returns a negative value and sets errno to ENOMEM. When tcperror() is called, it prints the string:
      socket():  not enough storage (ENOMEM)
Example 2:
   if ((s=socket(AF_INET, SOCK_DGRAM, 0)) < 0)
      tcperror(NULL);
If the socket() call produces the error ENOMEM, socket() returns a negative value and sets errno to ENOMEM. When tcperror() is called, it prints the string:
       Not enough storage (ENOMEM)

Related information