a little futzing around with sockets while reading through the linux programming interface book
This commit is contained in:
parent
74dfe433b9
commit
ef128a996f
10
.gitignore
vendored
10
.gitignore
vendored
@ -1,4 +1,6 @@
|
||||
gowrikumar/bin
|
||||
gdbtut/bin
|
||||
*.i
|
||||
*.s
|
||||
*
|
||||
!gowrikumar/bin
|
||||
!gdbtut/bin
|
||||
!*.i
|
||||
!*.s
|
||||
!*.c
|
||||
|
4
Makefile
4
Makefile
@ -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
48
socky.c
Normal 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;
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user