a little futzing around with sockets while reading through the linux programming interface book

This commit is contained in:
Dan Buch 2011-11-06 09:53:00 -05:00
parent 74dfe433b9
commit ef128a996f
3 changed files with 57 additions and 5 deletions

10
.gitignore vendored
View File

@ -1,4 +1,6 @@
gowrikumar/bin
gdbtut/bin
*.i
*.s
*
!gowrikumar/bin
!gdbtut/bin
!*.i
!*.s
!*.c

View File

@ -4,10 +4,12 @@ RM = rm -v
CC := gcc
CFLAGS := -std=c99 -Wall -g
ALL_TARGETS := $(patsubst %.c,%,$(wildcard *.c))
export CD RM CFLAGS
all:
all: $(ALL_TARGETS)
$(CD) gowrikumar && $(MAKE)
$(CD) gdbtut && $(MAKE)

48
socky.c Normal file
View File

@ -0,0 +1,48 @@
#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;
}