|
Home > Archive > Unix Programming > April 2006 > windows to linux udp listener
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
windows to linux udp listener
|
|
| danjones.email@gmail.com 2006-04-02, 7:42 pm |
| I have a program that i simplified so that it only receives a udp
multicast and prints it. It works fine on windows, but it won't work
in linux. In linux it looks like it runs fine, but nothing is
received. Is there something I am missing in linux? I have spent a
lot of time trying to figure this out and I would appreciate any
suggestions on what i can do to get it working. The code is included
below.
#include <stdio.h>
#include <errno.h>
#include <sys/socket.h>
#include <resolv.h>
int main(int count, char *strings[])
{ int sd,len;
struct in_addr *phost;
struct ip_mreq mreq;
struct sockaddr_in addr;
struct sockaddr_in fromaddr;
int port;
int rc;
int addr_len = sizeof(addr);
char buffer[8192];
int bytes;
if ( count != 4 )
{
printf("usage: %s <multicastip> <multicastport>
<bindip>\n", strings[0]);
exit(1);
}
printf("Creating socket\n");
port = atoi(strings[2]);
sd = socket(PF_INET, SOCK_DGRAM, 0);
const int unused = 1;
if (setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, (char *)&unused,
sizeof(unused)) == -1)
{
perror("reuse");
}
const int optval = 0;
if (setsockopt(sd, IPPROTO_IP, IP_MULTICAST_LOOP, (const
void*)&optval, sizeof(int)) == -1)
{
perror("loop");
}
int sendbufsize = 0;
int rcvbufsize = 0;
int size = sizeof(sendbufsize);
if (getsockopt(sd, SOL_SOCKET, SO_SNDBUF, (char
*)&sendbufsize, (socklen_t *)&size) != -1 )
{
sendbufsize *= 3;
if (setsockopt(sd, SOL_SOCKET, SO_SNDBUF, (char
*)&sendbufsize, sizeof(sendbufsize)) == -1)
{
perror("sendbuf");
}
}
if (getsockopt(sd, SOL_SOCKET, SO_RCVBUF, (char *)&rcvbufsize,
(socklen_t *)&size) != -1 )
{
rcvbufsize *= 1250;
if (setsockopt(sd, SOL_SOCKET, SO_RCVBUF, (char
*)&rcvbufsize, sizeof(rcvbufsize)) == -1)
{
perror("recvbuf");
}
}
bzero(&addr, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = inet_addr(strings[3]);
phost = &addr.sin_addr;
len = sizeof(mreq);
if (setsockopt(sd, IPPROTO_IP, IP_MULTICAST_IF, (char *)phost,
sizeof(struct in_addr)) == -1)
{
perror("interface");
}
unsigned char ttl = 128;
if (setsockopt (sd, IPPROTO_IP, IP_MULTICAST_TTL, (const void
*)&ttl, sizeof(ttl)) == -1)
{
perror("ttl");
}
printf("Binding socket to %s\n", strings[3]);
if ( bind(sd, (struct sockaddr*)&addr, sizeof(addr)) != 0 )
perror("bind");
struct ip_mreq sub;
sub.imr_multiaddr.s_addr = inet_addr( strings[1] );
sub.imr_interface.s_addr = inet_addr( strings[3] );
if( setsockopt(sd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char*)&sub,
sizeof(struct ip_mreq)) == -1)
{
perror("Error in joining Multicast group");
}
printf("Waiting for messages on %s port %s bound to %s\n",
strings[1],strings[2], strings[3]);
while (1)
{
bzero(&buffer, sizeof(buffer));
bytes = recvfrom(sd, (char *)buffer, 8191, 0, (struct
sockaddr*)&fromaddr, (int *)&addr_len);
printf("Received [%s]\n", buffer);
}
close(sd);
}
Thanks,
Dan
| |
| Ulrich Eckhardt 2006-04-02, 7:42 pm |
| danjones.email@gmail.com wrote:
> const int unused = 1;
> if (setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, (char *)&unused,
> sizeof(unused)) == -1)
Don't use a constant here, even though the setsockopt() interface sucks in
terms of const correctness. The compiler might do some weird things
knowing that any legal program can never (even not with the cast!) change
it - but I'm just guessing here.
> addr.sin_addr.s_addr = inet_addr(strings[3]);
> sub.imr_multiaddr.s_addr = inet_addr( strings[1] );
> sub.imr_interface.s_addr = inet_addr( strings[3] );
Check the returnvalue of these against INADDR_NONE. I have seen some of
these even resolve hostnames on some(!) win32 systems even though they are
only documented to convert between dotted quad and numeric IP address.
Uli
--
http://www.erlenstar.demon.co.uk/unix/
| |
| danjones.email@gmail.com 2006-04-02, 7:42 pm |
| Thanks for the suggestion, unfortunately neither one fixed it.
|
|
|
|
|