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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T07:22:42+00:00 2026-06-03T07:22:42+00:00

I am trying to use AF_UNIX sockets under Mac Os X, and I have

  • 0

I am trying to use AF_UNIX sockets under Mac Os X, and I have made a structure that contains some information:

typedef struct
{
    int socket;
    int conn;
    struct sockaddr addr;
}socket_info;

And I use these functions:

socket_info* server_init(void)
{
  socket_info* result=(socket_info*)malloc(sizeof(socket_info));
  socklen_t length=sizeof(struct sockaddr);
  bzero(&(result->addr),sizeof(struct sockaddr));
  result->socket=socket(AF_UNIX,SOCK_STREAM,0);
  (result->addr).sa_family=AF_UNIX;
  (result->addr).sa_len=sizeof(struct sockaddr);
  strcpy((result->addr).sa_data,"./mysock");
  bind(result->socket,&(result->addr),length);
  listen(result->socket,MAX);
  result->conn=accept(result->socket,&(result->addr),&length);
  return result;
}


socket_info* client_init(socket_info* info)
{
  socket_info* result=(socket_info*)malloc(sizeof(socket_info));
  result->socket=socket(AF_UNIX,SOCK_STREAM,0);
  while( connect(result->socket,&(info->addr),sizeof(struct sockaddr))==-1)
  {
    if(errno==ENOENT)
    {
        sleep(1);
    }
    else
    {
        fprintf(stderr,"Error: %d",errno);
        free(result);
        result=NULL;
    }
  }
  return result;
}

And I use them in main:

int main(int argc, char** argv)
{
  socket_info *server_info, *client_info;
  pid_t pid;
  int fd[2];
  pipe(fd);
  pid=fork();
  if(pid==0)
  {
    char buffer[100];
    read(fd[0], server_info, sizeof(struct sockaddr));
    client_info=client_init(server_info);
    read(client_info->socket,buffer,100);
    printf("Messaggio ricevuto: %s",buffer);
  }
  else
  {
    const char* msg="ciao";
    server_info=server_init();
    write(fd[1],server_info,sizeof(struct sockaddr));
    write(server_info->socket,msg,sizeof(msg));
  }
  close(fd[0]);
  close(fd[1]);
  return 0;
}

But the client fails to connect: instead it prints the message: “Error: 61”.
What could be the reason?

  • 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-03T07:22:43+00:00Added an answer on June 3, 2026 at 7:22 am

    You have two main issues:

    1. You have a classic race condition. If the client tries to connect before the server sets up to listen, the client will get an error.

    2. Your server doesn’t check for any errors, so it could be failing and you wouldn’t know it.

    In either case, you’ll get exactly the error you are getting (connection refused).

    My money is on the first problem. If you flip the client and server so the child is the server, it will likely go from “happening not to work” to “happening to work”. Most operating systems let the child run first because that saves a ton of memory and eliminates huge numbers of page faults in the typical case where the child just calls exec.

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

Sidebar

Related Questions

I am trying to use non-blocking TCP sockets. The problem is that they are
I am trying use gem tire to search in my application. I have tables
Hi I'm trying use a datepicker on a field I have. I'm trying to
I am trying to write a set of simple client/sever programs that make use
I'm trying to write a UDP server in C (under Linux). I know that
I do have a java server. I am trying to build IPhone application that
I am trying out some python network programming on Mac OS X 10.6. I
I'm trying to use a lambda as a static member, like this: struct A
I am trying use the jQuery table sorter plugin for a table that is
I'm trying to use a socketpair to have a parent process provide input to

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.