Problem receiving from broadcast

Started by
5 comments, last by oliii 15 years, 5 months ago
Hello, I have an application (server side) I need to receive data from. The application is broadcasting on 255.255.255.255 using UDP on port 5000. I have verified the data is being sent using the program WireShark. My code to init and receive are down below. The prob is in recvfrom I always get the WSA error 10022 which means one of the items is an invalid argument but I cant for the live of me figure out which one and why. Any help would be greatly appreciated - Richard THIS IS THE INIT FUNCTION memset(&m_SenderAddr,0, sizeof(sockaddr_in)); m_SenderAddrSize = sizeof(sockaddr_in); //----------------------------------------------- // Initialize Winsock int ret = WSAStartup(MAKEWORD(2,2), &m_WsaData); if(ret != 0) { ret = WSAGetLastError(); return false; } //----------------------------------------------- // Create a receiver socket to receive datagrams if(( m_RecvSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == INVALID_SOCKET) { ret = WSAGetLastError(); WSACleanup(); return false; } u_long enable = 1; ioctlsocket( m_RecvSocket, FIONBIO, &enable); char bOptVal = 1; if(SOCKET_ERROR == setsockopt(m_RecvSocket, SOL_SOCKET, SO_BROADCAST, &bOptVal, sizeof(bOptVal))) { ret = WSAGetLastError(); } //----------------------------------------------- // Bind the socket to any address and the specified port. sockaddr_in pIn; memset(&pIn,0, sizeof(sockaddr_in)); pIn.sin_family = AF_INET; pIn.sin_port = htons(5000); pIn.sin_addr.s_addr = htonl(INADDR_ANY); ret = bind(m_RecvSocket,(sockaddr*) &pIn, sizeof(pIn)); THIS IS THE BODY OF THE RECEIVE FUNCTION if(SOCKET_ERROR == recvfrom( m_RecvSocket, m_RecvBuf, (int)sizeof(UAVPayloadStateMsg), 0, (sockaddr *)&m_SenderAddr, &m_SenderAddrSize)) { //bad news - see whats up ret = WSAGetLastError(); } //we got good data else { //I NEVER GET HERE }
"Do not fear mistakes. There are none." - Miles Davis
Advertisement
Your socket is non-blocking. Most of the time, there will be nothing to receive. That will return an error from recvfrom(). Note that you should set the value of the "size" parameter each time before you call recvfrom(), too.
enum Bool { True, False, FileNotFound };
for a non-blocking socket, you would get WSAEWOULDBLOCK if there was no data pending. Dunno why you are getting WSAEINVAL.

try using select() before calling recvfrom() to check if you have something to be read in the socket.

Also, is the bind() working?

Everything is better with Metal.

Oliii Great Eye! The Bind function as it turns out is not working correctly I get the general winsock error 10048. If you wouldnt mind looking over my code again to maybe help understand what I am doing to cause this I would appreciate it!
"Do not fear mistakes. There are none." - Miles Davis
Hey Thanks to everyone for the help I got it figured out. There was another application that was bound (for listening) to the same port I was trying to use thus my bind was failing. Oliii thanks again for the extra set of eyes - Richard
"Do not fear mistakes. There are none." - Miles Davis
your error is the htonl for the address. Only the port needs to be byte swapped.
np.

...and that too. Although INADDR_ANY is 0.

Everything is better with Metal.

This topic is closed to new replies.

Advertisement