Click here to Skip to main content
15,885,886 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to create a simple UDP app to capture data being sent by another app. When the code hits the call to recvfrom() method it returns -1 and WSAGetLastError returns 10022. I am unable to detect what is wrong with the arguments. Here are the declarations, the setup, and the call.
Please note that I cannot copy paste from my working mechine to here and I ask that you be a bit tolerant of typos.
The declarations

const unsigned in BUFLEN = 1024;
const char IP_ADDRESS[ ] = "192.10.11.33";
const unsigned int ETHERNET_PORT = 2055;
int receive_status = 0;
SOCKET  m_receive_socket;
char       buff[ FUBLEN ];
STRUCT  sockaddr_in  m_socket_address_in
int  sock_address_size;


Initialization

sock_address_size = sizeof( m_socket_address_in );
m_receive_socket = socket( AF_INET, SOCK_DGRAM, IPPROTO_UDP);
// error check from previous line not shown, did not return SOCKET_ERROR
memset( (char *) &m_socket_address_in, 0, sizeof( m_socket_address_in) );
m_socket_address_in.sin_family = AF_INET;
m_socket_address_in.sin_port =  htons( ETHERNET_PORT );
m_socket_address_in.sin_addr.S_un.S_addr = INET_ADDR( IP_ADDRESS );


Code
receive_status = recvfrom( m_receive_socket,  // value = 264
                                buff,
                                BUFLEN,            // value = 1024
                                0,
                                (struct sockaddr * ) &m_socket_address_in,
                                &sock_address_size );   // value = 16


I have looked at the arguments and am unable to recognize the invalid one. Is this clear to someone with more experience?

What I have tried:

I have looked at the values of the arguments. The addresses look valid.
Posted
Updated 22-Mar-17 2:56am
v3
Comments
Richard MacCutchan 21-Mar-17 16:17pm    
I think you have the socket setups the wrong way round. m_receive_socket should contain the IP address, port etc. details. The m_socket_address_in packet gets filled in when a message is received.

You have not bound the socket. See the recvfrom function (Windows)[^]:
Quote:

WSAEINVAL
The socket has not been bound with bind, or an unknown flag was specified, or MSG_OOB was specified for a socket with SO_OOBINLINE enabled, or (for byte stream-style sockets only) len was zero or negative.

See also the above link for example code:
// Bind the socket to the specified address and port.
m_socket_address_in.sin_family = AF_INET;
m_socket_address_in.sin_port = htons(ETHERNET_PORT);
m_socket_address_in.sin_addr.s_addr = htonl(IP_ADDRESS);
int bind_status = bind(m_receive_socket, (SOCKADDR *) &m_socket_address_in, sizeof (m_socket_address_in));

// Wait for data and retrieve
sockaddr_in SenderAddr;
receive_status = recvfrom(m_receive_socket,
                          buff, sizeof(buff),
                          0,
                          (struct sockaddr * )&SenderAddr,
                          sizeof(SenderAddr));
 
Share this answer
 
To RM,
Thanks for the reply.
The declaration of m_receive_socket in my code is
SOCKET  m_receive_socket;

When SOCKET is chased down the definition is
typedef UINT_PTR SOCKET

I do not know how the SOCKET item could contain the IP address.
 
Share this answer
 
Comments
Richard MacCutchan 22-Mar-17 13:25pm    
Well I have only found this by luck since it is a)not a solution, and b) was not posted as a reply to my comment, and c) bears no relation to my comment. You have filled the details of the wrong sockaddr structure so that is why you get the 10022 error. You need to fill the details into the structure that the recvfrom function need to listen on, not the one that will be filled in from the received message.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900