Linux C Socket Error Codes
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company linux errno example Business Learn more about hiring developers or posting ads with us Stack Overflow Questions
How To Use Errno In C
Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million
Errno.h In C
programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Getting error code when creating a socket in Linux up vote 0 down vote favorite I'm doing some socket programming in
Errno C++
Linux and am wondering how to get the error code when the function socket(...); fails. for example for the "getaddrinfo" function i can do this: //Resolve the server address and port result = (struct addrinfo *) calloc(1, sizeof(struct addrinfo)); iResult = getaddrinfo("google.com", DEFAULT_PORT, &hints, &result); if (iResult != 0){ printf("%d\n", iResult); fprintf(stderr, "getaddrinfo failed: %s\n", gai_strerror(iResult)); getchar(); exit(EXIT_FAILURE); } However I want to do a similar thing using socket(...) function. According to this: http://linux.die.net/man/2/socket strerror(errno) the function returns -1 on failure, and sets errno to the appropriate error number. How do i access this "errno" though? This is my code so far: int connectSocket = 0; connectSocket = socket(AF_INET, SOCK_STREAM, 0); printf("%d\n", connectSocket); if (connectSocket == -1){ printf("socket failed with error: %s\n", error_string); //TODO: HELP DECLARING error_string getchar(); exit(EXIT_FAILURE); } Please help. Thanks! yahya c linux sockets share|improve this question asked Nov 24 '14 at 2:23 Yahya Uddin 2,04611440 2 See errno(3) and strerror(3) manual pages. –Nikolai N Fetissov Nov 24 '14 at 2:28 2 DO NOT allocate memory for the addrinfo that you pass in the last parameter of getaddrinfo(). It will allocate the addrinfo for you, which you pass to freeaddrinfo() to free it: result = NULL; iResult = getaddrinfo(..., &result); if (iResult == 0) { ...; freeaddrinfo(result); } –Remy Lebeau Nov 24 '14 at 6:29 add a comment| 2 Answers 2 active oldest votes up vote 2 down vote accepted errno is a thread-local global variable, defined in
DESCRIPTION top The
codes can’t occur on GNU systems, but they can occur using the GNU C Library on other systems. Macro: int EPERM Operation not permitted; only the owner of the file (or other resource) or http://www.gnu.org/s/libc/manual/html_node/Error-Codes.html processes with special privileges can perform the operation. Macro: int ENOENT No such file or directory. This is a “file doesn’t exist” error for ordinary files that are referenced in contexts where they are expected to already exist. Macro: int ESRCH No process matches the specified process ID. Macro: int EINTR Interrupted function call; an asynchronous signal occurred and prevented completion of the call. When this happens, you should try the call in c again. You can choose to have functions resume after a signal that is handled, rather than failing with EINTR; see Interrupted Primitives. Macro: int EIO Input/output error; usually used for physical read or write errors. Macro: int ENXIO No such device or address. The system tried to use the device represented by a file you specified, and it couldn’t find the device. This can mean that the device file was installed linux c socket incorrectly, or that the physical device is missing or not correctly attached to the computer. Macro: int E2BIG Argument list too long; used when the arguments passed to a new program being executed with one of the exec functions (see Executing a File) occupy too much memory space. This condition never arises on GNU/Hurd systems. Macro: int ENOEXEC Invalid executable file format. This condition is detected by the exec functions; see Executing a File. Macro: int EBADF Bad file descriptor; for example, I/O on a descriptor that has been closed or reading from a descriptor open only for writing (or vice versa). Macro: int ECHILD There are no child processes. This error happens on operations that are supposed to manipulate child processes, when there aren’t any processes to manipulate. Macro: int EDEADLK Deadlock avoided; allocating a system resource would have resulted in a deadlock situation. The system does not guarantee that it will notice all such situations. This error means you got lucky and the system noticed; it might just hang. See File Locks, for an example. Macro: int ENOMEM No memory available. The system cannot allocate more virtual memory because its capacity is full. Macro: int EACCES Permission denied; the file permissions do not allow the attempted operation. Macro: int EFAULT Ba