I need to write a program (using Dev-C++) which can count the number of hyperlinks of a given web page. The main objective is to practice socket programming and try to use HTTP protocol.

I am not sure exactly how to go about doing this. Can anyone help me figure out how to do this? References or suggestions or anything that would point me in the right direction would really help.
(This is for my networking class)

Requirements:
a. Input: The users enter the URL of the desired web page without “
b. Output: Print all hyperlinks and the number of hyperlinks. Note that only <a href=”xxx”> should be counted while <link href=”xxx”> shouldn’t.

Dani AI

Generated

Good call by to start with raw socket work. A focused, reliable plan for this assignment is: open a TCP socket to port 80, send a plain HTTP GET (HTTP/1.0 or HTTP/1.1 with Connection: close), read the full response until the socket closes, separate headers from the body at \r\n\r\n, then extract only <a href="..."> occurrences from the body. Using HTTP/1.0 keeps the server behavior simple for a class project and avoids having to implement chunked decoding.

A minimal raw request (replace host/path) looks like:

GET /path/to/page.html HTTP/1.0\r\n
Host: example.com\r\n
\r\n

After the body is available, remove <script> and <style> blocks first (these commonly contain href-like text), then use a case-insensitive pattern to find anchors with quoted href values. A compact C++ example using std::regex:

std::regex re(R"(<a\s+[^>]*href\s*=\s*(['"])(.*?)\1)", std::regex::icase);
for (std::sregex_iterator it(body.begin(), body.end(), re), end; it != end; ++it) {
  std::string href = (*it)[2].str();
  // ignore hrefs starting with "mailto:", "javascript:", or "#"
  // convert relative URLs to absolute using the page base
  // collect/count unique/total as needed
}

Caveats and troubleshooting: handle 3xx redirects by reading the Location header and following up to a small limit; HTTPS requires TLS (not plain sockets) and is out of scope for a simple socket exercise; many real-world pages break naive regex parsing, so consider an HTML parser (Gumbo/libxml2) for a robust tool. Also be careful with Dev-C++/MinGW and old std::regex implementations—if regex fails, a simple state-machine scanner that finds <a then extracts the following href= is a reliable fallback.

Recommended Answers

All 2 Replies

Have you read Beej's guide to socket programming?

No, i haven't read it but i will read it and see if it helps. Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.