box-o-sand/PracticingC/socky.c
Dan Buch a04a502787 Add 'PracticingC/' from commit 'ef128a996fc826339bbc3d9deea376932caf6981'
git-subtree-dir: PracticingC
git-subtree-mainline: fd87ec8fe9
git-subtree-split: ef128a996f
2013-01-09 23:49:46 -05:00

49 lines
1.1 KiB
C

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/un.h>
#include <sys/types.h>
#include <sys/socket.h>
const char *SOCKNAME = "/tmp/socky";
int sfd;
struct sockaddr_un addr;
int main(int argc, char *argv[])
{
sfd = socket(AF_UNIX, SOCK_STREAM, 0);
if (sfd == -1) {
fprintf(stderr, "FAILED TO ALLOCATE SOCKET\n");
goto error;
}
memset(&addr, 0, sizeof(struct sockaddr_un));
addr.sun_family = AF_UNIX;
strncpy(addr.sun_path, SOCKNAME, sizeof(addr.sun_path) - 1);
if (bind(sfd, (struct sockaddr *)&addr, sizeof(struct sockaddr_un)) == -1) {
fprintf(stderr, "FAILED TO BIND SOCKET TO ADDRESS\n");
goto error;
}
if (listen(sfd, 128) == -1) {
fprintf(stderr, "FAILED TO LISTEN ON ADDRESS\n");
goto error;
}
for (;;) {
fprintf(stdout, "ATTEMPTING TO WAIT FOR REQUEST ON %s\n", SOCKNAME);
if (accept(sfd, NULL, NULL) == -1) {
fprintf(stderr, "FAILED TO ACCEPT REQUEST ON ADDRESS\n");
goto error;
}
}
return 0;
error:
return 1;
}