How to set sockets to blocking mode in Windows?

前端 未结 2 1268
无人共我
无人共我 2021-01-06 13:20

I\'m doing some fairly simple cross-platform TCP socket programming. I have unfortunately found out that when compiled on Windows, my sockets are non-blocking by default, wh

相关标签:
2条回答
  • 2021-01-06 13:57

    I believe this reference may help; note, in particular, that

    Although blocking operations on sockets are supported under Windows Sockets, their use is strongly discouraged. Programmers who are constrained to use blocking mode -- for example, as part of an existing application which is to be ported -- should be aware of the semantics of blocking operations in Windows Sockets.

    If you're fully aware of the zillion issues listed here, and find dealing with all of them preferable to designing your program to fit in well with Windows rather than being a half-beeped port from Unix, go right ahead with a ioctlsocket call with the cmd argument set to FIONBIO and the third argument pointing to a longword worth 0. But, don't say you weren't warned;-).

    0 讨论(0)
  • 2021-01-06 14:04

    No, plain old BSD style sockets are not non-blocking by default on Windows. See the examples in the Winsock Programmer's FAQ.

    If you use MFC and its CAsyncSocket mess, then yes, it's all non-blocking, but that's a separate issue.

    0 讨论(0)
提交回复
热议问题