#include #include #include #include "gis.h" int main (int argc, char *argv[]) { int listenfd, rwfd; char *path; pid_t pid; struct sockaddr_un addr = {0}; /* Path is built using server's name */ // path: F-des if (NULL == (path = G_sock_get_fname (argv[0]))) exit (EXIT_FAILURE); /* Make sure another instance isn't running */ if (G_sock_exists (path)) { if ((listenfd = G_sock_connect (path)) != -1) { close (listenfd); exit (EXIT_FAILURE); } remove (path); } /* Bind the socket */ // listenfd: F-des, path: F-des if ((listenfd = G_sock_bind (path)) < 0) exit (EXIT_FAILURE); /* Begin listening on the socket */ // 1: int queue length, should never be = 0 if (G_sock_listen (listenfd, 1) != 0) exit (EXIT_FAILURE); /* Loop forever waiting for connections */ for (;;) { if ((rwfd = G_sock_accept (listenfd)) < 0) { if (errno == EINTR) continue; } else exit (EXIT_FAILURE); /* Fork connection */ // parent process: listen to socket // child process: process request // pid: pid of new child process if ((pid = fork()) == 0) { char c; /* child closes listenfd */ close (listenfd); // rwfd: F-des, c: Zeichenvektor,aus dem Zeichen gelesen werden sollen // 1: Anz zu lesende bytes while (read (rwfd, &c, 1) > 0) write (rwfd, &c, 1); // schreibt 1 char von c in rwfd close (rwfd); return 0; } else if (pid > 0) { /* parent closes rwfd * a well behaved server would limit * the number of forks. */ close (rwfd); } else exit (EXIT_FAILURE); } G_free (path); return 0; }