Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C 

recv() fails every time..

I cannot use the Recv function. It always gives me errors.

PHP Code

<?php 
if ((recv_size = recv(main, server_reply, 2000, 0)) > 0) {
 
cout << "Recv failed!" << endl;
 
getchar();
 return 
1;
 }
 
 
server_reply[recv_size] = '\0';
 
cout << server_reply << endl;

 
closesocket(main);
 
WSACleanup();
 
getchar();
 
   return 0; 

Can anyone tell me a fix.
You're saying if recv is greater than zero, then it failed. Recv fails on -1, it succeeds if it's above zero.
Hi,

As said above.

https://docs.microsoft.com/en-us/windows...nsock-recv

Quote:int recv( SOCKET  s,  char *buf,  int  len,  int  flags);

Return Value

If no error occurs, recv returns the number of bytes received and the buffer pointed to by the buf parameter will contain this data received. If the connection has been gracefully closed, the return value is zero.

Otherwise, a value of SOCKET_ERROR is returned, and a specific error code can be retrieved by calling WSAGetLastError.


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.
(Jul 29, 2018 02:59 AM)sqsgkrlhdwg Wrote:  Hi,

As said above.

https://docs.microsoft.com/en-us/windows...nsock-recv


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.
This explanation was actually good. Thanks a lot for this.
I'll try this implementation and tell you, Thanks again.
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.
(Jul 29, 2018 05:00 AM)sqsgkrlhdwg Wrote:  But going in with std::string or anything related will cause serious issues ...
You must implement a serialize function for that.

Have fun.

Example of serialization and deserialization with struct of std::string
https://github.com/HFaeiro/ChatServerAnd...er.cpp#L23

struct was defined as:
Code
struct _sData {
            int Type;
            std::string fU;
            std::string fP;
            std::string tU;
            char* Buffer = new char[512];
    };
[Image: 5Iq5rDJ.gif]

winapi, and winsock programmer

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

<?php 
if (recv_size == recv(main, server_reply, BUFFER, 0) > 0) {
        
//server_reply[recv_size] = '\0';
        
std::cout << server_reply << std::endl;
    }
    else {
        
std::cout << "recv() returned -1.." << std::endl;
    } 


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

<?php 
std
::cout << "Attempting to connect to serv " << std::endl;
    if (
connect(main, (struct sockaddr*)&main, sizeof(main)) != SOCKET_ERROR) {
        
std::cout << "Unable to Connect : " << WSAGetLastError();
        
getchar();
        return 
1;
    }
    
std::cout << "Connected!" << std::endl; 
(Jul 29, 2018 11:25 AM)F.A.S.T Wrote:  
PHP Code

<?php 
if (connect(main, (struct sockaddr*)&main, sizeof(main)) != SOCKET_ERROR) {
 
std::cout << "Unable to Connect : " << WSAGetLastError();
 
getchar();
 return 
1;
 } 
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.
[Image: 5Iq5rDJ.gif]

winapi, and winsock programmer

(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.
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.

Yeah. I had doubts about it as well. Thanks.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C] by pebblakOct 15, 2019 05:24 AM 6 307 Oct 15, 2019 05:24 AM
Last Post: Joseph Ducreux
[C] by F.A.S.TNov 2, 2018 03:35 PM 1 148 Nov 2, 2018 03:35 PM
Last Post: Auriana
[C] by GoliaXJul 16, 2018 08:26 PM 3 169 Jul 16, 2018 08:26 PM
Last Post: Auriana