Alex
Alex

Reputation: 45

C++ WSAEFAULT error sending integer

I am trying to send an integer over the internet. The problem is not bid-endian related, as the problem also arises when i send the integer to myself with 127.0.0.1 . This is the code:

int res = send(Socket, (char*)refresh, sizeof(refresh), 0);

Refresh is usually 100. res is -1, and calling WsaGetLastError returns 10014, WSAEFAULT. From what I understood this means the size I've specified is different from the size of the buffer. How can the size of refresh be different from sizeof(refresh)? On the other side:

int dim = recv(Socket, (char*)&refresh, sizeof (refresh), MSG_WAITALL);

dim is 4, wich is right, but if I check refresh it's still 0. Any clue? Thanks in advance.

Upvotes: 0

Views: 659

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 596352

WSAEFAULT means the combination of buf and len parameter values working together does not represent a valid memory block within the calling process's address space.

(char*)refresh passes the value of the integer interpreted as a memory address, which is the wrong thing to do.

(char*)&refresh passes the address of the integer, which is the correct thing to do.

Upvotes: 1

Related Questions