Question

In this activity, you will complete the RSS Client that connects to a UNL RSS feed,...

In this activity, you will complete the RSS Client that connects to a UNL RSS feed, processes the XML data and outputs the results to the standard output. Most of the client has been completed for you. You just Lab Handout: Structures 4 need to complete the design and implementation of a C structure that models the essential parts of an RSS item. Your structure will need to support an RSS item’s title, link, description, and publication date. As a first attempt you can treat the publication date as a string, but ultimately you should use a tm structure similar to the Student example. This structure has several fields that give you access to the year, month, date, hour, minute, etc. For full documentation, you can see the following: http://pubs.opengroup.org/onlinepubs/7908799/xsh/time.h.html Instructions

1. Uncomment the hostname and resource in the main function for the RSS feed that you want to pull from (you can change this later by commenting/uncommenting).

2. Design and implement the RSS structure.

3. Complete the other functions • createEmptyRSS – this function should create an “empty” RSS structure • createRSS – This function should construct an RSS structure based on the input parameters. Hint: to parse the typical RSS date/time stamp (the pubDate), you can use the following formatting: strptime(date, "%a, %d %b %Y %H:%M:%S ", &(rss->XXX)); where XXX is replaced by whatever you name your publication date field . • printRSS – This function should print the given RSS to the standard output in a human readable format (the details are up to you).

4. You will need to use the makefile to compile your program as it contains the flags necessary to import the proper XML libraries. Recall that you can execute the makefile by typing the “make” command

***************************************************************************************************

#include <stdio.h>

#include <stdlib.h>

#include <sys/types.h>

#include <sys/socket.h>

#include <netinet/in.h>

#include <netdb.h>

#include <string.h>

#include <time.h>

#include <libxml/parser.h>

#include <libxml/tree.h>

#define HTTP_PORT 80

#define DIRSIZE 100000

typedef struct {

//TODO: define your structure here

} RSS;

/* Implement these functions */

RSS * createEmptyRSS();

RSS * createRSS(char * title, char * link, char * date, char * description);

void printRSS(RSS * item);

/* these functions are done for you, do not change them */

void parseRSS_XML(xmlNode * root_node);

char * getRSS_XML(const char * hostname, const char * resource);

int main(int argc, char **argv) {

//TODO: uncomment the RSS feed that you wish to use

/*

//UNL In the News Channel:

char hostname[] = "ucommxsrv1.unl.edu";

char resource[] = "/rssfeeds/unlinthenewsrss.xml";

*/

/*

//Nebraska Headline News - Husker Sports

char hostname[] = "www.huskers.com";

char resource[] = "/rss.dbml?db_oem_id=100&media=news";

*/

/*

//The scarlet - The news source for faculty and staff at the University of Nebraska-Lincoln

char hostname[] = "scarlet.unl.edu";

char resource[] = "/?feed=rss2";

*/

char *rawXML = getRSS_XML(hostname, resource);

xmlDocPtr doc = xmlReadMemory(rawXML, strlen(rawXML), "noname.xml", NULL, 0);

xmlNode *root_element = xmlDocGetRootElement(doc);

parseRSS_XML(root_element);

xmlCleanupParser();

}

RSS * createEmptyRSS() {

//TODO: implement

}

RSS * createRSS(char * title, char * link, char * date, char * description) {

//TODO: implement

}

void printRSS(RSS * item) {

//TODO: implement

}

void parseRSS_XML(xmlNode * root_node) {

//we limit the number of items to a maximum of 100

int n=100;

int i=0, numItems=0;

RSS items[100];

xmlNode *cur_node = NULL;

xmlNode *inner_node = NULL;

xmlNode *channel = root_node->children->next;

  

for(cur_node=channel->children; cur_node; cur_node = cur_node->next) {

if (cur_node->type == XML_ELEMENT_NODE && strcmp(cur_node->name, "item") == 0) {

RSS * anRSS = NULL;

char * title = NULL;

char * link = NULL;

char * date = NULL;

char * description = NULL;

for(inner_node=cur_node->children; inner_node; inner_node = inner_node->next) {

if(inner_node->type == XML_ELEMENT_NODE) {

      if(strcmp(inner_node->name, "title") == 0) {

      title = xmlNodeGetContent(inner_node);

      } else if (strcmp(inner_node->name, "link") == 0) {

      link = xmlNodeGetContent(inner_node);

      } else if (strcmp(inner_node->name, "description") == 0) {

      description = xmlNodeGetContent(inner_node);

      } else if (strcmp(inner_node->name, "pubDate") == 0) {

      date = xmlNodeGetContent(inner_node);

}

}

}

anRSS = createRSS(title, link, date, description);

if(numItems < n) {

   items[numItems] = *anRSS;

   i++;

numItems++;

}

}

}

for(i=0; i<numItems; i++) {

printRSS(&items[i]);

}

}

char * getRSS_XML(const char * hostname, const char * resource){

char *rawXML;

char dir[DIRSIZE];

char result[DIRSIZE * 2];

char * request = (char *) malloc(sizeof(char) * (200 + strlen(hostname) + strlen(resource)));

sprintf(request, "GET %s HTTP/1.1 \r\nHost: %s\r\n\r\n", resource, hostname);

int sd;

struct sockaddr_in sin;

struct sockaddr_in pin;

struct hostent *hp;

/* go find out about the desired host machine */

if ((hp = gethostbyname(hostname)) == 0) {

perror("gethostbyname");

exit(1);

}

  

/* fill in the socket structure with host information */

memset(&pin, 0, sizeof(pin));

pin.sin_family = AF_INET;

pin.sin_addr.s_addr = ((struct in_addr *)(hp->h_addr))->s_addr;

pin.sin_port = htons(HTTP_PORT);

  

/* grab an Internet domain socket */

if ((sd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {

perror("socket");

exit(1);

}

  

/* connect to PORT on HOST */

if (connect(sd,(struct sockaddr *) &pin, sizeof(pin)) == -1) {

perror("connect");

exit(1);

}

  

/* send a message to the server PORT on machine HOST */

if (send(sd, request, strlen(request), 0) == -1) {

perror("send");

exit(1);

}

int len = 0;

while(1) {

len = recv(sd, dir, DIRSIZE, 0);

strncat(result, dir, len);

if(len <= 0)

break;

}

rawXML = (char *) malloc(sizeof(char) * (strlen(strchr(result, '<')) + 1));

strcpy(rawXML, strchr(result, '<'));

close(sd);

return rawXML;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <stdio.h>

#include <stdlib.h>

#include <sys/types.h>

#include <sys/socket.h>

#include <netinet/in.h>

#include <netdb.h>

#include <string.h>

#include <time.h>

#include <libxml/parser.h>

#include <libxml/tree.h>

#define HTTP_PORT 80

#define DIRSIZE 100000

typedef struct {

//TODO: define your structure here

} RSS;

/* Implement these functions */

RSS * createEmptyRSS();

RSS * createRSS(char * title, char * link, char * date, char * description);

void printRSS(RSS * item);

/* these functions are done for you, do not change them */

void parseRSS_XML(xmlNode * root_node);

char * getRSS_XML(const char * hostname, const char * resource);

int main(int argc, char **argv) {

//TODO: uncomment the RSS feed that you wish to use

/*

//UNL In the News Channel:

char hostname[] = "ucommxsrv1.unl.edu";

char resource[] = "/rssfeeds/unlinthenewsrss.xml";

*/

/*

//Nebraska Headline News - Husker Sports

char hostname[] = "www.huskers.com";

char resource[] = "/rss.dbml?db_oem_id=100&media=news";

*/

/*

//The scarlet - The news source for faculty and staff at the University of Nebraska-Lincoln

char hostname[] = "scarlet.unl.edu";

char resource[] = "/?feed=rss2";

*/

char *rawXML = getRSS_XML(hostname, resource);

xmlDocPtr doc = xmlReadMemory(rawXML, strlen(rawXML), "noname.xml", NULL, 0);

xmlNode *root_element = xmlDocGetRootElement(doc);

parseRSS_XML(root_element);

xmlCleanupParser();

}

RSS * createEmptyRSS() {

//TODO: implement

}

RSS * createRSS(char * title, char * link, char * date, char * description) {

//TODO: implement

}

void printRSS(RSS * item) {

//TODO: implement

}

void parseRSS_XML(xmlNode * root_node) {

//we limit the number of items to a maximum of 100

int n=100;

int i=0, numItems=0;

RSS items[100];

xmlNode *cur_node = NULL;

xmlNode *inner_node = NULL;

xmlNode *channel = root_node->children->next;

  

for(cur_node=channel->children; cur_node; cur_node = cur_node->next) {

if (cur_node->type == XML_ELEMENT_NODE && strcmp(cur_node->name, "item") == 0) {

RSS * anRSS = NULL;

char * title = NULL;

char * link = NULL;

char * date = NULL;

char * description = NULL;

for(inner_node=cur_node->children; inner_node; inner_node = inner_node->next) {

if(inner_node->type == XML_ELEMENT_NODE) {

      if(strcmp(inner_node->name, "title") == 0) {

      title = xmlNodeGetContent(inner_node);

      } else if (strcmp(inner_node->name, "link") == 0) {

      link = xmlNodeGetContent(inner_node);

      } else if (strcmp(inner_node->name, "description") == 0) {

      description = xmlNodeGetContent(inner_node);

      } else if (strcmp(inner_node->name, "pubDate") == 0) {

      date = xmlNodeGetContent(inner_node);

}

}

}

anRSS = createRSS(title, link, date, description);

if(numItems < n) {

   items[numItems] = *anRSS;

   i++;

numItems++;

}

}

}

for(i=0; i<numItems; i++) {

printRSS(&items[i]);

}

}

char * getRSS_XML(const char * hostname, const char * resource){

char *rawXML;

char dir[DIRSIZE];

char result[DIRSIZE * 2];

char * request = (char *) malloc(sizeof(char) * (200 + strlen(hostname) + strlen(resource)));

sprintf(request, "GET %s HTTP/1.1 \r\nHost: %s\r\n\r\n", resource, hostname);

int sd;

struct sockaddr_in sin;

struct sockaddr_in pin;

struct hostent *hp;

/* go find out about the desired host machine */

if ((hp = gethostbyname(hostname)) == 0) {

perror("gethostbyname");

exit(1);

}

  

/* fill in the socket structure with host information */

memset(&pin, 0, sizeof(pin));

pin.sin_family = AF_INET;

pin.sin_addr.s_addr = ((struct in_addr *)(hp->h_addr))->s_addr;

pin.sin_port = htons(HTTP_PORT);

  

/* grab an Internet domain socket */

if ((sd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {

perror("socket");

exit(1);

}

  

/* connect to PORT on HOST */

if (connect(sd,(struct sockaddr *) &pin, sizeof(pin)) == -1) {

perror("connect");

exit(1);

}

  

/* send a message to the server PORT on machine HOST */

if (send(sd, request, strlen(request), 0) == -1) {

perror("send");

exit(1);

}

int len = 0;

while(1) {

len = recv(sd, dir, DIRSIZE, 0);

strncat(result, dir, len);

if(len <= 0)

break;

}

rawXML = (char *) malloc(sizeof(char) * (strlen(strchr(result, '<')) + 1));

strcpy(rawXML, strchr(result, '<'));

close(sd);

return rawXML;

}

Add a comment
Know the answer?
Add Answer to:
In this activity, you will complete the RSS Client that connects to a UNL RSS feed,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Modify the client server system program given below so that instead of sendto() and recvfrom(), you...

    Modify the client server system program given below so that instead of sendto() and recvfrom(), you use connect() and un-addresssed write() and read() calls. //Server.c #include #include #include #include #include #include #include #include #include #include # define PortNo 4567 # define BUFFER 1024 int main(int argc, char ** argv) { int ssd; int n; socklen_t len; char msg[BUFFER]; char clientmsg[BUFFER]; struct sockaddr_in server; struct sockaddr_in client; int max_iterations = 0; int count = 0, totalChar = 0, i = 0;...

  • Assume I don't understand C++ Can someone explain this program to me Line by Line? Basically...

    Assume I don't understand C++ Can someone explain this program to me Line by Line? Basically what each line actually does? whats the function? whats the point? Don't tell me what the program does as a whole, I need to understand what each line does in this program. #include #include #include #include #include #define SERVER_PORT 5432 #define MAX_LINE 256 int main(int argc, char * argv[]) {    FILE *fp;    struct hostent *hp;    struct sockaddr_in sin;    char *host;...

  • /* myloggerd.c * Source file for thread-lab * Creates a server to log messages sent from...

    /* myloggerd.c * Source file for thread-lab * Creates a server to log messages sent from various connections * in real time. * * Student: */ #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> #include <fcntl.h> #include <sys/socket.h> #include <sys/un.h> #include <stdlib.h> #include <pthread.h> #include "message-lib.h" // forward declarations int usage( char name[] ); // a function to be executed by each thread void * recv_log_msgs( void * arg ); // globals int log_fd; // opened by main() but accessible...

  • IN UNIX, MODIFY CODE, PROVIDE SCREENSHOTS FOR GOOD RATING: T1. Modify Client.c program to accept two...

    IN UNIX, MODIFY CODE, PROVIDE SCREENSHOTS FOR GOOD RATING: T1. Modify Client.c program to accept two arguments (IP add & port no. of the concurrent Server with thread - conServThread.c). Similarly, modify the Server (conServThread.c) program to accept an argument which is the port number of the server to bind and listen to. Try these two updated programs (server and client) with a port number (e.g., hhmm6) with current time where hh is hours in 24-hour format and mm is...

  • For Unix in a C/C++ environment echoServer and echoClient are provided below In this lab, we will...

    For Unix in a C/C++ environment echoServer and echoClient are provided below In this lab, we will modifiy echoServer.c and echoClient.c programs (for the server's port# not fixed). (1) Modify echoServer.c program to take one argument (a port number) to be used for its listening port when it starts. (2) Modify echoClient.c program to take two arguments (server's IP address and Port number) to be used for its connection. (3) Find a port free for the server using netstat (see...

  • C programming help! /* Your challenge is to format the following code in a readable manner, anwsering the questions in...

    C programming help! /* Your challenge is to format the following code in a readable manner, anwsering the questions in the comments. * */ #include <stdio.h> #include <stdlib.h> #include <sys/time.h> #include <time.h> #include <sys/socket.h> #include <arpa/inet.h> #include <sys/types.h> #include <sys/uio.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <strings.h> #define BUFFERT 512 #define BACKLOG 1 int create_server_socket (int port); struct sockaddr_in sock_serv,sock_clt; int main(int argc, char** argv){ int sfd, fd; unsigned int length = sizeof(struct sockaddr_in); long int n, m, count...

  • Run the code in Linux and provide the screenshot of the output and input #include <signal.h>...

    Run the code in Linux and provide the screenshot of the output and input #include <signal.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <arpa/inet.h> #include <sys/types.h> #include <sys/socket.h> static void cleanup(); static void docleanup(int signum); static const char *SERVER_ADDR = "127.0.0.1"; static const int SERVER_PORT = 61234; static int cfd = -1; int main(int argc, char *argv[]) { struct sockaddr_in saddr; char buf[128]; int bufsize = 128, bytesread; struct sigaction sigact; printf("client starts running ...\n"); atexit(cleanup); sigact.sa_handler =...

  • Implement a program that: reads a number of personal records (for example, using PERSON struct from...

    Implement a program that: reads a number of personal records (for example, using PERSON struct from the earlier lab) from the standard input, creates a database of personal records, allows for adding new entries to the database, allows for deleting entries from the database, includes functions to acquire a record of personal data, and includes functions to display (print) a single selected record from the database, and also allows for printing all records in the database. NOTES: if name is...

  • I am having problems with the following assignment. It is done in the c language. The...

    I am having problems with the following assignment. It is done in the c language. The code is not reading the a.txt file. The instructions are in the picture below and so is my code. It should read the a.txt file and print. The red car hit the blue car and name how many times those words appeared. Can i please get some help. Thank you. MY CODE: #include <stdio.h> #include <stdlib.h> #include <string.h> struct node { char *str; int...

  • 3. (8 pts) You need to understand how to define and use a struct for this exercise. You should be able to figure out the...

    3. (8 pts) You need to understand how to define and use a struct for this exercise. You should be able to figure out the answer without actually compiling or running the program. Which is true about the following codes? If you choose a, you need to specify where the syntax error(s) occur; if you choose b, you need to specify what error occurs when you run it; if you choose c, you need to specify the outputs of the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT