Handling Persistent Connections in Edge Similar to Firefox

Wojciech Sobiesiak 96 Reputation points
2024-11-15T08:26:01.27+00:00

Handling Persistent Connections in Edge Similar to Firefox

Hello,

I have a question regarding creating a connection to the server. The task is straightforward: send a packet that introduces itself and specifies the size of the transmission. The server reads the packet and, in a loop, fetches subsequent data as many times as requested.

The issue is that after the first connection, Edge closes the connection and opens a new one. While this works, it is not elegant. On the other hand, Firefox handles the entire transmission within the same connection. How can I make Edge behave like Firefox in this case? How to solve this?

Edge Output:


=========== Client number ====1                     // First connection

-==== Client number == 1 ==== Size 586 ===

===============================

00000002loop

=========== Client number ====2                     // Second connection

========= Client number 2, Packet number 0, Size 677 ====    // Second connection

==================================================

Packet 0

AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

========= Client number 2, Packet number 1, Size 677 ====    // Second connection

==================================================

Packet 1

BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB

Total packets: 2

Finished

Firefox Output:


=========== Client number ====1              // First connection

-==== Client number == 1 ==== Size 412 ===

===============================

00000002loop

========= Client number 1, Packet number 0, Size 503 ====       // First connection

==================================================

Packet 0

AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

========= Client number 1, Packet number 1, Size 503 ====     // First connection

==================================================

Packet 1

BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB

Total packets: 2

Finished

As you can see, Firefox handles this much more efficiently.

How can I achieve the same behavior in Edge?

Best regards,

Wojtek Sobiesiak

https://www.youtube.com/@wojciechsobiesiak/videos

Microsoft Edge
Microsoft Edge
A Microsoft cross-platform web browser that provides privacy, learning, and accessibility tools.
2,369 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Wojciech Sobiesiak 96 Reputation points
    2024-11-18T05:17:47.15+00:00

    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"

    0 comments No comments

  2. Wojciech Sobiesiak 96 Reputation points
    2024-11-18T05:22:07.57+00:00

    This is client code in javascript (maybe it will help) - two functions , one starts transmision and next next one sends alll data.

    body: "D1"+intToString(window.top.totalPackets) - D name od operation, client number (10 will be ok for now), and number of packets.

    
    let textElement = document.body.children[0];
    
    let outputTextarea = document.querySelectorAll("TEXTAREA")[0];
    
    let buffer = new ArrayBuffer(600);
    
    let uint8View = new Uint8Array(buffer);
    
    // Fill buffer with sequential data
    
    for (let i = 0; i < 6; i++) {
    
        uint8View.fill(65 + i, i * 100, (i * 100) + 100);
    
    }
    
    // Global packet information
    
    window.top.currentPacketNumber = 0;
    
    window.top.totalPackets = 0;
    
    // Function to start data transfer
    
    function startDataTransfer() {
    
        window.top.currentPacketNumber = 0;
    
        let totalSize = sizeInput.value;
    
        let packetSize = packetSizeInput.value;
    
        window.top.totalPackets = Math.ceil(totalSize / packetSize);
    
        let progressPercentage = 100 / window.top.totalPackets;
    
        let progressWidth = 0;
    
        // Notify the server about the total number of packets
    
        fetch("http://localhost:80", {
    
            method: "POST",
    
            mode: "no-cors",
    
            headers: { "Content-Type": "text/plain" },
    
            body: "D1" + intToString(window.top.totalPackets)
    
        });
    
        // Start sending packets after a delay
    
        setTimeout(function() {
    
            sendPacket();
    
        }, 600);
    
    }
    
    // Function to send packets
    
    function sendPacket() {
    
        let totalSize = sizeInput.value;
    
        let packetSize = packetSizeInput.value;
    
        let packet;
    
        if (window.top.currentPacketNumber < window.top.totalPackets) {
    
            console.log("Sent packet: " + window.top.currentPacketNumber);
    
            let start = window.top.currentPacketNumber * packetSize;
    
            let end = Number(start) + Number(packetSize);
    
            packet = uint8View.slice(start, end);
    
            window.top.currentPacketNumber++;
    
            // Send packet to server
    
            fetch("http://localhost:80", {
    
                method: "POST",
    
                mode: "no-cors",
    
                headers: { "Content-Type": "text/plain" },
    
                body: packet
    
            })
    
            .then(function(response) {
    
                sendPacket(); // Send the next packet
    
                if (!response.ok) {
    
                    console.error("Failed to send packet", window.top.currentPacketNumber);
    
                }
    
            })
    
            .catch(function(error) {
    
                console.error("Error sending packet:", error);
    
            });
    
        } else {
    
            if (statusElement.innerText == '1') {
    
                setTimeout(function() {
    
                    startDataTransfer();
    
                }, 3000);
    
            }
    
        }
    
    }
    
    

    The goal is to send random number of packets in loop, one after another in many threads on server to test it, how it will handle many connections. Any suggestions?

    0 comments No comments

  3. ShiJieLi-MSFT 10,446 Reputation points Microsoft Vendor
    2024-11-18T09:35:48.36+00:00

    Hi @Wojciech Sobiesiak ,

    Unfortunately, I'm not able to reproduce this issue with the code you've provided. Please try testing on Edge insider channels and see if this problem still occurs.

    Moreover, if the problem still occurs, please report this problem to the Edge Dev Team by sending feedback. You can send feedback in Microsoft Edge at ··· menu --> Help and feedback --> Send feedback.


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best Regards,

    Shijie Li


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.