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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:40:40+00:00 2026-05-31T05:40:40+00:00

I am having a weird problem. I am trying to create a unix socket

  • 0

I am having a weird problem. I am trying to create a unix socket in a directory but it does not create the one I want. I have cut down the code to the below example.

#include <iostream>
#include <string>
#include <cstdlib>
#include <string.h>
#include <sys/un.h>
#include <sys/socket.h>
#include <errno.h>
#include <limits.h>

int main(int argc, char *argv[])
{
   std::string socketname(argv[1]);

   socketname += "my_socket";

    int  fd;
    int  result;
    struct  sockaddr_un addr;

    fd = socket(AF_UNIX, SOCK_STREAM, 0);

    if (fd == -1)
    {
        std::cerr << "socket returned " << errno << ": " << strerror(errno) << std::endl;
        exit(1);
    }

    memset(&addr, 0, sizeof(struct sockaddr_un));

    addr.sun_family = AF_UNIX;

    strncpy(addr.sun_path, socketname.c_str(), sizeof(addr.sun_path) - 1);

    result = bind(fd, (struct sockaddr *) &addr, sizeof(struct sockaddr_un));

    if (result == -1)
    {
        std::cerr << "bind returned " << errno << ": " << strerror(errno ) << std::endl;
        exit(1);
    }

    return 0;
}

The problem is that when I run program like

./a.out /home/rasterblaster/local/media/video/television/UnitedStates/series/Californication/SeasonOne/xvid-conversions

I do not get “my_socket”. Instead I find some random socket named like “xvid-convers”.

/home/rasterblaster/local/media/video/television/UnitedStates/series/Californication/SeasonOne$ ls -la

drwxrwxrwx 3 rasterblaster rasterblaster  4096 2012-03-08 22:06 .
drwxrwxrwx 3 rasterblaster rasterblaster  4096 2012-03-08 21:39 ..
srwxrwxr-x 1 rasterblaster rasterblaster  0 2012-03-08 22:06:08 xvid-convers
drwxrwxrwx 2 rasterblaster rasterblaster  4096 2012-03-08 21:39 xvid-conversions

What am I doing wrong?

  • 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-31T05:40:42+00:00Added an answer on May 31, 2026 at 5:40 am

    There is nothing really weird going on. If you look at the result of sizeof(addr.sun_path) it should be about 100 or so bytes. When you try to copy more than that size into it with strncpy() you are truncating the path to what fits into sun_path and bind() is just creating a filesystem entry for any name beyond the last valid directory name.

    There is no benefit to placing a socket this deep into a directory structure. You can just as easily put it in /tmp or /run or some common directory that just clients and the server have permission if you want a bit more security.

    Also, you probably want

    socketname += "/my_socket";  // prefix with slash
    

    just in case the path named passed doesn’t end in a slash.

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

Sidebar

Related Questions

I am trying to write the manifest file but I am having weird problem
I have a weird problem! I'm trying to create a listview with checkboxes. In
I'm having a weird problem, and not sure why. I have a main activity
This is a really weird problem that I have been having. When I download
This is a weird problem I have started having recently. My team is developing
all. I'm having a bit of weird problem with client server program. I have
I'm having a weird problem i'm trying to read a string from a console
I'm having a weird problem on deployment while trying to use Microsoft Enterprise Library's
I'm trying to build a simple guessing game. I'm having a weird problem: -(int)setRandom
Greetings. I'm having a weird problem with jQuery on my website. I'm trying 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.