Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <netinet/in.h>
#include <sys/wait.h>
#include<time.h>
#include<sys/time.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
char pass[100];
char pass1[100];
int main()
{
int sockfd, new_fd, numbytes;
struct addrinfo hints, *servinfo, *p;
struct sockaddr_storage their_addr;
socklen_t sin_size;
socklen_t *size;
char opr[2];
FILE *f, *t;
time_t ti;
struct sockaddr *names;
struct timeval tv;
struct timezone tz;
char *times;
int yes = 1, len = 0;
struct sigaction sa;
char s[INET6_ADDRSTRLEN], s1[INET6_ADDRSTRLEN];
char name[20];
int rv;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
if ((rv = getaddrinfo(NULL, "2001", &hints, &servinfo)) == -1) {
fprintf(stderr, "getaddrinfo:%s", gai_strerror(rv));
return 1;
}
printf("\n \n getaddrinfo:%d", rv);
printf("\n------------------------------");
for (p = servinfo; p != NULL; p = p->ai_next) {
if ((sockfd =
socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
perror("server:socket");
continue;
}
printf("\n server socket launched...");
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int))
== -1) {
perror("setsockopt");
return 0;
}
printf("\n server setsocket option ...");
if (bind(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
close(sockfd);
perror("server:bind");
continue;
}
printf("\n socket binded....");
break;
}
if (p == NULL) {
fprintf(stderr, "server:failed");
return 2;
}
if (listen(sockfd, 10) == -1) {
perror("listen");
return 0;
}
//sa.sa_handler=sigchld_handler;
//sa.sa_flags=SA_RESTART;
/*if(sigaction(SIGCHLD,&sa,NULL)==-1)
{
perror("sigaction");
exit(0);
}*/
printf("\n\nlisten to socket :%d", sockfd);
sin_size = sizeof their_addr;
new_fd = accept(sockfd, (struct sockaddr *) &their_addr, &sin_size);
printf("\n sockfd=%d new_fd=%d", sockfd, new_fd);
if (new_fd == -1) {
perror("accept");
}
//inet_ntop(their_addr.ss_family,get_in_addr((struct sockaddr *)&their_addr),s,sizeof s);
//printf("\n\nserver:connected %s",s);
int count = 0;
if ((send(new_fd, "start", 5, 0)) == -1)
perror("ww");
printf("\n started operation");
printf("\n The operation is ");
if ((recv(new_fd, pass, sizeof pass, 0)) == -1)
perror("r");
printf("%s", pass);
if ((strcmp(pass, "1")) == 0) {
int i;
printf("\n hai");
f = fopen("file.txt", "a");
if ((recv(new_fd, pass1, sizeof pass1, 0)) == -1)
perror("err");
if ((i = gettimeofday(&tv, &tz)) == -1)
perror("y");
ti = tv.tv_sec;
times = ctime(&ti);
fprintf(f, "%s %s", pass1, times);
fclose(f);
} else {
char op[2];
char i;
printf("\n hello");
f = fopen("file.txt", "r");
while (fgets(pass, 100, f) != NULL)
count++;
op[0] = count + 48;
op[1] = '\0';
printf("no of lines:%s", op);
if ((send(new_fd, op, sizeof op, 0)) == -1)
perror("re");
fseek(f, 0, SEEK_SET);
count = 0;
while (fgets(pass, 100, f) != NULL) {
if ((send(new_fd, pass, 100, 0)) == -1)
perror("l");
}
fclose(f);
}
close(new_fd);
close(new_fd);
return 0;
}