BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

UDP Broadcast

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Internet Socket)
View previous topic :: View next topic  
Author Message
Adam Griffin
Guest





PostPosted: Mon Apr 04, 2005 3:00 pm    Post subject: UDP Broadcast Reply with quote



I need to broadcast a udp packet across my lan. I get an invalid ip address
error when trying to with INADDR_ANY. I can successfully call sendto if I
specify an ip address. Below is the code I have written.

WSADATA wsaData;
SOCKET s;
sockaddr_in addr;

WSAStartup(MAKEWORD(2,0), &wsaData);
s = socket(AF_INET, SOCK_DGRAM,IPPROTO_UDP);
if (s==INVALID_SOCKET) {throw AnsiString("");}

addr.sin_family = AF_INET;
addr.sin_port = htons(ToPort->Text.ToInt());
addr.sin_addr.s_addr = htonl(INADDR_ANY);

int rc = bind(s,(SOCKADDR*)&addr,sizeof(SOCKADDR_IN));
if (rc==SOCKET_ERROR) {throw AnsiString("Bind Error:
"+AnsiString(WSAGetLastError()));}

int ret = sendto(s,pkt,len,0,(SOCKADDR*)&addr,sizeof(addr));
if (ret == SOCKET_ERROR) {
throw AnsiString("Send Error: "+AnsiString(WSAGetLastError()));
}
closesocket(s);
WSACleanup();

Thanks for any help,
atg




Back to top
Bob Gonder
Guest





PostPosted: Mon Apr 04, 2005 4:31 pm    Post subject: Re: UDP Broadcast Reply with quote



Adam Griffin wrote:

Quote:
I need to broadcast a udp packet across my lan. I get an invalid ip address
error when trying to with INADDR_ANY. I can successfully call sendto if I

INADDR_ANY, as the name implies, is for INput, or receiving a
broadcast. The Output address is

#define UDP_GROUP "255.255.255.255"
#define UDP_PORT 12345 /*whatever you want to use*/

SendSocket=socket( AF_INET, SOCK_DGRAM, IPPROTO_UDP )) )
/* turn on broadcast */
t = TRUE;

setsockopt(SendSocket,SOL_SOCKET,SO_BROADCAST,(char*)&t,sizeof(t));
/* set up destination address */
memset( &SendAddr, 0, sizeof(SendAddr) );
SendAddr.sin_family = AF_INET;
SendAddr.sin_addr.s_addr = inet_addr( UDP_GROUP );
SendAddr.sin_port = htons( UDP_PORT );
/* now just sendto() our destination! */
sendto( SendSocket, DataGram, sizeof(DataGram), 0,
(struct sockaddr *) &SendAddr, sizeof(SendAddr) );



Back to top
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Internet Socket) All times are GMT
Page 1 of 1

 
 


Powered by phpBB © 2001, 2006 phpBB Group
.