I'm learning networking and sockets so I decided to test my skills by writing a client. It just hangs for some reason:
Any idea why? (I'm aware of the buffer overflow vuln this is just a quick and dirty program)
Code:
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <cstring>
#include <iostream>
const char* request = "GET http://www.imdb.com/index.html HTTP/1.0";
int main()
{
int sock = socket(AF_INET, SOCK_STREAM, 0);
try
{
if (sock<1)
throw "Failed to create a socket";
sockaddr_in session;
session.sin_port = htons(80);
session.sin_family = AF_INET;
session.sin_addr.s_addr = inet_addr("72.21.206.80");
if (connect(sock, (sockaddr*)&session, sizeof(session)) < 0)
throw "Failed to establish a connection";
send(sock, request, strlen(request), 0);
char buf[1000];
recv(sock, buf, 1000, 0);
for (int i{}; i<1000; ++i)
std::cout << buf[i];
}
catch (const char* exc)
{
std::cerr << "An error occured: " << exc << std::endl;
close(sock);
}
}
Last edited by StackMasher,