Perror and strerror show the string error message corresponding to errno

Source: Internet
Author: User
Tags connection reset

2012-02-14 wcdj Happy Valentine's Day!

Error Handling

-- View errno, which is useful when locating error messages

When a Unix function fails, a negative value is often returned, and the integer errno is usually set to a value containing additional information. (Note that not all cases are true)

The file <errno. h> defines the symbol errno and various constants that can be assigned to it. These constants start with the character E.

Note:

(1) Use MAN 3 errno to view the error constant details.

(2) The <errno. h> header file defines the integer variable errno, which is set by system CILS and some library functions in the event of an error to indicate what went wrong.

(3) If no call error occurs, the errno value will not be cleared. Therefore, the errno value should be checked only when the return value of the function indicates an error.

(4) Valid error numbers are all non-zero; errno is never set to zero by any library function. no function will set the errno value to 0. h> all error constants defined in are not 0.

(5) errno is defined by the iso c standard to be a modifiable lvalue of Type int, and must not be explicitly declared; errno is defined by the system and cannot be redefined.

(6) errno is thread-local; setting it in one thread does not affect its value in any other thread. in a thread-supporting environment, multiple threads share the process address space. Each thread has its own local errno to avoid one thread interfering with another thread.

(7) It was common in traditional C to declare errno manually (I. E ., extern int errno) instead of including <errno. h>. do not do this. it will not work with modern versions of hte C library. however, on (very) Old Unix systems, there may be no <errno. h> and
The Declaration is needed. In modern compilers, the Declaration header file should be displayed; otherwise, the system may prompt that the errno identifier cannot be found.

A Simple Method for viewing error code details:

Method 1:

Find/usr/include-name "errno. H" | xargs grep 115 view error code information

Method 2:

Man perror
Perror-explain error codes
For example:
$ Perror 2
OS error code 2: no such file or directory

When writing a program, C StandardTwo functions are defined to print error information:

Char * strerror (INT errnum );
More

Void perror (const char * s );More


Description

(1) # include <string. h>, returns a pointer to the message string. The strerror function maps errnum (usually errno value) to an error message string and returns a pointer to this string.(It can be used for your own log functions)

(2) # include <stdio. h>: The perror function generates an error message for a standard error based on the current value of errno and returns it. It first outputs the string directed by S (user-defined information), followed by a colon, a space, followed by an error message corresponding to the errno value, and finally a line break.(For standard output only)

The error message corresponding to errno is displayed below:

#include <netinet/in.h>#include <sys/types.h>#include <sys/socket.h>#include <errno.h>#include <stdio.h>#include <stdlib.h>#include <time.h>void fred(int error_number){fprintf(stderr, "%3d ", error_number);errno = error_number;perror("test");}/*fred()*/int main(void){int socket1;int socket2;int the_error;struct timeval tv;for(the_error = 0;the_error <= 127;the_error++){fred(the_error);}fprintf(stderr, "==== about to find EDOM\n");fred(EDOM);fprintf(stderr, "==== about to do socket stuff\n");socket1 = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);if(socket1 < 0){perror("first socket()");exit(1);}socket2 = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);if(socket2 < 0){perror("second socket()");exit(1);}tv.tv_sec = 0;//tv.tv_usec = 1000000;tv.tv_usec = 999999;if(setsockopt(socket1,SOL_SOCKET,SO_RCVTIMEO,&tv,sizeof(tv))< 0){perror("first setsockopt()");}else{fprintf(stderr, "first setsockopt() worked\n");}tv.tv_sec = 1;tv.tv_usec = 0;if(setsockopt(socket2,SOL_SOCKET,SO_RCVTIMEO,&tv,sizeof(tv))< 0){perror("second setsockopt()");}else{fprintf(stderr, "second setsockopt() worked\n");}return 0;}/*main()*/

Output:

  0 test: Success  1 test: Operation not permitted  2 test: No such file or directory  3 test: No such process  4 test: Interrupted system call  5 test: Input/output error  6 test: No such device or address  7 test: Argument list too long  8 test: Exec format error  9 test: Bad file descriptor 10 test: No child processes 11 test: Resource temporarily unavailable 12 test: Cannot allocate memory 13 test: Permission denied 14 test: Bad address 15 test: Block device required 16 test: Device or resource busy 17 test: File exists 18 test: Invalid cross-device link 19 test: No such device 20 test: Not a directory 21 test: Is a directory 22 test: Invalid argument 23 test: Too many open files in system 24 test: Too many open files 25 test: Inappropriate ioctl for device 26 test: Text file busy 27 test: File too large 28 test: No space left on device 29 test: Illegal seek 30 test: Read-only file system 31 test: Too many links 32 test: Broken pipe 33 test: Numerical argument out of domain 34 test: Numerical result out of range 35 test: Resource deadlock avoided 36 test: File name too long 37 test: No locks available 38 test: Function not implemented 39 test: Directory not empty 40 test: Too many levels of symbolic links 41 test: Unknown error 41 42 test: No message of desired type 43 test: Identifier removed 44 test: Channel number out of range 45 test: Level 2 not synchronized 46 test: Level 3 halted 47 test: Level 3 reset 48 test: Link number out of range 49 test: Protocol driver not attached 50 test: No CSI structure available 51 test: Level 2 halted 52 test: Invalid exchange 53 test: Invalid request descriptor 54 test: Exchange full 55 test: No anode 56 test: Invalid request code 57 test: Invalid slot 58 test: Unknown error 58 59 test: Bad font file format 60 test: Device not a stream 61 test: No data available 62 test: Timer expired 63 test: Out of streams resources 64 test: Machine is not on the network 65 test: Package not installed 66 test: Object is remote 67 test: Link has been severed 68 test: Advertise error 69 test: Srmount error 70 test: Communication error on send 71 test: Protocol error 72 test: Multihop attempted 73 test: RFS specific error 74 test: Bad message 75 test: Value too large for defined data type 76 test: Name not unique on network 77 test: File descriptor in bad state 78 test: Remote address changed 79 test: Can not access a needed shared library 80 test: Accessing a corrupted shared library 81 test: .lib section in a.out corrupted 82 test: Attempting to link in too many shared libraries 83 test: Cannot exec a shared library directly 84 test: Invalid or incomplete multibyte or wide character 85 test: Interrupted system call should be restarted 86 test: Streams pipe error 87 test: Too many users 88 test: Socket operation on non-socket 89 test: Destination address required 90 test: Message too long 91 test: Protocol wrong type for socket 92 test: Protocol not available 93 test: Protocol not supported 94 test: Socket type not supported 95 test: Operation not supported 96 test: Protocol family not supported 97 test: Address family not supported by protocol 98 test: Address already in use 99 test: Cannot assign requested address100 test: Network is down101 test: Network is unreachable102 test: Network dropped connection on reset103 test: Software caused connection abort104 test: Connection reset by peer105 test: No buffer space available106 test: Transport endpoint is already connected107 test: Transport endpoint is not connected108 test: Cannot send after transport endpoint shutdown109 test: Too many references: cannot splice110 test: Connection timed out111 test: Connection refused112 test: Host is down113 test: No route to host114 test: Operation already in progress115 test: Operation now in progress116 test: Stale NFS file handle117 test: Structure needs cleaning118 test: Not a XENIX named type file119 test: No XENIX semaphores available120 test: Is a named type file121 test: Remote I/O error122 test: Disk quota exceeded123 test: No medium found124 test: Wrong medium type125 test: Operation canceled126 test: Required key not available127 test: Key has expired==== about to find EDOM 33 test: Numerical argument out of domain==== about to do socket stufffirst setsockopt() workedsecond setsockopt() worked

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.