Skip to content

Instantly share code, notes, and snippets.

@sorbits
Created February 10, 2021 21:24
Show Gist options
  • Save sorbits/43461943741f8833d491a223e07756b7 to your computer and use it in GitHub Desktop.
Save sorbits/43461943741f8833d491a223e07756b7 to your computer and use it in GitHub Desktop.
#include <fcntl.h>
#include <sys/socket.h>
#include <sys/un.h>
int main (int argc, char const* argv[])
{
int fd = socket(AF_UNIX, SOCK_STREAM, 0);
if(fd != -1)
{
char const* socket_path = "/tmp/test.socket";
fcntl(fd, F_SETFD, FD_CLOEXEC);
struct sockaddr_un addr = { 0, AF_UNIX };
strcpy(addr.sun_path, socket_path);
addr.sun_len = SUN_LEN(&addr);
if(bind(fd, (sockaddr*)&addr, sizeof(addr)) == -1)
fprintf(stderr, "Could not bind to socket: %s\n", socket_path);
else if(listen(fd, SOMAXCONN) == -1)
fprintf(stderr, "Could not listen to socket");
}
else
{
perror("Unable to create socket");
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment