#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<netdb.h>
#include<ctype.h>
#include<string.h>
#include<time.h>
#define MAXLINE 4096
#define PORT 4600
void translate_day();
void translate_month();
void get_date();
void translate();
void error(char *str);
/*this is the structure that can represent
any date*/
typedef struct dat{
char day_name[20];
char day_number[20];
char year[20];
char time[20];
char month[20];
}date_st;
date_st date;
int date_length;
/*the date will be stored in sendline then
sent*/
char
sendline[MAXLINE+1];
int main() {
/*for handling the name of the server instead of the IP address*/
struct hostent *hp;
/*the socket for listening to connect requets*/
int sock_listen;
/*socket for having a connection to a specific client*/
int sock_service;
/*length of client struct sockaddr*/
int length;
/*
the struct address of both the client and the server*/
struct sockaddr_in servaddr, client_addr;
/*Creates the socket listen and verifies the returned value*/
if((sock_listen = socket(AF_INET, SOCK_STREAM,0))<0)
error("unable to open a socket\n");
/*Allocates space for the struct address of the server*/
bzero(&servaddr, sizeof(servaddr));
/*Fill the struct address*/
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(PORT);
servaddr.sin_addr.s_addr
= INADDR_ANY;
/*bind the socket and verify the returned value*/
if(bind(sock_listen,(struct
sockaddr*)&servaddr,sizeof(servaddr))!=0)
error("can not bind");
/*allow up to 5 connections*/
listen(sock_listen,5);
while(1){
length = sizeof(client_addr);
sock_service = accept(sock_listen,(struct
sockaddr*)&client_addr,&length);
/*Allocate space for the sendline*/
bzero((char*)&sendline,sizeof(sendline));
/*we get the english date*/
get_date();
date_length=strlen(sendline);
/*we translate the date to frensh*/
translate();
/*send the frensh date to the client*/
send(sock_service,(char*)&sendline,sizeof(sendline),0);
/*close the connection with the client*/
close(sock_service);
}
}
/* This function get the time of the OS (in
ms) by using (time) then it converts it
*
to a string representing a complete date(ctime)*/
void get_date() {
clock_t date1;
clock_t * clock1;
/*get the time of the system in ms from 1970*/
date1 = time(NULL);
clock1= &date1;
/*convert the ms into a complete date and copy this in sendline*/
strcpy(sendline,ctime(clock1));
}
/*this function translate the english date
to frensh date*/
/*first it scans the english date and put
it in a structure, then translates
*the day_name and the month and finally put the whole structure in
sendline
*following a frensh order*/
void translate(){
int i,j,k;
char temp[20];
i
= date_length;
j=0;
k=0;
printf("%s",sendline);
/*sanning the day name*/
while(j<i-1){ while(!(isspace(sendline[j]))){
temp[k]=sendline[j];j++;k++;}
temp[k]= '\0';
strcpy(date.day_name,temp);
j++;
k=0;
/*scaning the Month*/
while(!(isspace(sendline[j]))){
temp[k]=sendline[j];j++;k++;}
temp[k]= '\0';
strcpy(date.month,temp);
k=0;
j++;
/*scaning the day number */
while(!(isspace(sendline[j]))){
temp[k]=sendline[j];j++;k++;}
temp[k]= '\0';
strcpy(date.day_number,temp);
k=0;
j++;
/*scaning the time */
while(!(isspace(sendline[j]))){
temp[k]=sendline[j];j++;k++;}
temp[k]=
'\0';strcpy(date.time,temp);
k=0;
j++;
/*scanning the year*/
while(!(isspace(sendline[j]))){
temp[k]=sendline[j];j++;k++;}
temp[k]= '\0';
strcpy(date.year,temp);
j++;
}
sendline[0] =
'\0';
/*put the first component of sendline which
is the time*/
strcpy(sendline, date.time);
/*let some space*/
strcat(sendline," ");
/*tranlate the day name*/
translate_day();
/*put the second component which is the day
name*/
strcat(sendline,date.day_name);
/*put the third component which is the day
number*/
strcat(sendline,date.day_number);
strcat(sendline, " ");
/*translate the month*/
translate_month();
/*put the fourth component which is the
month*/
strcat(sendline,date.month);
/*put the fifth component which is the
year*/
strcat(sendline,date.year);
strcat(sendline, " ");
}
void error (char* str){
printf("%s",str);
exit(1);
}
/*
it simply translates the name of the day*/
void translate_day() {
if(strcmp(date.day_name, "Mon")==0) strcpy(date.day_name,"Lundi
");
else
if(strcmp(date.day_name, "Tue")==0) strcpy(date.day_name,"Mardi ");
else
if(strcmp(date.day_name, "Wed")==0) strcpy(date.day_name,"Mercredi ");
else
if(strcmp(date.day_name, "Th")==0) strcpy(date.day_name,"Jeudi ");
else
if(strcmp(date.day_name, "Fri")==0) strcpy(date.day_name,"Vendredi ");
else
if(strcmp(date.day_name, "Sat")==0) strcpy(date.day_name,"Samedi ");
else
if(strcmp(date.day_name, "Sun")==0) strcpy(date.day_name,"Dimanche ");
}
/*it simply translates the month*/
void translate_month(){
if(strcmp(date.month, "Jan")==0) strcpy(date.month,"Janvier
");
else
if(strcmp(date.month, "Feb")==0) strcpy(date.month,"February ");
else
if(strcmp(date.month, "Mar")==0) strcpy(date.month,"Mars ");
else
if(strcmp(date.month, "Apr")==0) strcpy(date.month,"Avril ");
else
if(strcmp(date.month, "Jun")==0) strcpy(date.month,"Juin ");
else
if(strcmp(date.month, "Jul")==0) strcpy(date.month,"Juillet ");
else
if(strcmp(date.month, "Aug")==0) strcpy(date.month,"August ");
else if(strcmp(date.month,
"Sep")==0)
strcpy(date.month,"Septembre ");
else
if(strcmp(date.month, "Oct")==0) strcpy(date.month,"Octobre ");
else
if(strcmp(date.month, "Nov")==0) strcpy(date.month,"Novenbre ");
else
if(strcmp(date.month, "Dec")==0) strcpy(date.month,"Decembre ");
else
if(strcmp(date.month, "Jul")==0) strcpy(date.month,"Juillet ");
else
if(strcmp(date.month, "Aug")==0) strcpy(date.month,"August ");
}