привет, пытаюсь написать простейший веб сервер каждою секунду отсылающий время на клиент
но он ен работает( что я сделал не так
#include <winsock2.h>
#include <ws2tcpip.h>
#include <iostream>
#include <string>
#include <ctime>
#pragma comment(lib, "ws2_32.lib")
int main() {
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
std::cerr << "Failed to initialize winsock" << std::endl;
return -1;
}
SOCKET ListenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (ListenSocket == INVALID_SOCKET) {
std::cerr << "Failed to create socket" << std::endl;
return -1;
}
sockaddr_in serverService;
serverService.sin_family = AF_INET;
// Use inet_pton instead of inet_addr
if (inet_pton(AF_INET, "127.0.0.1", &(serverService.sin_addr)) <= 0) {
std::cerr << "Failed to set address" << std::endl;
return -1;
}
serverService.sin_port = htons(8080);
if (bind(ListenSocket, (SOCKADDR*)&serverService, sizeof(serverService)) == SOCKET_ERROR) {
std::cerr << "Failed to bind socket" << std::endl;
return -1;
}
if (listen(ListenSocket, SOMAXCONN) == SOCKET_ERROR) {
std::cerr << "Failed to start listening on socket" << std::endl;
return -1;
}
while (true) {
SOCKET ClientSocket = accept(ListenSocket, NULL, NULL);
if (ClientSocket == INVALID_SOCKET) {
std::cerr << "Failed to accept connection" << std::endl;
return -1;
}
char buffer[256] = { 0 };
std::time_t now = std::time(0);
struct tm timeinfo;
localtime_s(&timeinfo, &now);
std::strftime(buffer, sizeof(buffer), "%Y-%m-%dT%H:%M:%SZ", &timeinfo);
std::string message = "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\r\n{ \"time\": \"" + std::string(buffer) + "\" }\n";
send(ClientSocket, message.c_str(), message.size(), 0);
closesocket(ClientSocket);
}
closesocket(ListenSocket);
WSACleanup();
return 0;
}
когда пытаюсь зайти с браузера chrome
localhost:8080 This site can't be reached