Re: Multicast client for UDP doesn't work?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



On Oct 2, 1:45 pm, Srikanth <s...@xxxxxxxxxx> wrote:
Hi
I'm a newbie to multicast programming, and I have to build a client
for a UDP multicast. However, when I created my basic client (from the
MSDN examples), I seem to have a problem... Even when the server isn't
running, my program manages to connect to some server, but it never
receives any data (even when the server is connected)... I'll give the
server code (which I am not allowed to modify), and the main parts of
my code. Could someone please tell me what I am doing wrong?

Also, the server is a part of a Win32 app, but I am trying to build a
quick client, to receive data, as a command line (console) app. Would
this be a problem? The server address and port are 224.0.0.2 and 7476
respectively. However, as I've mentioned above, it doesn't matter what
server address I set in my client - it seems to connect for any
value.

============================================================
Server Initialization Code:

int MulticastServer::open( char * address, int port )
{
unsigned char ttl = 1;
struct hostent *h;
#ifdef WIN32
WORD wVersionRequested;
WSADATA wsaData;

wVersionRequested = MAKEWORD( 2, 0 );
if(WSAStartup( wVersionRequested, &wsaData ) != 0) {
printf( "Multicast::open(), Windows Socket Initialization Error
\n" );
return 0;
}
#endif

h = gethostbyname( address );
if (h == NULL) {
printf( "Multicast::open(), gethostbyname failed '%s'\n", address );
return 0;
}

serverAddr.sin_family = h->h_addrtype;
memcpy((char *) &serverAddr.sin_addr.s_addr, h->h_addr_list[0],h->h_length);

serverAddr.sin_port = htons( port );

/* check if dest address is multicast */
if (!IN_MULTICAST( ntohl( serverAddr.sin_addr.s_addr ) )) {
printf( "Multicast::open(), address '%s' is not multicast \n",
inet_ntoa( serverAddr.sin_addr ) );
return 0;
}

/* create socket */
sd = socket( AF_INET, SOCK_DGRAM, 0 );
if (sd < 0) {
perror( "Multicast::open(), socket" );
return 0;
}

/* bind any port number */
clientAddr.sin_family = AF_INET;
clientAddr.sin_addr.s_addr = htonl( INADDR_ANY );
clientAddr.sin_port = htons(0);
if ( bind( sd, (struct sockaddr *) &clientAddr, sizeof(clientAddr) )
< 0 ) {
perror( "Multicast::open(), bind" );
return 0;
}

if (setsockopt( sd, IPPROTO_IP, IP_MULTICAST_TTL, (char*)&ttl,
sizeof(ttl) ) < 0) {
perror( "Multicast::open(), setsockopt, TTL" );
return 0;
}

printf("INFO: ready to send data on multicast group '%s' (%s)\n",
h->h_name, inet_ntoa( *(struct in_addr *) h-

h_addr_list[0]) );

return 1;}

==========================================================================
Client Code, modification of MSDN example:

#include <stdio.h>
#include "winsock2.h"

void main() {

// Initialize Winsock.
WSADATA wsaData;
int iResult = WSAStartup( MAKEWORD(2,2), &wsaData );
if ( iResult != NO_ERROR )
printf("Error at WSAStartup()\n");

// Create a socket.
SOCKET m_socket;
m_socket = socket( AF_INET, SOCK_DGRAM, 0 );

if ( m_socket == INVALID_SOCKET ) {
printf( "Error at socket(): %ld\n", WSAGetLastError() );
WSACleanup();
return;
}

// Connect to a server.
sockaddr_in clientService;

clientService.sin_family = AF_INET;
clientService.sin_addr.s_addr = inet_addr( "224.0.0.2" );
clientService.sin_port = htons( 7476 );

if ( connect( m_socket, (SOCKADDR*) &clientService,
sizeof(clientService) ) == SOCKET_ERROR) {
printf( "Failed to connect.\n" );
WSACleanup();
return;
}

// Receive data.
int bytesRecv = SOCKET_ERROR;
char recvbuf[32] = "";

sockaddr s;
int plen;
bytesRecv = recvfrom( m_socket, recvbuf, 32, 0, &s, &plen );

if ( bytesRecv == 0 || bytesRecv == WSAECONNRESET ) {
printf( "Connection Closed.\n");
}
printf( "Bytes Recv: %ld\n", bytesRecv );

return;

}

==========================================================================

Thanks a lot for any help
Srikanth

Hi Srikanth,

I am not sure why you are trying to connect to the server. I think
you may have a mis-conception of what multicast is all about.

The whole point of multicast is so that a single UDP stream can be
sent from the server and received by many clients.

There is no need to connect to a server. You need to create a
multicast request structure (struct mc_req) and request a join to the
mutlicast address.

Once you are done, you need to un-join the address.

It is probably better for you to read all about it:

http://ntrg.cs.tcd.ie/undergrad/4ba2/multicast/antony/index.html

At the bottom there are listener and sender examples... take a
look.... They are not the the cleanest to learn from, but its what i
could find real fast...

Hope this helps...

Tom Handal

.



Relevant Pages

  • Re: suid bit files + securing FreeBSD (new program: LockDown)
    ... I found the design maybe LockDown or your IDS could use ... So you need at least one CFC server, ... the client boots, it will just use the files it already have and update ... The multicast address the client is a member of. ...
    (FreeBSD-Security)
  • Re: Unicast works, but Multicast will not
    ... RTP is not involved with multicast broadcasts. ... packets flowing from your video server to your client PC. ... The server should always be spitting out multicast packets, ... I am using Windows Media encoder in a PUSH ...
    (microsoft.public.windowsmedia.server)
  • Re: NTS multicast response on IPv6
    ... > Please understand that is not how multicast works. ... > passive and only receive multicast packets from the server (modulo the ... Once the authentication is set up the client start listening on ... >> like ntpq to do my tests? ...
    (comp.protocols.time.ntp)
  • Re: NTS multicast response on IPv6
    ... NTP client to discover by multicast a NTP server on my IPv6 network. ...
    (comp.protocols.time.ntp)
  • problem with the udp socket
    ... The sockets are created and I am able to bind also both the client and the ... server apps to the port and the ethereal shows that data is being transferred ... int s, i, slen=sizeof; ... WSADATA wsaData; ...
    (microsoft.public.pocketpc.developer.networking)