Daytimec

The Daytime Server application can be broken into two basic functional parts. The first is the TCP socket server.

Our Daytime Server application binds to TCP port 1024 when looking for new connections. Ordinarily a daytime service binds to TCP port 13; however, Linux has a security feature where only trusted users can bind to any port below 1023. The second feature is the insertion of data into a SQLite database. Listing 13.8 shows the code for the Daytime Server application.

A Required ' headers

Listing 13.8 daytime.c

#include <time.h> #include <stdio.h> #include <string.h> #include <errno.h> #include <arpa/inet.h> #include <netinet/in.h> #include <sys/socket.h> #include <resolv.h> #include "sqlite3.h"

int portnumber = 1024; <—Q Listening port number

#define htons(a)

( ((a & oxooff) << 8) | ((a & oxffoo) >> 8)) <—Q Define helpful macro void RecordHit (char * when) {

int rc ; sqlite3 *db; char *zErrMsg = o; char sql [2oo] ;

rc = sqlite3_open("daytime_db.db",&db);

printf( "Can't open database: %s\n", sqlite3_errmsg(db)); sqlite3_close(db); return;

SQLite O interaction bzero(sql,sizeof(sql));

sprintf(sql,"insert into hits values (DATETIME('NOW'),'%s');",when); rc = sqlite3_exec (db, sql, NULL, 0, SzErrMsg);

sqlite3_close(db);

int listenfd, connfd;

struct sockaddr_in servaddr;

time_t ticks;

int rc;

fd_set readset; int result; struct timeval tv;

printf("Daytime Server\n");

F Set up and listen listenfd = socket (AF_INET, SOCK_STREAM, o) ; <1-1 on socket bzero(&servaddr,sizeof(servaddr) ) ; servaddr.sin_family = AF_INET;

servaddr.sin_addr.s_addr = INADDR_ANY; servaddr.sin_port = htons(PORTNUMBER);

rc = bind(listenfd, (struct sockaddr *) &servaddr,sizeof(servaddr));

printf ("after bind,rc = [%d] \n",rc); return rc ;

0 0

Post a comment