如何在Windows中将套接字设置为阻塞模式? [英] How to set sockets to blocking mode in Windows?

查看:514
本文介绍了如何在Windows中将套接字设置为阻塞模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一些相当简单的跨平台TCP套接字编程。我不幸发现,在Windows上编译时,我的套接字默认情况下是非阻塞的,而在OS X上,他们默认阻塞。如何在Windows上强制套接字进入阻塞模式?他们通常默认为非阻塞模式或是非常错误的?

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, while on OS X they are blocking by default. How do I force a socket into blocking mode on Windows? Do they normally default to non-blocking mode or is something terribly wrong?

我的代码部分基于这些简单的例子:
http://cs.baylor.edu/~donahoo/practical/CSockets/code/TCPEchoClient。 c
http://cs.baylor .edu /〜donahoo / practical / CSockets / code / TCPEchoServer.c

My code is based in part on these simple examples: http://cs.baylor.edu/~donahoo/practical/CSockets/code/TCPEchoClient.c http://cs.baylor.edu/~donahoo/practical/CSockets/code/TCPEchoServer.c

推荐答案

我相信此引用可能有帮助;注意,特别是

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


虽然在
套接字下支持
套接字的阻塞操作,强烈
不鼓励。
被限制使用阻塞模式的程序员 -
例如,作为要移植的现有
应用程序的一部分 -
应该知道$的语义b $ b在Windows中阻止操作
套接字。

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.

如果您完全知道列出的zillion问题这里,并找到处理所有他们更喜欢设计您的程序,以适应与Windows而不是来自Unix的半>嘟嘟声端口,请立即使用 ioctlsocket 调用,cmd参数设置为FIONBIO,第三个参数指向一个值为0的longword。但是,不要说你没有被警告过; - )。

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;-).

这篇关于如何在Windows中将套接字设置为阻塞模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆