Mam taki kodzik który uruchamia serwer, w razie kontaktu z przeglądarką pobiera dane z klawiatury i odsyła stronę - ale 2 razy się uruchamia przy jednym wczytaniu strony
#include <winsock2.h>
#include <stdio.h>
#include <string.h>
#include <windows.h>
#pragma comment(lib, "ws2_32.lib")
// Define the structure
struct res {
const char* res1;
char tmp[200];
const char* res3;
};
// Create an instance of the structure
struct res tmp_res = {
"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<html><body>",
"", // Initialize tmp as an empty string
"</body></html>"
};
char httpResponse[2000];
int dl = 0;
// ...
void load() {
// Clear httpResponse array
memset(httpResponse, 0, sizeof(httpResponse));
dl=0;
// Copy res1 to httpResponse
strcpy(httpResponse + dl, tmp_res.res1);
dl += strlen(tmp_res.res1);
// Take input from the user for tmp_res.tmp
printf("Enter content for tmp_res.tmp: ");
scanf("%199s", tmp_res.tmp); // Limit input to 199 characters to leave space for null-terminator
// Copy tmp_res.tmp to httpResponse
strcpy(httpResponse + dl, tmp_res.tmp);
dl += strlen(tmp_res.tmp);
// Copy res3 to httpResponse
strcpy(httpResponse + dl, tmp_res.res3);
dl += strlen(tmp_res.res3);
// Add Connection: close header
strcat(httpResponse + dl, "Connection: close\r\n");
dl += strlen("Connection: close\r\n");
// Add Content-Length header
char contentLengthHeader[100];
sprintf(contentLengthHeader, "Content-Length: %d\r\n", (int)strlen(tmp_res.tmp));
strcpy(httpResponse + dl, contentLengthHeader);
dl += strlen(contentLengthHeader);
dl++; // Add one extra byte (for null-terminator)
}
// ...
int main() {
WSADATA wsaData;
// Initialize Winsock
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
fprintf(stderr, "Failed to initialize Winsock.\n");
return 1;
}
// Create socket
SOCKET serverSocket = socket(AF_INET, SOCK_STREAM, 0);
if (serverSocket == INVALID_SOCKET) {
fprintf(stderr, "Failed to create socket.\n");
WSACleanup();
return 1;
}
// Set up the server address structure
struct sockaddr_in serverAddr;
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr = INADDR_ANY;
serverAddr.sin_port = htons(8080); // Port 8080
// Bind the socket
if (bind(serverSocket, (struct sockaddr*)&serverAddr, sizeof(serverAddr)) == SOCKET_ERROR) {
fprintf(stderr, "Failed to bind socket.\n");
closesocket(serverSocket);
WSACleanup();
return 1;
}
int k=0;
do {
// Listen for incoming connections
if (listen(serverSocket, SOMAXCONN) == SOCKET_ERROR) {
fprintf(stderr, "Error listening on socket.\n");
closesocket(serverSocket);
WSACleanup();
return 1;
}
printf("Server listening on port 8080...\n");
// Accept incoming connections
SOCKET clientSocket = accept(serverSocket, NULL, NULL);
if (clientSocket == INVALID_SOCKET) {
fprintf(stderr, "Error accepting connection.\n");
closesocket(serverSocket);
WSACleanup();
return 1;
}
printf("Client connected.\n");
// Receive and print data
char buffer[1024];
memset(buffer, 0, sizeof(buffer));
int bytesRead = recv(clientSocket, buffer, sizeof(buffer) - 1, 0);
if (bytesRead > 50) {
// TO JEST WYKONYWANE 2 razy
buffer[bytesRead] = '\0';
// printf("Received data:\n%s\n---------\n----\n%d\n----\n", buffer,k);k++;
load();
send(clientSocket, httpResponse, strlen(httpResponse), 0);
}
// Cleanup for the current connection
closesocket(clientSocket);
printf("Client disconnected.\n");
} while (1);
// Cleanup (this part will never be reached in this example)
closesocket(serverSocket);
WSACleanup();
return 0;
}
I normalnie odczytuje pierwsze zapytanie do serwera, pobiera dane z klawiatury wyświetla je na stronie ale zaraz potem próbuje wczytać drugi raz
jak to zatrzymać, to wygląda jak by przeglądarka podwójne zapytania wysyłała