Created
January 22, 2015 09:53
-
-
Save MatanTubul/323376d0d21ca2e05ade to your computer and use it in GitHub Desktop.
server+client+category(working)+date
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Client server Developed By Matan Tubul | |
*/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <sys/types.h> | |
#include <unistd.h> | |
#include <sys/wait.h> | |
#include "brd.h" | |
#include <sys/shm.h> | |
#include <fcntl.h> | |
#include <string.h> | |
#include <sys/socket.h> | |
#include <stdint.h> | |
#include <arpa/inet.h> | |
#include <sys/socket.h> | |
#include <linux/filter.h> | |
#include <netinet/in.h> | |
#define MYSERVER "127.0.0.1" | |
void failure (char *msg); | |
void printMsg(msg_ent_t s); | |
int main(int argc,char *argv[]) | |
{ | |
int sd,i; | |
struct sockaddr_in clientaddr; | |
msg_ok_t reply; | |
msg_cat_t msg_cat; | |
msg_cnt_t msg_cnt; | |
msg_ent_t msgFromSrvr; | |
if(argc < 2) | |
{ | |
printf("few arguments!\n"); | |
exit(EXIT_FAILURE); | |
} | |
if((sd = socket(AF_INET,SOCK_STREAM,0)) < 0) | |
failure("Client socket:"); | |
printf("Connecting to Server %s\n",MYSERVER); | |
// converitng the ip address and the struct into the correct type | |
clientaddr.sin_addr.s_addr = inet_addr("127.0.0.1"); | |
clientaddr.sin_family = AF_INET; | |
clientaddr.sin_port = htons(B_PORT_NUM); | |
if (inet_aton(MYSERVER , &clientaddr.sin_addr) == 0) | |
failure("inet_aton() failed\n"); | |
//connect to server | |
if((connect(sd, (struct sockaddr *)&clientaddr, sizeof(clientaddr))) < 0 ) | |
failure("Connect failed:"); | |
puts("Connected\n;"); | |
strcpy(msg_cat.m_cat,argv[1]); | |
msg_cat.m_type = B_TYPE_CAT; | |
//sending message of type msg_cat_t | |
if(send(sd,(char *) &msg_cat ,sizeof(msg_cat) , 0 ) < 0) | |
failure("Send failed"); | |
//reciving message from type msg_cnt_t | |
if(recv(sd,(char *)&msg_cnt,sizeof(msg_cnt),0) < 0) | |
failure("recv failed:"); | |
printf("%d\n",msg_cnt.m_count); | |
for(i=0;i<msg_cnt.m_count; i++) | |
{ | |
sleep(1); | |
if(recv(sd,(char *)&msgFromSrvr,sizeof(msgFromSrvr),0) < 0) | |
failure("recv failed:"); | |
printMsg(msgFromSrvr); | |
} | |
if (close(sd) < 0) | |
failure("close"); | |
} | |
void failure (char *msg) | |
{ | |
perror(msg); | |
exit(EXIT_FAILURE); | |
} | |
void printMsg(msg_ent_t s) | |
{ | |
printf("%d/%d/%d",s.m_entry.b_date.day,s.m_entry.b_date.month,s.m_entry.b_date.year); | |
printf(" "); | |
printf("%s",s.m_entry.b_category); | |
printf("%s\n",s.m_entry.b_text); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Client server Developed By Nir Bercovitz | |
*/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <sys/types.h> | |
#include <unistd.h> | |
#include <sys/wait.h> | |
#include "brd.h" | |
#include <sys/shm.h> | |
#include <fcntl.h> | |
#include <string.h> | |
#include <sys/socket.h> | |
#include <stdint.h> | |
#include <arpa/inet.h> | |
#include <sys/socket.h> | |
#include <linux/filter.h> | |
#include <netinet/in.h> | |
#define MYSERVER "127.0.0.1" | |
void failure (char *msg); | |
void printMsg(msg_ent_t s); | |
int main(int argc,char *argv[]) | |
{ | |
int sd,i; | |
struct sockaddr_in clientaddr; | |
msg_date_t msg_date; | |
msg_cnt_t msg_cnt; | |
msg_ent_t msgFromSrvr; | |
if(argc < 2) | |
{ | |
printf("few arguments!\n"); | |
exit(EXIT_FAILURE); | |
} | |
// open a socket | |
if((sd = socket(AF_INET,SOCK_STREAM,0)) < 0) | |
failure("Client socket:"); | |
printf("Connecting to Server %s\n",MYSERVER); | |
// converitng the ip address and the struct into the correct type | |
clientaddr.sin_addr.s_addr = inet_addr("127.0.0.1"); | |
clientaddr.sin_family = AF_INET; | |
clientaddr.sin_port = htons(B_PORT_NUM); | |
if (inet_aton(MYSERVER , &clientaddr.sin_addr) == 0) | |
failure("inet_aton() failed\n"); | |
//connect to server | |
if((connect(sd, (struct sockaddr *)&clientaddr, sizeof(clientaddr))) < 0 ) | |
failure("Connect failed:"); | |
puts("Connected\n;"); | |
//filling fields of msg type date | |
char * sptr = NULL; | |
msg_date.m_start.day = atoi(strtok_r(argv[1],"/\n",&sptr)); | |
msg_date.m_start.month = atoi(strtok_r(NULL,"/\n",&sptr)); | |
msg_date.m_start.year = atoi(strtok_r(NULL,"/\n",&sptr)); | |
msg_date.m_type = B_TYPE_DATE; | |
//sending message of type msg_date_t | |
if(send(sd,(char *) &msg_date ,sizeof(msg_date) , 0 ) < 0) | |
failure("Send failed"); | |
//reciving message from type msg_date_t | |
if(recv(sd,(char *)&msg_cnt,sizeof(msg_cnt),0) < 0) | |
failure("recv failed:"); | |
for (i = 0 ; i < msg_cnt.m_count; i++ ) | |
{ | |
if(recv(sd,(char *)&msgFromSrvr,sizeof(msgFromSrvr),0) < 0) | |
failure("recv failed:"); | |
printMsg(msgFromSrvr); | |
} | |
if (close(sd) < 0) | |
failure("close"); | |
} | |
void failure (char *msg) | |
{ | |
perror(msg); | |
exit(EXIT_FAILURE); | |
} | |
void printMsg(msg_ent_t s) | |
{ | |
printf("%d/%d/%d",s.m_entry.b_date.day,s.m_entry.b_date.month,s.m_entry.b_date.year); | |
printf(" "); | |
printf("%s",s.m_entry.b_category); | |
printf("%s\n",s.m_entry.b_text); | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Client server Developed By Matan Tubul | |
*/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <sys/types.h> | |
#include <unistd.h> | |
#include <sys/wait.h> | |
#include "brd.h" | |
#include <sys/shm.h> | |
#include <fcntl.h> | |
#include <string.h> | |
#include <sys/socket.h> | |
#include <stdint.h> | |
#include <arpa/inet.h> | |
#include <sys/socket.h> | |
#include <linux/filter.h> | |
#include <netinet/in.h> | |
#define MYSERVER "127.0.0.1" | |
void printMsg(msg_ent_t s); | |
void failure (char *msg); | |
int main (int argc,char *argv[]) | |
{ | |
int boardMessagesFD, nread, lineInputIndex = 0,j,sd; | |
char date[B_DATE_LEN], tempText[2], temp[B_TEXT_LEN]; | |
struct sockaddr_in clientaddr; | |
board_ent_t boardEntry; // struct of board entry | |
msg_ent_t msgToSrvr;// struct of message entity | |
msg_ok_t reply;//struct of reply message | |
//open a txt file | |
if ((boardMessagesFD = open ("classified.txt", O_RDONLY, 0664)) == -1) | |
failure("Open"); | |
while(1) | |
{ | |
if ((nread = read (boardMessagesFD, date,B_DATE_LEN)) == -1) | |
failure("read_date_1"); | |
else if (nread == 0) //check if you got to the end of the input txt file | |
{ | |
lseek(boardMessagesFD, 0, SEEK_SET); //if so place the pointer to the file at the beginning and read again | |
if ((nread = read(boardMessagesFD, date, B_DATE_LEN)) == -1) | |
failure("read_date_2"); | |
} | |
date[B_DATE_LEN] = '\0'; | |
char * sptr = NULL; | |
boardEntry.b_date.day = atoi(strtok_r(date,"/\n",&sptr)); | |
boardEntry.b_date.month = atoi(strtok_r(NULL,"/\n",&sptr)); | |
boardEntry.b_date.year = atoi(strtok_r(NULL,"/\n",&sptr)); | |
if ((nread = read (boardMessagesFD, boardEntry.b_category ,B_CAT_LEN)) == -1) | |
failure("read_category"); | |
boardEntry.b_category [B_CAT_LEN] = '\0'; | |
strcpy(boardEntry.b_text,""); | |
// copy the free text into the struct text field. | |
lineInputIndex = 0; | |
j=0; | |
do{ | |
if(j > B_TEXT_LEN) | |
j=0; | |
if ((nread = read (boardMessagesFD, tempText ,1)) == -1) | |
failure("read_text"); | |
tempText[1] = '\0'; | |
lineInputIndex++; | |
temp[j] = tempText[0]; | |
j++; | |
} while ( strcmp(tempText,"\n") && nread !=0); | |
temp[j]='\0'; | |
strcpy(boardEntry.b_text,temp); | |
//put text into the right place | |
boardEntry.b_text[lineInputIndex] = '\0'; | |
// open a socket | |
if((sd = socket(AF_INET,SOCK_STREAM,0)) < 0) | |
failure("Client socket:"); | |
printf("Connecting to Server %s\n",MYSERVER); | |
// convert the details into the correct network | |
clientaddr.sin_addr.s_addr = inet_addr("127.0.0.1"); | |
clientaddr.sin_family = AF_INET; | |
clientaddr.sin_port = htons(B_PORT_NUM); | |
if (inet_aton(MYSERVER , &clientaddr.sin_addr) == 0) | |
failure("inet_aton() failed\n"); | |
//connect to the server | |
if((connect(sd, (struct sockaddr *)&clientaddr, sizeof(clientaddr))) < 0 ) | |
failure("Connect failed:"); | |
puts("Connected\n"); | |
// format msg_ent | |
msgToSrvr.m_entry = boardEntry; | |
msgToSrvr.m_type = B_TYPE_ENT; | |
//send | |
if(send(sd,(char *) &msgToSrvr ,sizeof(msgToSrvr) , 0 ) < 0) | |
failure("Send failed"); | |
printMsg(msgToSrvr); | |
//recv | |
if(recv(sd,(char *)&reply,sizeof(reply),0) < 0) | |
failure("recv failed:"); | |
printf("server answered : %d\n",reply.m_type); | |
if (close(sd) < 0) | |
failure("close"); | |
sleep(3); | |
}//while | |
}//main | |
void failure (char *msg) | |
{ | |
perror(msg); | |
exit(EXIT_FAILURE); | |
} | |
void printMsg(msg_ent_t s) | |
{ | |
printf("%d/%d/%d",s.m_entry.b_date.day,s.m_entry.b_date.month,s.m_entry.b_date.year); | |
printf(" "); | |
printf("%s",s.m_entry.b_category); | |
printf("%s\n",s.m_entry.b_text); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Client server Developed By Nir Bercovitz | |
*/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <sys/types.h> | |
#include <unistd.h> | |
#include <sys/wait.h> | |
#include "brd.h" | |
#include <sys/shm.h> | |
#include <fcntl.h> | |
#include <string.h> | |
#include <sys/socket.h> | |
#include <stdint.h> | |
#include <arpa/inet.h> | |
#include <sys/socket.h> | |
#include <linux/filter.h> | |
#include <netinet/in.h> | |
#define MAX_CONNECTIONS 5 | |
void failure (char *msg); | |
void printMsg(msg_ent_t s); | |
int countThisCategory(char * category, board_ent_t* mainBoard, board_ent_t* specificRequest); | |
int countThisDate(date_t date, board_ent_t* mainBoard, board_ent_t* specificRequest); | |
void copyEntryDetails(board_ent_t * to, board_ent_t * from); | |
board_ent_t specificRequest[B_ENT_CNT]; | |
int main (int argc,char *argv[]) | |
{ | |
int sock_fd, acpt_fd, messagesBoardIndex = 0, pid , i, typeOfMsg; | |
board_ent_t mainBoard[B_ENT_CNT]; | |
struct sockaddr_in serverAddr, senderAddr; | |
msg_ent_t msgEnt; //incoming msg type - post | |
msg_cat_t msgCat; //incoming msg type - category | |
msg_date_t msgDate; //incoming msg type - date | |
msg_ok_t rplyOK; //reply msg type - OK | |
msg_cnt_t rplyCNT; //reply msg type - count of a category in the board | |
msg_ent_t rplyCat; //reply msg type - specific category posts in the board | |
msg_ent_t rplyDate; //reply msg type - specific date posts in the board | |
msg_ent_t temp; | |
int slen = sizeof(senderAddr); | |
//socket | |
if((sock_fd = socket(AF_INET,SOCK_STREAM,0)) < 0) | |
failure("Server socket"); | |
//bind | |
serverAddr.sin_family = AF_INET; | |
serverAddr.sin_port = htons(B_PORT_NUM); | |
serverAddr.sin_addr.s_addr = INADDR_ANY; | |
if (bind(sock_fd , (struct sockaddr *) &serverAddr , sizeof(serverAddr) ) < 0) | |
failure("Bind failed"); | |
//listen | |
if (listen(sock_fd , MAX_CONNECTIONS ) < 0 ) | |
failure("Listen failed"); | |
while(1) { | |
//accept | |
if ((acpt_fd = accept(sock_fd, (struct sockaddr *) &senderAddr, &slen)) < 0) | |
failure("Accept failed"); | |
//peek | |
if ((recv(acpt_fd, (char *) &typeOfMsg, sizeof(typeOfMsg), MSG_PEEK)) < 0) | |
failure("Peek failed"); | |
if (typeOfMsg == B_TYPE_ENT) //check msg type | |
{ | |
//recv | |
if ((recv(acpt_fd, (char *) &msgEnt, sizeof(msgEnt),0 )) < 0)//get Msg and sender addr | |
failure("ReceiveMsg failed"); | |
if (messagesBoardIndex == B_ENT_CNT) //check if you got to the end of the board array before filling it | |
messagesBoardIndex = 0; //if so start filling the board array with messages from the beginning | |
//calculate data - fill the details in the right place in the array | |
copyEntryDetails(&mainBoard[messagesBoardIndex], &msgEnt.m_entry); | |
messagesBoardIndex++; | |
//send reply | |
rplyOK.m_type = B_TYPE_OK; | |
if ((sendto(acpt_fd, (char *) &rplyOK, sizeof(rplyOK), 0, (struct sockaddr *) &senderAddr, sizeof(senderAddr))) < 0) //send reply to sender | |
failure("SendOKto failed"); | |
}//if | |
else | |
{ | |
//create son | |
pid = fork(); | |
if (pid == 0) //this is our son | |
{ | |
close(sock_fd); | |
if (typeOfMsg == B_TYPE_CAT) //check msg type | |
{ | |
//recv | |
if ((recv(acpt_fd ,(char *) &msgCat ,sizeof(msgCat) ,0 )) < 0 )//get Msg and sender addr | |
failure("Receivecat failed"); | |
//calculate category | |
rplyCNT.m_type = B_TYPE_CNT ; | |
rplyCNT.m_count = countThisCategory(msgCat.m_cat,mainBoard,specificRequest); | |
//print which indicate the amount of message that should sent. | |
printf("%d\n",rplyCNT.m_count); | |
//send reply | |
if ((send(acpt_fd ,(char *) &rplyCNT ,sizeof(rplyCNT) ,0)) < 0 ) //send reply to sender | |
failure("SendCNTCat failed"); | |
//calculate | |
rplyCat.m_type = B_TYPE_ENT; | |
for(i=0;i<B_ENT_CNT;i++) | |
{ | |
if((strncmp(mainBoard[i].b_category,msgCat.m_cat,strlen(msgCat.m_cat)))==0) | |
{ | |
strcpy(rplyCat.m_entry.b_category,mainBoard[i].b_category); | |
strcpy(rplyCat.m_entry.b_text,mainBoard[i].b_text); | |
rplyCat.m_entry.b_date.day=mainBoard[i].b_date.day; | |
rplyCat.m_entry.b_date.month=mainBoard[i].b_date.month; | |
rplyCat.m_entry.b_date.year=mainBoard[i].b_date.year; | |
if ((send(acpt_fd ,(char *) &rplyCat ,sizeof(rplyCat) ,0)) < 0 ) //send reply to sender | |
failure("SendCaT failed"); | |
} | |
} | |
/* for (i = 0 ; i<rplyCNT.m_count ; i++) | |
{ | |
printf("%d/%d/%d", mainBoard[i].b_date.day, mainBoard[i].b_date.month, mainBoard[i].b_date.year); | |
//copyEntryDetails(&rplyCat.m_entry, &specificRequest[i]); | |
printf("%d/%d/%d",rplyCat.m_entry.b_date.day,rplyCat.m_entry.b_date.month,rplyCat.m_entry.b_date.year); | |
//send reply | |
if ((send(acpt_fd ,(char *) &rplyCat ,sizeof(rplyCat) ,0)) < 0 ) //send reply to sender | |
failure("SendCaT failed"); | |
sleep(2); | |
}//for*/ | |
exit(EXIT_SUCCESS); | |
}//if | |
else | |
{ | |
//recv | |
if ((recv(acpt_fd ,(char *) &msgDate ,sizeof(msgDate) ,0 ) ) < 0 )//get Msg and sender addr | |
failure("Receivedate failed"); | |
//calculate date | |
rplyCNT.m_type = B_TYPE_CNT; | |
rplyCNT.m_count = countThisDate(msgDate.m_start,mainBoard,specificRequest); | |
//send reply | |
if ((send(acpt_fd ,(char *) &rplyCNT ,sizeof(rplyCNT) ,0 )) < 0 ) //send reply to sender | |
failure("SendCNTDate failed"); | |
//calculate | |
rplyDate.m_type = B_TYPE_ENT; | |
for (i = 0 ; i<rplyCNT.m_count ; i++) | |
{ | |
copyEntryDetails(&rplyDate.m_entry, &specificRequest[i]); | |
//send reply | |
if ((send(acpt_fd ,(char *) &rplyDate ,sizeof(rplyDate) ,0) ) < 0 ) //send reply to sender | |
failure("SendDate failed"); | |
} | |
exit(EXIT_SUCCESS); | |
}//else | |
//abort();//son is out | |
} //if | |
else{ | |
close(acpt_fd); | |
} | |
}//else | |
} //while | |
//close | |
/*if (close(sock_fd) < 0) | |
failure("close recv socket failed"); | |
if (close(acpt_fd) < 0) | |
failure("close accept socket failed");*/ | |
}//main | |
void copyEntryDetails(board_ent_t * to, board_ent_t * from) | |
{ | |
to->b_date.day = from->b_date.day; //fill date | |
to->b_date.month = from->b_date.month; | |
to->b_date.year = from->b_date.year; | |
strcpy(to->b_category ,from->b_category); //fill categotry | |
strcpy(to->b_text ,from->b_text); //fill text | |
// printf("%s\n",to->b_text); | |
} | |
int countThisCategory(char * category, board_ent_t* mainBoard, board_ent_t* specificRequest) | |
{ | |
int i, countShows = 0; | |
for (i = 0 ; i<B_ENT_CNT ; i++) | |
if ( strncmp(category,mainBoard[i].b_category,strlen(category)) == 0) | |
{ | |
countShows++; | |
copyEntryDetails(&specificRequest[i], &mainBoard[i]);//saving (coping) the specific category entries for sending them later | |
} | |
return countShows; | |
} | |
int countThisDate(date_t date, board_ent_t* mainBoard, board_ent_t* specificRequest) | |
{ | |
int i, countShows = 0; | |
for (i = 0 ; i<B_ENT_CNT ; i++) | |
if ( (date.day == mainBoard[i].b_date.day) && (date.month == mainBoard[i].b_date.month) && (date.year == mainBoard[i].b_date.year) ) | |
{ | |
countShows++; | |
copyEntryDetails(&specificRequest[i], &mainBoard[i]);//saving (coping) the specific date entries for sending them later | |
} | |
return countShows; | |
} | |
void failure (char *msg) | |
{ | |
perror(msg); | |
exit(EXIT_FAILURE); | |
} | |
void printMsg(msg_ent_t s) | |
{ | |
printf("%d/%d/%d",s.m_entry.b_date.day,s.m_entry.b_date.month,s.m_entry.b_date.year); | |
printf(" "); | |
printf("%s",s.m_entry.b_category); | |
printf("%s\n",s.m_entry.b_text); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment