Hi @ShiJieLi
This was translated by GPT but looks like:
int clientNumber = 0;
unsigned __stdcall HandleClient(void* clientSocketPtr) {
int headerLength = 0;
int receivedBytes = 0;
clientNumber++;
// printf("\n=========== Client Number ====%d\n\n", clientNumber);
SOCKET clientSocket = *(SOCKET*)clientSocketPtr;
const int BUFFER_SIZE = 9000;
char buffer[BUFFER_SIZE];
char bufferWithOffset[BUFFER_SIZE + 2];
int bytesReceived;
const char* response = "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n";
memset(buffer, 0, BUFFER_SIZE);
bytesReceived = recv(clientSocket, buffer, BUFFER_SIZE, 0);
if (bytesReceived == 0) {
send(clientSocket, response, strlen(response), 0);
closesocket(clientSocket);
return 0;
}
send(clientSocket, response, strlen(response), 0);
if (bytesReceived > 0) {
int position = 0;
int offset = 0;
for (int i = 0; i < (bytesReceived - 3); i++, offset++) {
if (buffer[i] == 13 && buffer[i + 1] == 10 && buffer[i + 2] == 13 && buffer[i + 3] == 10) {
position = i + 5; // Position just after the sequence \r\n\r\n
headerLength = i + 4;
break;
}
}
char clientIdentifier = buffer[position];
printf("%c", clientIdentifier);
char numberBuffer[9];
printf("\n");
char* payloadStart = &buffer[position + 1]; // or maybe 2 - but this is ok
for (int i = 0; i < 8; i++) {
numberBuffer[i] = *payloadStart;
payloadStart++;
}
numberBuffer[8] = '\0';
int packetCount = string_to_int(numberBuffer);
for (int i = 0; i < packetCount; i++) {
memset(buffer, 0, BUFFER_SIZE);
bytesReceived = recv(clientSocket, buffer, BUFFER_SIZE, 0);
send(clientSocket, response, strlen(response), 0);
if (bytesReceived <= 0) {
break;
}
memcpy(bufferWithOffset, buffer + headerLength, bytesReceived - headerLength);
}
printf("Total packets: %d from client ID: %c\n", packetCount, clientIdentifier);
}
closesocket(clientSocket);
return 0;
}
string_to_int() - reads 8 characters from pointer returns an integer number.
Actually, code opens 2 connections at the same time. Tested on one machine. Adding variable to block one connection doesn't work, so doesn't using mutexes. At the end of Edge connection there are two "Finished"