Sockets: What is causing read() to return EINVAL?

瘦欲@ 提交于 2019-12-08 04:07:44

问题


A socket client program establishes a connection with the server, writes some bytes and waits for response using the (blocking) read().

But this fails with the error EINVAL ("Invalid argument"). Previous calls to create(), bind() and connect() the socket have been made successfully.

My Question

  • What's wrong here?

Platform is Linux x64.


回答1:


fd is attached to an object which is unsuitable for reading; or the file was opened with the O_DIRECT flag, and either the address specified in buf, the value specified in count, or the current file offset is not suitably aligned.

See http://www.kernel.org/doc/man-pages/online/pages/man2/read.2.html




回答2:


The problem was that I passed a size of 1 byte to the read() function. It seems that this is not supported
(why? what is the minimum size? must it be a multiple of 2/the bitness of the platform?).

Now I am passing 8 and it works. Thank you all for your comments.



来源:https://stackoverflow.com/questions/6018974/sockets-what-is-causing-read-to-return-einval

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!