![]() |
|
[C] recv() fails every time.. - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118) +---- Thread: [C] recv() fails every time.. (/showthread.php?tid=5867253) |
recv() fails every time.. - F.A.S.T - Jul 28, 2018 I cannot use the Recv function. It always gives me errors. PHP Code Can anyone tell me a fix. RE: recv() fails every time.. - Auriana - Jul 28, 2018 You're saying if recv is greater than zero, then it failed. Recv fails on -1, it succeeds if it's above zero. RE: recv() fails every time.. - sqsgkrlhdwg - Jul 29, 2018 Hi, As said above. https://docs.microsoft.com/en-us/windows/desktop/api/winsock/nf-winsock-recv Quote:int recv( SOCKET s, char *buf, int len, int flags); Be aware that sometimes your buffer could be incomplete, I mean the recv function may not completly fill up to len your buffer. In blocking mode, you can use this function to make sure to receive exactly len bytes. If using non blocking mode, this function will be useless ... the implementation is different. RE: recv() fails every time.. - F.A.S.T - Jul 29, 2018 (Jul 29, 2018 02:59 AM)sqsgkrlhdwg Wrote: Hi,This explanation was actually good. Thanks a lot for this. I'll try this implementation and tell you, Thanks again. RE: recv() fails every time.. - sqsgkrlhdwg - Jul 29, 2018 You seems to be a cool guy. Bonus shortcuts : With that you can pass "anything" over the socket. But it must be used only with basic data (integers and basic arrays). You must not use this to pass objects containing pointers obviously otherwise the pointer will pass over the network itself but the data it refers to. for instance : But going in with std::string or anything related will cause serious issues ... You must implement a serialize function for that. Have fun. RE: recv() fails every time.. - aeiro - Jul 29, 2018 (Jul 29, 2018 05:00 AM)sqsgkrlhdwg Wrote: But going in with std::string or anything related will cause serious issues ... Example of serialization and deserialization with struct of std::string https://github.com/HFaeiro/ChatServerAndClient/blob/245db60084b013c163a0605e5b8c5bce38314d6f/Server/HashChatServer/Data_Handler.cpp#L23 struct was defined as: Code struct _sData {RE: recv() fails every time.. - F.A.S.T - Jul 29, 2018 Ok so thanks a lot I actually understood how recv() works. but I don't know why it returns ASCII trash? Maybe because it's an int? .. @sqsgkrlhdwg, Thank you so much, For explaining that to me. I took your reference and did some experiments and the following code Worked.. PHP Code Also, I do not understand this. Clearly, The if statement is telling that if connect is not SOCKET_ERROR than raise error? What? And changing it to == SOCKET_ERROR it gives an connect Error? Am I understanding wrong or the code is wrong? have a look PHP Code RE: recv() fails every time.. - aeiro - Jul 30, 2018 (Jul 29, 2018 11:25 AM)F.A.S.T Wrote:so what your code is saying is that if it does not = SOCKET_ERROR then std::cout an error that does not exist. it should be == SOCKET_ERROR. then if there is an error it will cout the last error. at that point you should be looking up the error code. RE: recv() fails every time.. - F.A.S.T - Jul 30, 2018 (Jul 30, 2018 03:55 PM)aeiro Wrote: so what your code is saying is that if it does not = SOCKET_ERROR then std::cout an error that does not exist. Yeah. I had doubts about it as well. Thanks. |