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 497593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T05:47:47+00:00 2026-05-13T05:47:47+00:00

Linux GCC 4.4.2 I am doing some socket programming. However, I keep getting this

  • 0

Linux GCC 4.4.2

I am doing some socket programming.

However, I keep getting this error when I try and assign the sockfd from the socket function.

" Socket operation on non-socket"

Many thanks for any advice,

#if defined(linux)
#include <pthread.h>
/* Socket specific functions and constants */
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <errno.h>
#endif

#include "server.h"
#include "cltsvr_ults.h"

/* Listens for a connection on the designated port */
void wait_client()
{
    struct addrinfo add_info, *add_res;
    int sockfd;

    /* Load up the address information using getaddrinfo to fill the struct addrinfo */
    memset(&add_info, 0, sizeof(add_info));
    /* Use either IPv4 or IPv6 */
    add_info.ai_family = AF_UNSPEC; 
    add_info.ai_socktype = SOCK_STREAM;
    /* Fill in my IP address */
    add_info.ai_flags = AI_PASSIVE;

    /* Fill the struct addrinfo */
    int32_t status = 0;
    if(status = getaddrinfo(NULL, "6000", &add_info, &add_res) != 0)
    {
        fprintf(stderr, "getaddrinfo [ %s ]\n", gai_strerror(status));

        return;
    }

    if((sockfd = (socket(add_res->ai_family, add_res->ai_socktype, add_res->ai_protocol)) == -1))
    {
        fprintf(stderr, "Socket failed [ %s ]\n", strerror(errno));

        return;
    }

    /* Bind to the port that has been assigned by getaddrinfo() */
    if(bind(sockfd, add_res->ai_addr, add_res->ai_addrlen) != 0)
    {
        fprintf(stderr, "Bind failed [ %s ]\n", strerror(errno));

        return;
    }

    printf("Listening for clients\n");
}

Edit == coding in the old-school method

    int32_t sockfd = 0;
    struct sockaddr_in my_addr;

    memset(&my_addr, 0, sizeof(my_addr));

    my_addr.sin_family = AF_INET;
    my_addr.sin_port = htons(6000);
    my_addr.sin_addr.s_addr = inet_addr("127.0.0.1");

    sockfd = socket(PF_INET, SOCK_STREAM, 0);
    if(sockfd == -1)
    {
    fprintf(stderr, "Socket failed [ %s ]\n", strerror(errno));
    return;
    }

    if(bind(sockfd, (struct sockaddr *) &my_addr, sizeof(my_addr)) == -1)
    {
    fprintf(stderr, "Bind failed [ %s ]\n", strerror(errno));
    return;
    }
  • 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-05-13T05:47:47+00:00Added an answer on May 13, 2026 at 5:47 am

    Your main problem is that you have the wrong check for when socket() has an error. socket() will return -1 on error, not 0 on success. You may be getting a good socket value (2, 3, etc.) and treating it like an error.

    There is also a second problem in the way you are parenthesizing your code. When you write:

    if (sockfd = socket(add_res->ai_family, add_res->ai_socktype, add_res->ai_protocol) != 0)
    

    That is being treated as:

    if (sockfd = (socket(add_res->ai_family, add_res->ai_socktype, add_res->ai_protocol) != 0))
    

    So sockfd will not be assigned the return value of socket, but the value of comparing it against 0. Fixing both problems, you should be writing:

    if ((sockfd = socket(add_res->ai_family, add_res->ai_socktype, add_res->ai_protocol)) == -1)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm porting a small C++ console application from windows to linux, GCC 4.3.2. When
Linux provides the stime(2) call to set the system time. However, while this will
Doing cross platform development with 64bit. Using gcc/linux and msvc9/server 2008. Just recently deployed
I'm doing some experiments with opengl in c for linux. I've got the following
I was randomly testing std::thread in my virtual linux machine (GCC 4.4.5-Debian) with this
I'm fine working on Linux using gcc as my C compiler but would like
I am working on Linux with the GCC compiler. When my C++ program crashes
I don't understand how GCC works under Linux. In a source file, when I
I wish to build a compiler (GCC port) for Linux, so that the built
On Linux/NPTL , threads are created as some kind of process. I can see

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.