Jump to content
  • Advertisement
Sign in to follow this  
Richard3d

Problem receiving from broadcast

This topic is 3639 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 }

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!