Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

winsock socket as file handle

i have been scratching my head and been searching for an answer for this for hours now. Basically what I do is open a socket to some other machine and reading the data from it. This socket is than "converted" to a file handle via an fdopen call passing in the int that represent the socket. The resulting file handle is then passed to a bison parser which directly parsed the data received over the socket. All of this works fine on linux. Now I have tried to port this code to windows and I dismally fail. The code looks something like this:

        FILE* fileHandle;
        #if defined WINCE || defined WIN32
        int fd = _open_osfhandle(socket, _O_RDONLY);
        if (fileHandle = fdopen(fd, "r")) {
        #else
        if (fileHandle = fdopen(socket, "r")) {
        #endif
           ... // code to call my parser with fileHandle as argument

The bison/flex parser fails in the windows version since the filehandle seems to point to an empty stream/file. Can anybody point to a comprehensive resource that explains this stuff or hint at an alternative solution?

Thanks and best Regards,

André

like image 268
André Avatar asked Apr 09 '12 17:04

André


1 Answers

In Windows, a socket handle is not a file handle, and you cannot treat it as such in the C API. In Linux, you can. However, in Windows, a socket handle can be passed to the ReadFile/Ex() and WriteFile/Ex() functions, which support numerous handle types, not just files, despite their names.

like image 184
Remy Lebeau Avatar answered Sep 24 '22 19:09

Remy Lebeau