
Originally Posted by
DHillard
The ones that are listening to the port will receive the broadcast message. That's the definition of it being broadcast.
To solve your problem, you have to have the ones listening to the port, reply back to the PC that sent the broadcast. That PC can get the IP address of the ones that replied from the reply packet. The "From" address is in the packet header.
Thank you for the reply
I 'll tell in short what did I do....
I created a dialog based application. then I added a class inherited from the CAsyncSocket
In the dialog based class, I added a event handler to the button, When the button is clicked the socket is created then it broadcasts the message & listen to who is sending the reply.
But I am getting the response as: Socket error 10035.
I am at this point just listening to my PC still getting this error
Code:
void CModifiedSocketDlg :: SendMessageBroad()
{
int BroadCastInitial;
int iBroadCast = 0;
int rflag = 0, flag =1, len = 0;
BroadCastInitial = UDPSockptr -> SetSockOpt( SO_BROADCAST, &flag, sizeof( int ) );
if ( !BroadCastInitial )
{
wsprintf(m_SocError, "SetSockOpt failed to set SO_BROADCAST:% d", UDPSockptr->GetLastError());
delete UDPSockptr;
UDPSockptr = NULL;
AfxMessageBox ( m_SocError );
}
len = sizeof(rflag);
if ( UDPSockptr -> GetSockOpt( SO_BROADCAST, &rflag, &len, SOL_SOCKET ) && rflag == 0 )
{
wsprintf( m_SocError, "GetSockOpt failed to get SO_BROADCAST:%d ", UDPSockptr -> GetLastError());
delete UDPSockptr;
UDPSockptr = NULL;
AfxMessageBox ( m_SocError );
}
else
{
UpdateData(FALSE);
AfxMessageBox( "Msg received" );
}
sockaddr_in m_RemoteAdd;
m_RemoteAdd.sin_family = AF_INET;
m_RemoteAdd.sin_addr.s_addr = inet_addr( "255.255.255.255" );
m_RemoteAdd.sin_port = htonl( SO_BROADCAST );
iBroadCast = UDPSockptr -> SendTo("AnyBody here me?",20, (const SOCKADDR*) &m_RemoteAdd, sizeof ( m_RemoteAdd ) );
if ( !iBroadCast )
{
wsprintf(m_SocError, " Broadcasting failed % d", UDPSockptr->GetLastError());
AfxMessageBox ( m_SocError );
}
else
{
AfxMessageBox ( "Broadcasting successful" );
OnReceiveBroadcast();
}
}
This loop works fine as I can reach upto "Broadcasting successful" message..then ....
Code:
TCHAR ReceivedBuf[ 70000 ];
CString SendersIP;
TCHAR m_ScError[25];
UINT SendersPort;
int ChekReceived;
UpdateData(TRUE);
ChekReceived = UDPSockptr -> ReceiveFrom( ReceivedBuf, 69999, SendersIP, SendersPort );
if ( ChekReceived == SOCKET_ERROR )
{
wsprintf( m_ScError, "Failed to create Socket %i Close And Restart application", GetLastError());
AfxMessageBox ( m_ScError );
}
else
{
//TCHAR sazError[256];
//wsprintf(sazError, "OnReceive bytes: %d", ChekReceived);
//AfxMessageBox (sazError);
AfxMessageBox( "I come in OnReceive" );
CString Disp = ReceivedBuf;
m_ReceivedData += Disp;
UpdateData( FALSE );
This loop gives me the error ...... Failed to create Socket 10035 Close And Restart application.
Please suggest me why this is happening???