/*server.c - demonstration server program. Written in C Nov '94 from
Pascal version of Nov/Dec '93
revised Jan 2005 to use stdio.h and to count number of clients
*/
#include <stdio.h>
#include "sockets.c.h"
#define TRUE 1
socket_id server_socket;
int count1,count2;
int authentication(char box[],char id[], socket_id sock);
int query_dealing(char sending_data[],socket_id sock);
int viewing_data (char data[],socket_id sock);
int clientcount;
void serve_client(socket_id sock)
{
/*procedure to implement the service */
char message[500], box[10],id[50];
char query[256];
char data[256];
char sending_data[256];
/* set up data to serve to client */
sprintf(message,"Hello and welcome to NuTV's online service ",clientcount);
/* now send message to client */
send_data(sock,message,strlen(message)+1);
count1 = recv_data(sock,box);
send_data(sock,"+OK",4);
count2 = recv_data(sock,id);
send_data(sock,"+OK",4);
authentication(box,id,sock);
//char Q;
// recv_data(sock,sending_data);
// if (sending_data[0]==Q)
// {
// query_dealing(sending_data,sock);
// }
// else
// {
// viewing_data (sending_data,sock);
// }
close_socket(sock);
}
int authentication(char box[],char id[],socket_id sock)
{
printf("entering auth\n");
#define THEFILE "/usr/users/students/group_r/r020032/sockets1/customeraccounts.txt"
char part1[10];
char part2[6];;
char part3[256];
char part4[6];
/* pointer for the file */
FILE *fp;
/* file reading array */
/* open file for reading */
fp=fopen(THEFILE,"r");
if(fp!= NULL)
{
printf("scanning\n");
/* scans in the first 2 tab fields from the customeraccounts file and stores them in the a\
rrays part1 & part2 */
fscanf(fp, "%s %s %s %s",part1,part2,part3,part4);
printf("This the settop number %s and this is the customer ID number %s\n",part1,part2);
} else {
printf("Cannot open the file.\n");
}
if (fclose(fp)!=0)
{
printf("the file could not be closed.\n");
}
printf("we are here now\n");
if ((strcmp(box,part1)==0)&& (strcmp(id,part2)==0))
{
printf("entering authorise section");
printf("your set top box number is %d char long and is %s\n",count1,box);
printf("your id number is %d char long and is %s\n",count2,id);
printf(" Congraulations you have been authenticated");
send_data(sock,"+OK",4);
}
else
{
printf(" your numbers did not match!\n");
send_data(sock,"-ERR",5);
}
}
// int query_dealing(char sending_data[],socket_id sock)
// {
// printf("hi i am in query_handling\n");
// recieves the query from the client. Opens the viewing information folder and uses the info\
sent to send back the details wanted
//
// if (data[1]==1)
// {
// }
// else if (data[1]==2
// {// }
// else
// {
// }
//}
//int viewing_data(char data[],socket_id sock)
// {
// printf("hi i am in data");
// recv_data(sock,data);
// gets the viewing data from the client and writes it to a new file
// }void accept_clients()
{ int process;
socket_id new_socket;
host_name client_host;
/* look for and deal with clients */
while (TRUE)
{
printf(".... server waiting to accept connection\n");
new_socket = accept_connection(server_socket, client_host);
if (new_socket<0)
{
printf("failed to accept a connection\n");
exit(1);
}
else
{ /* client connection accepted, now create service process */
printf("server: connection accepted from %s \n",client_host);
clientcount++;
process = fork();
if (process == 0) /* child server process */
{
close_socket(server_socket);
serve_client(new_socket);
exit(0);
}
else
{ /*parent server process*/
close_socket(new_socket);
} }
} /*client processed*/
} /*loop forever */
} /*accept & serve clients */
main()
{ port_number port;
int ok;
printf("server ...\n");
printf("please enter port number\n");
scanf("%d",&port);
server_socket = create_socket();printf("server: socket %d created, trying to bind name ...",server_socket);
ok=bind_socket(server_socket, port);
if (ok < 0)
{
printf("failed to bind name to socket(ok =%d)",ok);
}
else
{
printf("server: name bound to socket, now listen ...\n");
listen(server_socket,5);
accept_clients();
}
}