-
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
-
January 8th, 2009, 03:53 AM
#2
Re: Broadcasting in UDP
Note that the most of network administrators do NOT allow any UDP (neither sockets nor mailslots).
However, your own computer is supposed to receive those messages.
Victor Nijegorodov
-
January 8th, 2009, 05:18 AM
#3
Re: Broadcasting in UDP
@VictorN - are you sure about your statement that "most of network administrators do NOT allow any UDP" as I've never come across that situation. Or did you mean broadcast messages?
-
January 8th, 2009, 05:21 AM
#4
Re: Broadcasting in UDP
Originally Posted by Edders
... Or did you mean broadcast messages?
You are right! I meant broadcast messages. Sorry!
Victor Nijegorodov
-
January 8th, 2009, 05:50 AM
#5
Re: Broadcasting in UDP
In my network broadcast messages are allowed as I have checked out the same using another EXE.
The issue is how to achieve this, I mean get the IP of the machine using the application
-
January 8th, 2009, 06:00 AM
#6
Re: Broadcasting in UDP
Have a look at this thread and at the pointed out FAQ.
Victor Nijegorodov
-
January 8th, 2009, 06:01 AM
#7
Re: Broadcasting in UDP
Scorrpeio, I should expect a while() { ... } block that contains the messages received code, not only a single GetSockOpt() call.
-
January 8th, 2009, 07:04 AM
#8
Re: Broadcasting in UDP
@Maximus X, I dont get your point clearly
@Victor, Thank you for the link. But, corresponding discussion was about getting the IP/name of the machine which uses the application(sorry, I did mistake in last reply by saying that I want such function which could give me the name of only single PC )
I want to know the IP/names of all the machines which are using the application. just like in LAN messenger we can see the available names.
Please suggest me the solution
-
January 8th, 2009, 08:35 AM
#9
Re: Broadcasting in UDP
Originally Posted by scorrpeio
@Maximus X, I dont get your point clearly
At this moment your code fails because I cannot received instantaneous good result. So you need to wait the right answer in a while block.
I suppose and guess that you are using a second thread code for this broadcast code. Well from the application interface, you can control this while using a bool flag:
Code:
while(!pObject->m_bStatus)
{
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" );
}
}
But be careful... don't use AfxMessageBox() inside of while block because it stops your thread over it.
-
January 9th, 2009, 07:36 AM
#10
Re: Broadcasting in UDP
Originally Posted by VictorN
Thank you for the link. But, corresponding discussion was about getting the IP/name of the machine which uses the application(sorry, I did mistake in last reply by saying that I want such function which could give me the name of only single PC )
I want to know the IP/names of all the machines which are using the application. just like in LAN messenger we can see the available names.
Please suggest me the solution
-
January 9th, 2009, 03:01 PM
#11
Re: Broadcasting in UDP
salve scorrpeio,
Something looks a bit dubious.
Are you positive on this line: m_RemoteAdd.sin_port = htonl( INADDR_BROADCAST ); ?
INADDR_BROADCAST is usually a placeholder for the bcast addr 255.255.255.255.
You should pass a port number. INADDR_BROADCAST is equivalent to 0xffffffff.
sin_port being an unsigned short gives you portnumber 65535. Is this your intended usage ?
Also check the send/receive buffer sizes.
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
|