cheekykevwalker
Programmer
Not being very familiar with C I am doing a little Network Programming and have hit a stumbling block.The actual problem is to do with C itself and not the network side of things. I have passed over a string variable 'buffer' to the server in a simple client/server setup containing the last day/month/dateInMonth/Time(HRS:MINS:SECS)/Year a person logged in read from a text file and separated by whitespaces. However I need this in time format as when it is over at the server I need to do a comparison with a previous date in order to check what to send back to the client. I'm also unsure of how to do date comparison in C. I'm not expecting full answers but any ideas would be greatly appreciated. This code has been adapted by myself from basic client/server material that is available on the net and is for my own personal information.I can post the SERVER code if needed. Cheers.
Code:
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <time.h>
#include <string.h>
void error(char *msg)
{
perror(msg);
exit(0);
}
int main(int argc, char *argv[])
{
int sockfd, portno, n;
struct sockaddr_in serv_addr;
struct hostent *server;
//character buffer
char buffer[256];
int user_choice;
//Date
char day[5],month [10],hoursMinSecs[10],date[10], year[10];
FILE *lastlogin = fopen("LastLogin.txt","r");
char message[]= "All Headlines";
char logDate[50];
//If the number of arguments passed to the program is less than 3 then prompt for correct
//amount of arguments and exit the program.
if (argc < 3) {
fprintf(stderr,"usage %s hostname port\n", argv[0]);
exit(0);
}
//Integer portno is equal to the 3 argument passed in at the command line
portno = atoi(argv[2]);
//Socket call returns -1 if there is an error
sockfd = socket(AF_INET, SOCK_STREAM, 0);
//If the sockfd file descriptor is less then 1 then return error message
if (sockfd < 0)
error("ERROR opening socket");
//get the host name from the second command line argument
server = gethostbyname(argv[1]);
//Get new login time
//Define 'now'. time_t is probably a typedef Calender time is the number of
//seconds since 1/1/1970 */
time_t now;
//Get the system time and put it into 'now' as 'calender time' */
now = time((time_t *)NULL);
//check it is a valid host, if not then print message to screen and exit program
if (server == NULL) {
fprintf(stderr,"ERROR, no such host\n");
exit(0);
}
//find out what this does
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr,
(char *)&serv_addr.sin_addr.s_addr,
server->h_length);
serv_addr.sin_port = htons(portno);
//try to connect or print error message if can't connect
if (connect(sockfd,&serv_addr,sizeof(serv_addr)) < 0)
error("ERROR connecting");
//Get the last login time
fscanf(lastlogin, "%s%s%s%s%s", &day,&month,&date,&hoursMinSecs,&year);
fclose(lastlogin);
FILE *writeLogin = fopen("LastLogin.txt", "w");
printf("The last login was: %s %s %s %s %s", day,month,date,hoursMinSecs,year);
fprintf(writeLogin, "%s", ctime(&now));
fclose(writeLogin);
//Prompt for message to sent
//Format data in 'now' NOTE that 'ctime' inserts a '\n' */
printf("\nThe Present Login Date/Time: %s", ctime(&now));
printf("----------------------MENU-----------------\n\n");
printf("1.View all the headlines.\n\n");
printf("2.View the headlines since I last logged in.\n\n");
printf(" Please Enter choice (1 or 2):");
scanf("%d", &user_choice);
if (user_choice == 1)
{
printf("You have chosen to view all of the headlines.\n");
//write message to socket if the write call returns -1 then print error message
n = write(sockfd,"ALL");
if (n < 0)
error("ERROR writing to socket");
}
if (user_choice == 2)
{
printf("You have chosen to view all of the headlines since you last logged in.\n");
//write message to socket if the write call returns -1 then print error message
strcpy(logDate,day);
strcat(logDate," ");
strcat(logDate,month);
strcat(logDate," ");
strcat(logDate,date);
strcat(logDate," ");
strcat(logDate,hoursMinSecs);
strcat(logDate," ");
strcat(logDate,year);
n = write(sockfd,logDate);
if (n < 0)
error("ERROR writing to socket");
}
//clear the buffer
bzero(buffer,256);
//read message from server into the buffer
n = read(sockfd,buffer,255);
//if there is an error print msg
if (n < 0)
error("ERROR reading from socket");
//print the contents of the buffer
printf("%s\n",buffer);
return 0;
}