|
-
January 8th, 2009, 02:18 AM
#1
Broadcasting in UDP
I want to know the IP & port of the machines. For this purpose I am broadcasting any message through UDP socket.
But I dont receive any message(even from my machine).
Is it possible to code such a way that when dialog box is created, the constructor itself broadcast the message & listen to the reply. If reply occurs it will find out the IP & port of that machine.
If it is possible in VC++. PLease let me know how. I have done this(sorry, for it is wrong) but not up to the mark..............
Code:
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 );
}
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( INADDR_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" );
}
len = sizeof(rflag);
if ( UDPSockptr -> GetSockOpt( SO_BROADCAST, &rflag, &len ) && rflag == 0 )
{
wsprintf( m_SocError, "GetSockOpt failed to get SO_BROADCAST:% d", UDPSockptr -> GetLastError());
delete UDPSockptr;
UDPSockptr = NULL;
AfxMessageBox ( m_SocError );
}
else
{
AfxMessageBox( "Msg received" );
}
I called this function in the constructor. It gets executed without error. But, sent message is not displayed
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|