Click here to Skip to main content
15,938,578 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: how to add full path #include ? Pin
jschell12-Dec-23 11:06
jschell12-Dec-23 11:06 
AnswerRe: how to add full path #include ? Pin
Mircea Neacsu10-Dec-23 8:11
Mircea Neacsu10-Dec-23 8:11 
AnswerRe: how to add full path #include ? Pin
Richard MacCutchan10-Dec-23 21:48
mveRichard MacCutchan10-Dec-23 21:48 
Question[edit: most problems fixed, one problem remaining] Invalidate rectangle follow up, flickering and no movement Pin
Calin Negru9-Dec-23 23:10
Calin Negru9-Dec-23 23:10 
AnswerRe: [edit: most problems fixed, one problem remaining] Invalidate rectangle follow up, flickering and no movement Pin
Mircea Neacsu10-Dec-23 2:03
Mircea Neacsu10-Dec-23 2:03 
AnswerRe: [edit: most problems fixed, one problem remaining] Invalidate rectangle follow up, flickering and no movement Pin
Richard MacCutchan10-Dec-23 2:20
mveRichard MacCutchan10-Dec-23 2:20 
GeneralRe: [edit: most problems fixed, one problem remaining] Invalidate rectangle follow up, flickering and no movement Pin
Calin Negru10-Dec-23 4:48
Calin Negru10-Dec-23 4:48 
Questiondownload a web page Pin
mike74116-Dec-23 4:30
mike74116-Dec-23 4:30 
I created some code to download a web page:

#define _CRT_SECURE_NO_WARNINGS
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <iostream>

#pragma comment(lib, "ws2_32.lib")

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

    WSADATA wsaData;
    int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
    if (iResult != 0) {
        std::cerr << "WSAStartup failed: " << iResult << std::endl;
        return 1;
    }

    const char* hostname = "www.example.com";
    const char* path = "/";
    struct hostent* host = gethostbyname(hostname);
    struct sockaddr_in server_address;
    int socket_fd, bytes_received;
    char buffer[1024];

    if (host == NULL) {
        fprintf(stderr, "Error: Could not resolve hostname.\n");
        int error_num = WSAGetLastError();
        exit(1);
    }

    socket_fd = socket(AF_INET, SOCK_STREAM, 0);
    if (socket_fd < 0) {
        perror("Error: Could not create socket.\n");
        exit(1);
    }

    memset(&server_address, 0, sizeof(server_address));
    server_address.sin_family = AF_INET;
    server_address.sin_port = htons(80);
    memcpy(&server_address.sin_addr, host->h_addr_list[0], host->h_length);

    if (connect(socket_fd, (struct sockaddr*)&server_address, sizeof(server_address)) < 0) {
        perror("Error: Could not connect to server.\n");
        exit(1);
    }

    char* request = (char *)malloc(strlen(path) + strlen(hostname) + 16);
    sprintf(request, "GET %s HTTP/1.1\r\nHost: %s\r\n\r\n", path, hostname);
    send(socket_fd, request, strlen(request), 0);

    while ((bytes_received = recv(socket_fd, buffer, sizeof(buffer), 0)) > 0) {
        fwrite(buffer, 1, bytes_received, stdout);
    }

    free(request);
    closesocket(socket_fd);

    return 0;
}


It prints the web page out, but then it seems to get stuck in the recv function.

Anyone know what's wrong?

Thanks.
AnswerRe: download a web page Pin
Mircea Neacsu6-Dec-23 5:00
Mircea Neacsu6-Dec-23 5:00 
GeneralRe: download a web page Pin
mike74116-Dec-23 13:28
mike74116-Dec-23 13:28 
GeneralRe: download a web page Pin
Mircea Neacsu6-Dec-23 14:18
Mircea Neacsu6-Dec-23 14:18 
AnswerRe: download a web page Pin
jschell6-Dec-23 6:52
jschell6-Dec-23 6:52 
AnswerRe: download a web page Pin
Gerry Schmitz6-Dec-23 7:28
mveGerry Schmitz6-Dec-23 7:28 
Questioncopying a file in C++17 Pin
mike74115-Dec-23 21:36
mike74115-Dec-23 21:36 
AnswerRe: copying a file in C++17 Pin
Richard MacCutchan5-Dec-23 22:07
mveRichard MacCutchan5-Dec-23 22:07 
AnswerRe: copying a file in C++17 Pin
jschell6-Dec-23 7:04
jschell6-Dec-23 7:04 
Questionprintf without stdio.h Pin
mike74115-Dec-23 5:21
mike74115-Dec-23 5:21 
AnswerRe: printf without stdio.h Pin
Mircea Neacsu5-Dec-23 5:31
Mircea Neacsu5-Dec-23 5:31 
GeneralRe: printf without stdio.h Pin
mike74115-Dec-23 16:23
mike74115-Dec-23 16:23 
GeneralRe: printf without stdio.h Pin
Mircea Neacsu5-Dec-23 16:42
Mircea Neacsu5-Dec-23 16:42 
AnswerRe: printf without stdio.h Pin
Victor Nijegorodov5-Dec-23 5:32
Victor Nijegorodov5-Dec-23 5:32 
AnswerRe: printf without stdio.h Pin
k50545-Dec-23 6:02
mvek50545-Dec-23 6:02 
GeneralRe: printf without stdio.h Pin
jschell6-Dec-23 7:07
jschell6-Dec-23 7:07 
Questioncopying a file Pin
mike74115-Dec-23 3:24
mike74115-Dec-23 3:24 
AnswerRe: copying a file Pin
Mircea Neacsu5-Dec-23 3:32
Mircea Neacsu5-Dec-23 3:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.