|
There's plenty of documentation out there about writing daemons. This article shows you how to make them in Linux, NT and Java
http://www-106.ibm.com/developerworks/library/sockets/understanding-sockets.html Basically it is: Listen on a port; while (true) { Accept a connection Fork -start a new thread- } You can make a daemon in any language you want (Perl is easy). This is an example in C. Use it just as that. I'm not a C expert. I don't know how portable this is. #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <pthread.h> #define BUFSIZE 1024 #define PORTNUM 8500 // Numero del puerto void do_something (void *); int main () { int s, new_s, addrlen; struct sockaddr_in sock_addr; pid_t pid; pthread_t thread; if ((s = socket (AF_INET, SOCK_STREAM, 0)) > 0) printf ("Socket creado.\n"); else { printf ("No se pudo crear socket.\n"); exit (1); } addrlen = sizeof (sock_addr); sock_addr.sin_family = AF_INET; sock_addr.sin_addr.s_addr = INADDR_ANY; sock_addr.sin_port = htons (PORTNUM); if (bind (s, (struct sockaddr *) &sock_addr, addrlen) == 0) printf ("Asociando socket al puerto %d...\n", PORTNUM); else { printf ("No se puede asociar socket.\n"); exit (1); } if (listen (s, 3) < 0) { printf ("No se puede escuchar socket.\n"); exit (1); } while ((new_s = accept (s, (struct sockaddr *) &sock_addr, &addrlen)) > 0) { printf ("Conexion aceptada. Lanzando proceso hijo.\n"); pthread_create (&thread, NULL, (void *) &do_something, (void *) new_s); } printf("Error al aceptar la conexion.\n"); return 0; } void do_something (void *arg) { int new_s, res; pid_t this_pid; char buffer[BUFSIZE]; new_s = (int *) arg; this_pid = getpid (); while (read (new_s, buffer, BUFSIZE)) { printf ("Mensaje de proceso %d: %s.\n", this_pid, buffer); // Write something to the socket ---I don't remember if this was strlen() or strlen()-1 if (!write (new_s, somedata, strlen(somedata)) { printf ("Error escribiendo al socket.\n"); close (new_s); pthread_exit (0); } } close (new_s); pthread_exit (0); } On Thu, 2020-11-19 at 09:11, Aschwin Wesselius wrote: Thanks guys. I will define "daemons" a bit more. |
- [Mono-list] RE: Writing daemons in C# Aschwin Wesselius
- Re: [Mono-list] RE: Writing daemons in C# Erik B�gfors
- Re: [Mono-list] RE: Writing daemons in C# Miguel de Icaza
- [Mono-list] Re: RE: Writing daemons in C# Francesco Delfino
- Re: [Mono-list] Re: RE: Writing daemon... Mathias Hasselmann
- Re: [Mono-list] RE: Writing daemons in C# Pablo Baena
- Re: [Mono-list] RE: Writing daemons in C# Erik B�gfors
- [Mono-list] RE: Writing daemons in C# Aschwin Wesselius
