
|
[openbeosnetteam]
||
[Date Prev]
[10-2004 Date Index]
[Date Next]
||
[Thread Prev]
[10-2004 Thread Index]
[Thread Next]
[openbeosnetteam] Re: DHCP Status + Help Needed
- From: Ingo Weinhold <bonefish@xxxxxxxxxxxxxxx>
- To: openbeosnetteam@xxxxxxxxxxxxx
- Date: Wed, 13 Oct 2004 00:16:13 +0200
On 2004-10-12 at 23:56:04 [+0200], David Enderson wrote:
> Networking Team,
>
> I'm into my second week of unpaid vacation and have spent part of the
> time working on the Haiku DHCP implementation as promised. I've got a
> nearly complete understanding of the RFC and I've enough of the client
> together for some test messages with a DHCP server.
>
> (This is where the "Help Needed" part comes in)
>
> What I'm lacking is the ability to broadcast a UDP packet. I have
> googled for numerous examples and have been thoroughly frustrated. I
> think I can send a UDP packet, but I can't get broadcast to work. I
> implemented a working sockets implementation for an open source game a
> while back, so I think I can do this, I just need the...rosetta
> stone...if you will for UDP.
>
> Can anyone point me towards excellent, simple, and COMPILEABLE examples?
I just very recently learned how to do broadcasting myself. Attached is my
test app. It's definitely simple and compiles and runs under R5. About the
`excellent' part I don't know.
Note that POSIX defines the socket option SO_BROADCAST, that -- if I
understand it correctly -- has to be set (via setsockopt()) for both the
socket of the sender and the receiver. Since R5 doesn't know this option, I
can't set it. Don't know, if it is required for BONE/our stack.
CU, Ingo // broadcast-test.cpp
#include <errno.h>
#include <socket.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// client
static
status_t
client(unsigned short port)
{
printf("client(): port: %hu\n", port);
// create a socket
int fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd < 0)
return errno;
// bind it to the port
sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = INADDR_ANY;
if (bind(fd, (sockaddr*)&addr, sizeof(addr)) < 0) {
printf("binding failed: %s\n", strerror(errno));
closesocket(fd);
return errno;
}
// receive
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = INADDR_BROADCAST;
int addrSize = sizeof(addr);
char buffer[1024];
ssize_t bytesRead = recvfrom(fd, buffer, sizeof(buffer), 0,
(sockaddr*)&addr, &addrSize);
if (bytesRead >= 0)
printf("received %ld bytes\n", bytesRead);
else
printf("recvfrom failed: %s\n", strerror(errno));
closesocket(fd);
return B_OK;
}
// server
static
status_t
server(unsigned short port)
{
printf("server(): port: %hu\n", port);
// create a socket
int fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd < 0)
return errno;
// send
sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = INADDR_BROADCAST;
int addrSize = sizeof(addr);
char buffer[1024];
ssize_t bytesSent = sendto(fd, buffer, 29, 0,
(sockaddr*)&addr, addrSize);
if (bytesSent >= 0)
printf("sent %ld bytes\n", bytesSent);
else
printf("send failed: %s\n", strerror(errno));
closesocket(fd);
return B_OK;
}
// main
int
main(int argc, char** argv)
{
if (argc <= 2) {
fprintf(stderr, "Usage: %s client|server <port>\n", argv[0]);
return 1;
}
int port = atoi(argv[2]);
if (strcmp(argv[1], "client") == 0)
client(port);
else
server(port);
return 0;
}
|

|