简体   繁体   English

Boost.asio,async_read和套接字类型

[英]Boost.asio, async_read and socket kind

I have a client/server application that is trying to read. 我有一个尝试读取的客户端/服务器应用程序。 When reading from the client, it is done asynchronously. 从客户端读取时,它是异步完成的。 It seems that async_read on the client side is failing. 似乎客户端的async_read失败。

I think that my client is trying to async_read before the server does the corresponding async_write (not completely sure). 我认为我的客户端在服务器执行相应的async_write之前尝试async_read(不确定)。 So my questions here are conceptual: 所以我的问题是概念性的:

  1. can I async_read before the corresponding async_write to the socket on the server side has been done? 在完成对服务器端套接字的相应async_write之前,我可以进行async_read吗?
  2. I am not sure (cannot try right now), but seems that the kind of socket (blocking/non-blocking) changes the behavior of 1. 我不确定(目前无法尝试),但是似乎套接字的类型(阻塞/非阻塞)会更改1的行为。
  3. In case I want to use non-blocking sockets, how can I async_read when I know there is data available? 如果我想使用非阻塞套接字,当我知道有可用数据时,如何async_read? Is the bytes_readable or similar what I am looking for? 我正在寻找bytes_read或类似的东西吗?

Thank you very much. 非常感谢你。 I would appreciate confirmation on my 3 questions, since I have been trying many things for a while and I have little time to try further experiments. 由于我已经尝试了很多时间,并且我没有时间尝试进一步的实验,因此希望对我的3个问题进行确认。

  1. can I async_read before the corresponding async_write to the socket on the server side has been done? 在完成对服务器端套接字的相应async_write之前,我可以进行async_read吗?

Yes. 是。

  1. I am not sure (cannot try right now), but seems that the kind of socket (blocking/non-blocking) changes the behavior of 1. 我不确定(目前无法尝试),但是似乎套接字的类型(阻塞/非阻塞)会更改1的行为。

Quite possibly, but you shouldn't put the socket into non-blocking mode. 很有可能,但是您不应该将套接字置于非阻塞模式。 Asynchronous I/O is a third I/O model, nothing to do with non-blocking mode. 异步I / O是第三个I / O模型,与非阻塞模式无关。

  1. In case I want to use non-blocking sockets, how can I async_read when I know there is data available? 如果我想使用非阻塞套接字,当我知道有可用数据时,如何async_read?

This is a contradiction in terms. 这是一个矛盾。 Asynchronous I/O starts the operation and provides a rendezvous when it finishes. 异步I / O启动操作,并在完成时提供一个集合点。 The operation itself remains blocking, but not to the async caller. 该操作本身仍然处于阻塞状态,但不会阻止异步调用方。 If you want to use non-blocking sockets you should not be also attempting to use asynchronous I/O. 如果要使用非阻塞套接字,则也不应尝试使用异步I / O。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM