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

  • SEARCH
  • Home
  • 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 9136603
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:58:28+00:00 2026-06-17T08:58:28+00:00

I am getting an error Invalid argument when i call the accept() function on

  • 0

I am getting an error “Invalid argument” when i call the accept() function on the server side of a client-server application. I don’t get what is wrong and if you see what is wrong let me know please. Thanks.

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>


int main(int argc, char* argv[])
{
    int sockfd, newsockfd, portno, len;
    struct sockaddr_in server, client;

    if(argc < 2){
        perror("Add more arguments");
    }

    sockfd = socket(AF_UNIX,SOCK_STREAM,0);
    if(sockfd < 0){
        perror("Error at socket()");    
    }
    printf("Socketfd: %d\n",sockfd);
    bzero((char *)&server, sizeof(server));
    portno = atoi(argv[1]);
    server.sin_family = AF_UNIX;
    server.sin_addr.s_addr = INADDR_ANY;
    server.sin_port = htons(portno);

    if(bind (sockfd, (struct sockaddr *)&server, sizeof(server)) <0){
        perror("Error at bind ()-server");  
    }

    listen(sockfd,5);

    int readval;
    char buffer[256];
    for(;;){
        len = sizeof(client);
        newsockfd = accept (sockfd, (struct sockaddr *)&server,&len);
        if (newsockfd == -1){
            perror("Erroare at accept()");      
        }
        else do{        
            readval = (sockfd,buffer,sizeof(buffer));
            if (readval < 0 ){
                perror("Error at reading()");       
            }
            else if (readval == 0){
                printf("End conection");            
            }
            else{
                printf("Mesage is: %s\n", buffer);
            }
        }while (readval > 0);
        close(newsockfd);
    }
    return 0;   
}
  • 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-17T08:58:29+00:00Added an answer on June 17, 2026 at 8:58 am

    You have to use sockaddr_un instead of sockaddr_in for Unix domain sockets OR substitute AF_UNIX with AF_INET.

    Plus, check the return of listen.

    Plus change this line

    readval = (sockfd,buffer,sizeof(buffer));
    

    with

    readval = read(newsockfd,buffer,sizeof(buffer));
    

    because data is going to be sent through the newly created socket and not on the listening one

    Plus, as mentioned by @trojanfoe, subst the server with the client structure into the accept() call

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

Sidebar

Related Questions

I am getting error: Invalid argument supplied for foreach() This is my class for
I'm getting the error Invalid argument supplied for foreach() even though the array being
I am getting the following error : Invalid postback or callback argument. Event validation
im currently getting these error on my site: Warning: array_keys() [function.array-keys]: The first argument
I'm getting a compile-time error that my method call has some invalid arguments. public
Im getting invalid argument error when running the following code. Im trying to change
Am getting the error in the code igniter view page as Invalid argument supplied
So I'm getting a fantastically descriptive error message of Invalid Argument when trying to
I'm getting this error: Warning: Invalid argument supplied for foreach() in [page].php on line
i am getting an error: invalid lvalue in assignment. this is the only error

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.