Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7833201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T12:36:03+00:00 2026-06-02T12:36:03+00:00

What could possible by the problem when the errno value is not updated during

  • 0

What could possible by the problem when the errno value is not updated during successive calls to socket functions?

socket (AF_INET, -1, 0);
socket (AF_INET, SOCK_STREAM, -1);

The first should have errno = EINVAL
The second should have errno = EPROTONOSUPPORT


From the code provided below by @JonathanLeffler, output from cygwin is as follows:

*$ ./socket.exe
Error from socket(AF_INET, -1, 0): 124 (Socket type not supported)
-- errno = 124 (Socket type not supported)
Error from socket(AF_INET, SOCK_STREAM, -1): 123 (Protocol not supported)
-- errno = 123 (Protocol not supported)
socket(AF_INET, SOCK_STREAM, 0) succeeded (fd = 3)
-- errno = 123 (Protocol not supported)*

The said code below was edited to create a socket fd0 before creating the socket

int fd0 = socket(AF_INET, SOCK_STREAM, 0);
if (fd0 < 0)
    printf("Error from socket(AF_INET, SOCK_STREAM, 0): %d (%s)\n", errno, strerror(errno));
else
{
    printf("socket(AF_INET, SOCK_STREAM, 0) succeeded (fd = %d)\n", fd0);
    close(fd0);
}
printf("-- errno = %d (%s)\n", errno, strerror(errno));

int fd1 = socket(AF_INET, -1, 0);
.....

And the result is as follows:

*$ ./socket.exe
socket(AF_INET, SOCK_STREAM, 0) succeeded (fd = 3)
-- errno = 0 (No error)
Error from socket(AF_INET, -1, 0): 124 (Socket type not supported)
-- errno = 124 (Socket type not supported)
Error from socket(AF_INET, SOCK_STREAM, -1): 123 (Protocol not supported)
-- errno = 123 (Protocol not supported)
socket(AF_INET, SOCK_STREAM, 0) succeeded (fd = 3)
-- errno = 123 (Protocol not supported)*

It is expected that the first and the last socket creation should have the same errno values.


But how can this output be explained? 5th and 6th socket creation have the same errno value but the cause of the error is different.

$ ./test_select.exe
[1] ierr = 124, iSocket = -1 socket(AF_INET, -1, 0);
 : Socket type not supported
[2] ierr = 124, iSocket = -1 socket(AF_INET, -1, 0);
 : Socket type not supported
[3] ierr = 124, iSocket = 3 socket(AF_INET, SOCK_STREAM, 0);
 : Socket type not supported
[4] ierr = 124, iSocket = -1 socket (AF_INET, -1, 0);
 : Socket type not supported
[5] ierr = 123, iSocket = -1 socket (AF_INET, SOCK_STREAM, -1);
 : Protocol not supported
[6] ierr = 123, iSocket = -1 socket (AF_INET, -1, 0)
 : Protocol not supported
[7] ierr = 124, iSocket = -1 socket(AF_INET, SOCK_STREAM, -1)
 : Protocol not supported

Actual code is as follows:

   sockfd = socket(AF_INET, -1, 0);
    err = errno;
    printf("[1] err = %d, sockfd = %d socket(AF_INET, -1, 0);\n", err, sockfd);
    perror(" ");
    sockfd = socket(AF_INET, -1, 0);
     err = errno;
    printf("[2] err = %d, sockfd = %d socket(AF_INET, -1, 0);\n", err, sockfd);
    perror(" ");
     sockfd = socket(AF_INET, SOCK_STREAM, 0);
    err = errno;
    printf("[3] err = %d, sockfd = %d socket(AF_INET, SOCK_STREAM, 0);\n", err, sockfd);
    perror(" ");
    close(sockfd);
    sockfd = socket(AF_INET, -1, 0);
    err = errno;
    printf("[4] err = %d, sockfd = %d socket (AF_INET, -1, 0);\n", err, sockfd);
    perror(" ");
    sockfd = socket(AF_INET, SOCK_STREAM, -1);
    err = errno;
    printf("[5] err = %d, sockfd = %d socket (AF_INET, SOCK_STREAM, -1);\n", err, sockfd);
    perror(" ");
    sockfd = socket(AF_INET, -1, 0);
    err = errno;
    printf("[6] err = %d, sockfd = %d socket (AF_INET, -1, 0)\n", err, sockfd);
    perror(" ");
    err = errno;
    sockfd = socket(AF_INET, SOCK_STREAM, -1);
    printf("[7] err = %d, sockfd = %d socket(AF_INET, SOCK_STREAM, -1)\n", err, sockfd);
    perror(" ");
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T12:36:04+00:00Added an answer on June 2, 2026 at 12:36 pm

    No library function sets errno to zero. If you want it zeroed, your program must do it.

    You can only meaningfully test errno for information when a function says it has failed (and even then, only if its manual page indicates that it sets errno). You can find errno set to non-zero by a successful function. For example, on Solaris, you’ll often find ENOTTY in errno after a standard I/O call when writing to a file instead of a terminal.

    In your example, there’s no guarantee about which of multiple possible error values will be set by a given erroneous call to a function. As long as one valid error condition is reported, it is up to the system to decide which error it reports. So, you’d need to give a compelling example of why the second call should return EINVAL instead of EBADF; your question as written doesn’t include enough information to let us pontificate.

    Also note that the only safe way to declare errno is via the header: #include <errno.h>. In threaded environments in particular, it is often not simply extern int errno;. For example, on Mac OS X, it can be defined as:

    extern int * __error(void);
    #define errno (*__error())
    

    That is, __error is a function returning a pointer to an integer, and errno is a macro that dereferences that pointer, becoming a modifiable lvalue.


    Code Analysis

    The given code is:

    socket (AF_INET, -1, 0);
    socket (AF_INET, SOCK_STREAM, -1);
    

    Converted into runnable code, this might become:

    #include <errno.h>
    #include <stdio.h>
    #include <string.h>
    #include <sys/socket.h>
    #include <unistd.h>
    
    int main(void)
    {
        int fd1 = socket(AF_INET, -1, 0);
        if (fd1 < 0)
            printf("Error from socket(AF_INET, -1, 0): %d (%s)\n", errno, strerror(errno));
        else
        {
            printf("socket(AF_INET, -1, 0) succeeded (fd = %d)\n", fd1);
            close(fd1);
        }
        printf("-- errno = %d (%s)\n", errno, strerror(errno));
    
        int fd2 = socket(AF_INET, SOCK_STREAM, -1);
        if (fd2 < 0)
            printf("Error from socket(AF_INET, SOCK_STREAM, -1): %d (%s)\n", errno, strerror(errno));
        else
        {
            printf("socket(AF_INET, SOCK_STREAM, -1) succeeded (fd = %d)\n", fd2);
            close(fd2);
        }
        printf("-- errno = %d (%s)\n", errno, strerror(errno));
    
        int fd3 = socket(AF_INET, SOCK_STREAM, 0);
        if (fd3 < 0)
            printf("Error from socket(AF_INET, SOCK_STREAM, 0): %d (%s)\n", errno, strerror(errno));
        else
        {
            printf("socket(AF_INET, SOCK_STREAM, 0) succeeded (fd = %d)\n", fd3);
            close(fd3);
        }
        printf("-- errno = %d (%s)\n", errno, strerror(errno));
    
        return(0);
    }
    

    Running on Mac OS X 10.7.3, this produces:

    Error from socket(AF_INET, -1, 0): 43 (Protocol not supported)
    -- errno = 43 (Protocol not supported)
    Error from socket(AF_INET, SOCK_STREAM, -1): 43 (Protocol not supported)
    -- errno = 43 (Protocol not supported)
    socket(AF_INET, SOCK_STREAM, 0) succeeded (fd = 3)
    -- errno = 43 (Protocol not supported)
    

    As I said earlier, the error returned depends on the system.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 25; I am
I would like to know if it could be possible to add a file
One possible solution could be simple HTTP get or post request, but that wouldn't
Possible Duplicate: Could anyone explain these undefined behaviors (i = i++ + ++i ,
I am curious to know that could it be possible that in the below
Could anyone tell me if it is possible to use the flex 4 framework
Could someone tell if it's possible to get an OpenCL code working with both
Is it possible that the data of a QRCode could contain an image and
Possible Duplicate: A better way to compare Strings which could be null I have
Is it possible that adding more import statements to your java code could slow

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.