How to deal: can't connect to the socket

Viewed 33

I have created socket s and want to send some command with it:

int s = 0;
struct sockaddr_un remote;

if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
    throw std::runtime_error("can't create a socket");
}

remote.sun_family = AF_UNIX;
strcpy(remote.sun_path, "/tmp/my.sock");
size_t len = strlen(remote.sun_path) + sizeof(remote.sun_family);

if (connect(s, reinterpret_cast<sockaddr*>(&remote), static_cast<socklen_t>(len)) == -1) { 
    throw std::runtime_error("can't connect to the socket"); // <--- it throws this guy
}

if (send(s, command.c_str(), command.size(), 0) == -1) {
    throw std::runtime_error("can't send data to the socket");
}

Why I can't connect to it?

0 Answers
Related