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.
 
What I want is a program listening on a port, just like FTP or maybe an instant messaging bot based on Alice (www.alicebot.org). Since I don't know any C or C++ I would want to do it in C#, or else I have to forget the whole idea.
So to start off, I need to know if it is possible and next, I would go to figure out how to write daemons in general.
 
There is actually no documentation to find on the internet about writing daemons or how to set them up to run in the background. Maybe I just look for the wrong keywords, or google is playing games with me = )
 
And yes, writing a service/daemon once might become easier to have it both on windows and on unix/linux.
 
So there is no "need" for C# doing daemons, but in my case it might become helpfull. C and C++ will still be very good languages for that, I know. But in general I was just curious how to built them and set them up, and I would do it in C#.
 
I hope it is more clear now.....
 
Regards,
Aschwin

Reply via email to