/* Client server Developed By Matan Tubul */ #include #include #include #include #include #include "brd.h" #include #include #include #include #include #include #include #include #include #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:"); if(msg_cnt.m_count == 0) printf("No messages was found for category %s\n",argv[1]); else { printf("%d Messages was found:\n\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); }