OzHub, the Macquarie Telecom-led cloud computing alliance, has come down firmly on the side of Optus over the copyright controversy surrounding Optus TV Now, warning that any moves to change the law "risk branding Australia a global luddite state."
read more
David M Williams
Monday, 20 August 2007 21:18
Socket handling
This daemon, like any other, is designed to run in the background, and respond to network connections over TCP/IP. We can achieve this using the best known TCP/IP handling interface, Berkeley sockets. Our main() routine in dwserv.cpp sets it all up.
First, as we are going to be creating accounts, the server must be run as the super-user. However, this need not cause any fear, because our server is both robust and secure. And, of course, it is open source and can thus be inspected and enhanced.
We then establish a server socket, bound to the port we have chosen to use, as specified by the port variable above.
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = INADDR_ANY;
sin.sin_port = htons (port);
if ((ppe = getprotobyname ("tcp")) == 0)
errexit ("Can't find tcp: %s\n", strerror (errno));
if ((sock = socket (PF_INET, SOCK_STREAM, ppe->p_proto)) < 0)
errexit ("Can't create socket: %s\n", strerror (errno));
if (bind (sock, (struct sockaddr *) &sin, sizeof (sin)) < 0)
errexit ("Can't bind to port: %s\n", strerror (errno));
if (listen (sock, qlen) < 0)
errexit ("Can't listen on port: %s\n", strerror (errno));
We set the server to run in the background by using the fork system call to create a second process, and by disconnecting the new process from the controlling tty. The original process is terminated.
if (pid) // Non-zero is parent.
{
printf ("Server pid is %d.\n", pid);
if (strlen (FileName) > 0)
log ("Server pid is %d.\n\n", pid);
exit (0);
}
fd = open ("/dev/tty", O_RDWR);
ioctl (fd, TIOCNOTTY, 0);
close (fd);
The server now simply sits passively in the background listening forever for network connections.
If an incoming request is made, then a new, slave, socket is created to process it. This is hived off into a child process. This permits the server socket and process to continue accepting more connections because it has offloaded the processing. This gives the appearance of allowing multiple simultaneous connections.
strcpy (Remote, IPtoAddress (fsin.sin_addr));
log ("%s: connect from %s\n", CurrentDateTime (nowtime), Remote);
The process method, called above, deciphers the command received from the client and calls the appropriate method to deal with it - such as CreateAccount. Any error messages are returned to the client through the same socket.

|
Microsoft Office 365Try an easy-to-use set of web-enabled tools for business-class productivity services. Office 365 provides anywhere-access to email, important documents, contacts, and calendars on almost any device. |