[Mono-list] RE: Writing daemons in C#

Pablo Baena pbaena@uol.com.ar
19 Nov 2002 10:12:32 +0000


--=-pqOLaeCMllgMWS9egu1x
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

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
    

--=-pqOLaeCMllgMWS9egu1x
Content-Type: text/html; charset=utf-8

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN">
<HTML>
<HEAD>
  <META HTTP-EQUIV="Content-Type" CONTENT="text/html; CHARSET=UTF-8">
  <META NAME="GENERATOR" CONTENT="GtkHTML/1.0.4">
</HEAD>
<BODY BGCOLOR="#ffffff">
There's plenty of documentation out there about writing daemons. This article shows you how to make them in Linux, NT and Java
<BR>
<A HREF="http://www-106.ibm.com/developerworks/library/sockets/understanding-sockets.html">http://www-106.ibm.com/developerworks/library/sockets/understanding-sockets.html</A>
<BR>

<BR>
Basically it is:
<BR>

<BR>
Listen on a port;
<BR>
while (true) {
<BR>
 Accept a connection
<BR>
 Fork -start a new thread-
<BR>
}
<BR>

<BR>
You can make a daemon in any language you want (Perl is easy).
<BR>

<BR>
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.
<BR>

<BR>
#include &lt;sys/types.h&gt;
<BR>
#include &lt;sys/socket.h&gt;
<BR>
#include &lt;netinet/in.h&gt;
<BR>
#include &lt;unistd.h&gt;
<BR>
#include &lt;stdlib.h&gt;
<BR>
#include &lt;stdio.h&gt;
<BR>
#include &lt;pthread.h&gt;
<BR>

<BR>
#define BUFSIZE 1024
<BR>
#define PORTNUM 8500		// Numero del puerto
<BR>

<BR>
void do_something (void *);
<BR>

<BR>
int
<BR>
main ()
<BR>
{
<BR>
	int s, new_s, addrlen;
<BR>
	struct sockaddr_in sock_addr;
<BR>
	pid_t pid;
<BR>
	pthread_t thread;
<BR>

<BR>
	if ((s = socket (AF_INET, SOCK_STREAM, 0)) &gt; 0)
<BR>
		printf (&quot;Socket creado.\n&quot;);
<BR>
	else
<BR>
	{
<BR>
		printf (&quot;No se pudo crear socket.\n&quot;);
<BR>
		exit (1);
<BR>
	}
<BR>

<BR>
	addrlen = sizeof (sock_addr);
<BR>
	sock_addr.sin_family = AF_INET;
<BR>
	sock_addr.sin_addr.s_addr = INADDR_ANY;
<BR>
	sock_addr.sin_port = htons (PORTNUM);
<BR>
	if (bind (s, (struct sockaddr *) &amp;sock_addr, addrlen) == 0)
<BR>
		printf (&quot;Asociando socket al puerto %d...\n&quot;, PORTNUM);
<BR>
	else
<BR>
	{
<BR>
		printf (&quot;No se puede asociar socket.\n&quot;);
<BR>
		exit (1);
<BR>
	}
<BR>

<BR>
	if (listen (s, 3) &lt; 0)
<BR>
	{
<BR>
		printf (&quot;No se puede escuchar socket.\n&quot;);
<BR>
		exit (1);
<BR>
	}
<BR>

<BR>
	while ((new_s =
<BR>
		&nbsp;&nbsp;&nbsp;&nbsp; accept (s, (struct sockaddr *) &amp;sock_addr,
<BR>
			&nbsp;&nbsp;&nbsp;&nbsp; &amp;addrlen)) &gt; 0)
<BR>
	{
<BR>
		printf (&quot;Conexion aceptada. Lanzando proceso hijo.\n&quot;);
<BR>

<BR>
		pthread_create (&amp;thread, NULL,
<BR>
				(void *) &amp;do_something, (void *) new_s);
<BR>
	}
<BR>

<BR>
	printf(&quot;Error al aceptar la conexion.\n&quot;);
<BR>
	return 0;
<BR>
}
<BR>

<BR>

<BR>
void
<BR>
do_something (void *arg)
<BR>
{
<BR>
	int new_s, res;
<BR>
	pid_t this_pid;
<BR>
	char buffer[BUFSIZE];
<BR>

<BR>

<BR>
	new_s = (int *) arg;
<BR>
	this_pid = getpid ();
<BR>

<BR>
	while (read (new_s, buffer, BUFSIZE))
<BR>
	{
<BR>
		printf (&quot;Mensaje de proceso %d: %s.\n&quot;, this_pid, buffer);
<BR>

<BR>
// Write something to the socket ---I don't remember if this was strlen() or strlen()-1
<BR>
		if (!write (new_s, somedata, strlen(somedata))
<BR>
		{
<BR>
			printf (&quot;Error escribiendo al socket.\n&quot;);
<BR>
			close (new_s);
<BR>
			pthread_exit (0);
<BR>
		}
<BR>
	}
<BR>

<BR>
	close (new_s);
<BR>
	pthread_exit (0);
<BR>
}
<BR>

<BR>

<BR>
On Thu, 2020-11-19 at 09:11, Aschwin Wesselius wrote:
    <BLOCKQUOTE>
    <FONT COLOR="#737373"><FONT SIZE="2"><I>Thanks guys. I will define &quot;daemons&quot; a bit more.</FONT></FONT></I>
    <BR>
    <FONT COLOR="#737373"><FONT SIZE="3"><I>&nbsp;</FONT></FONT></I>
    <BR>
    <FONT COLOR="#737373"><FONT SIZE="2"><I>What I want is a program listening on a port, just like FTP or maybe an instant messaging bot based on&nbsp;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.</FONT></FONT></I>
    <BR>
    <FONT COLOR="#737373"><FONT SIZE="2"><I>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.</FONT></FONT></I>
    <BR>
    <FONT COLOR="#737373"><FONT SIZE="2"><I>&nbsp;</FONT></FONT></I>
    <BR>
    <FONT COLOR="#737373"><FONT SIZE="2"><I>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 = )</FONT></FONT></I>
    <BR>
    <FONT COLOR="#737373"><FONT SIZE="2"><I>&nbsp;</FONT></FONT></I>
    <BR>
    <FONT COLOR="#737373"><FONT SIZE="2"><I>And yes, writing a service/daemon once might become easier to have it both on windows and on unix/linux.</FONT></FONT></I>
    <BR>
    <FONT COLOR="#737373"><FONT SIZE="2"><I>&nbsp;</FONT></FONT></I>
    <BR>
    <FONT COLOR="#737373"><FONT SIZE="2"><I>So there is no &quot;need&quot; 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#.</FONT></FONT></I>
    <BR>
    <FONT COLOR="#737373"><FONT SIZE="2"><I>&nbsp;</FONT></FONT></I>
    <BR>
    <FONT COLOR="#737373"><FONT SIZE="2"><I>I hope it is more clear now.....</FONT></FONT></I>
    <BR>
    <FONT COLOR="#737373"><FONT SIZE="2"><I>&nbsp;</FONT></FONT></I>
    <BR>
    <FONT COLOR="#737373"><FONT SIZE="2"><I>Regards,</FONT></FONT></I>
    <BR>
    <FONT COLOR="#737373"><FONT SIZE="2"><I>Aschwin</FONT></FONT></I>
    <BR>
    
    </BLOCKQUOTE>
</BODY>
</HTML>

--=-pqOLaeCMllgMWS9egu1x--