Home > Net >  Why 0xFEFF appear in recv data
Why 0xFEFF appear in recv data

Time:08-04

i try to create a client written in c and a server using python flask. The task is simple, client connect and get data from server, then display it on console. I have two piece of code:

Server:

from flask import Flask

app = Flask(__name__)

@app.route('/hello')
def hello():
    return "hello".encode("utf-16")


if __name__ == "__main__":
    app.run(debug=True,host="127.0.0.1")

Client:

BOOL bResults = FALSE;
    bool ret = false;
    DWORD dwDownloaded = 0;
    WCHAR wDataRecv[1024] = {0};
    HINTERNET hConnect = NULL;
    HINTERNET hRequest = NULL;
    DWORD dwSize = 0;

    HINTERNET hSession = WinHttpOpen(L"WinHTTP Example/1.0",
        WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
        WINHTTP_NO_PROXY_NAME,
        WINHTTP_NO_PROXY_BYPASS, 0); # open session to connect
    hConnect = WinHttpConnect(hSession, L"localhost", (port == 0) ? INTERNET_DEFAULT_HTTP_PORT : port, 0); # connect to localhost with port 80 or custom port (this time i use port 5000)
    if (!hConnect)
        goto Free_And_Exit;
    hRequest = WinHttpOpenRequest(hConnect, L"GET", L"/hello", NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, 0); 
    
    if (hRequest) {
        bResults = WinHttpSendRequest(hRequest, NULL, 0,WINHTTP_NO_REQUEST_DATA, 0,0, 0);# send GET request
    }

    if (bResults)
        bResults = WinHttpReceiveResponse(hRequest, NULL);

    if (bResults)
    {
        dwSize = 0;
        if (!WinHttpQueryDataAvailable(hRequest, &dwSize)) {
            std::cout << "WinHttpQueryDataAvailable failed with code: " << GetLastError();
        }
        if (!WinHttpReadData(hRequest, wDataRecv, dwSize, &dwDownloaded)) {
            std::cout << "WinHttpReadData failed with code: " << GetLastError();
        }
        std::wcout << wDataRecv; # wcout wont print anything to console
    }
Free_And_Exit: #clean up
    if (hRequest) WinHttpCloseHandle(hRequest);
    if (hConnect) WinHttpCloseHandle(hConnect);
    return ret;

I noticed that the data return from server like:

b'\xfe\xff\hello'

Why 0xFEFF is there ?

CodePudding user response:

Its is BOM (byte order mark). I just need to decode from the client or use:

return "hello".encode('UTF-16LE') # not UTF-16
  • Related