Click here to Skip to main content
15,919,749 members
Home / Discussions / C#
   

C#

 
GeneralTesting remote object connection Pin
Skynyrd7-Dec-04 2:57
Skynyrd7-Dec-04 2:57 
Questiontooltip in notifyicon ? Pin
kendao7-Dec-04 2:26
kendao7-Dec-04 2:26 
Questionbackground imag in treeview ? Pin
kendao7-Dec-04 2:14
kendao7-Dec-04 2:14 
AnswerRe: background imag in treeview ? Pin
Dave Kreskowiak7-Dec-04 5:18
mveDave Kreskowiak7-Dec-04 5:18 
GeneralA problem with changing Form size Pin
sannak7-Dec-04 1:46
susssannak7-Dec-04 1:46 
GeneralRe: A problem with changing Form size Pin
Daniel Turini7-Dec-04 4:32
Daniel Turini7-Dec-04 4:32 
GeneralRe: A problem with changing Form size Pin
Dave Kreskowiak7-Dec-04 5:12
mveDave Kreskowiak7-Dec-04 5:12 
GeneralProblems with multicast Pin
Anup_Shankar7-Dec-04 1:34
Anup_Shankar7-Dec-04 1:34 
Hi

I hit this article which i was looking for.

Iam currently stuck in writing a Simple Multicast application. I would appreciate if any of you could trace out the exact problem.
I would like to know is there any problem with code, or we need to check some external settings. please also let me know how to do that.

I have pasted the code below:

Sender Code:
------------
#define DESTINATION_MCAST "235.6.7.8"
//#define DESTINATION_MCAST "224.0.0.2"

#define DESTINATION_PORT 2345
#define BUFSIZE 2000



void sendMulticast()
{

int nRet = 0, nSize = 0, nOptVal = 0;
SOCKET hSock;
char achInBuf[BUFSIZE];
struct sockaddr_in destAddr, socketLocalAddress;
u_short nSourcePort = 0;
struct ip_mreq stIpMreq;

/* create a datagram (UDP) socket */
hSock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);

int error = WSAGetLastError();

if (hSock == INVALID_SOCKET)
{
printf ("Socket creation failed \n");
return;
}


// Bind the socket

// NOTE: Normally, we wouldn't need to call bind unless we were
// assigning a local port number explicitly (naming the socket),
// however Microsoft requires that a socket be bound before it
// can join a multicast group with setsockopt() IP_ADD_MEMBERSHIP
// (or fails w/ WSAEINVAL).

socketLocalAddress.sin_family = AF_INET;
socketLocalAddress.sin_addr.s_addr = htonl(INADDR_ANY); // any interface
socketLocalAddress.sin_port = 0; // any port


nRet = bind(hSock, (struct sockaddr*) &socketLocalAddress,
sizeof(socketLocalAddress));


if (nRet == SOCKET_ERROR)
{

printf ("Bind call failed \n");
}


// Join the multicast group

stIpMreq.imr_multiaddr.s_addr = inet_addr(DESTINATION_MCAST); /* group addr */
stIpMreq.imr_interface.s_addr = htonl(INADDR_ANY); /* use default */


nRet = setsockopt (hSock, IPPROTO_IP, IP_ADD_MEMBERSHIP,
(char FAR *)&stIpMreq, sizeof (stIpMreq));


if (nRet == SOCKET_ERROR)
{

printf ("Add membership option failed \n");
}

nOptVal = 32;
nRet = setsockopt (hSock, IPPROTO_IP, IP_MULTICAST_TTL,
(char FAR *)&nOptVal, sizeof(int));

error = WSAGetLastError();

if (nRet == SOCKET_ERROR)
{
printf (" set Sock Opt failed \n");
return;
}


/* disable loopback of multicast datagrams we send, since the
* default--according to Steve Deering--is to loopback all
* datagrams sent on any interface which is a member of the
* destination group address of that datagram.
*/



//By disabling this option we avoid getting the multicast packet back.

nOptVal = FALSE;
nRet = setsockopt(hSock, IPPROTO_IP, IP_MULTICAST_LOOP,
(char FAR *)&nOptVal, sizeof(int));

if (nRet == SOCKET_ERROR)
{

printf ("multicast loop option not supported ");

}

/* increase the IP TTL from the default of one to 64, so our
multicast datagrams can get off of the local network
*/



/*
Initialize the Destination Address structure
this is the address we send the multicast packet to.
*/

destAddr.sin_family = AF_INET;
destAddr.sin_addr.s_addr = inet_addr (DESTINATION_MCAST);
destAddr.sin_port = htons (DESTINATION_PORT);

//Init the buffer we send
memset (achInBuf,'\0',sizeof(achInBuf));
strcpy(achInBuf,"Saying Hi To You");


/*
We don't have to register for sending,, any host can send a multicast
packet, expect that the receiver one who registers only will get it.
*/
for (int nIndex = 1; nIndex <= 5; nIndex++)
{

nRet = sendto (hSock, (char FAR *)achInBuf,
strlen(achInBuf), 0,
(struct sockaddr FAR *) &destAddr,
sizeof(destAddr));

Sleep(2000);

if (nRet == SOCKET_ERROR)
{
printf (" Failed in send to \n");
return;
}

printf ("Sent the %d multicast packet %s\n",nIndex, achInBuf);
}
closesocket(hSock);


}





RECEIVER CODE
-------------

#define DESTINATION_MCAST "235.6.7.8"
//#define DESTINATION_MCAST "224.0.0.2"

#define DESTINATION_PORT 2345
#define BUFSIZE 2000


void receiveMultiCast()
{

int nRet = 0, nSize = 0, nOptVal = 0;
SOCKET hSock;
char achInBuf[BUFSIZE];
struct sockaddr_in srcAddr, destAddr;
u_short nSourcePort = 0;
struct ip_mreq stIpMreq;


/* get a datagram (UDP) socket */
hSock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);

int error = WSAGetLastError();

if (hSock == INVALID_SOCKET)
{
printf ("Socket creation failed \n");
return;
}


#if 0


hostent* remoteHost;
unsigned int addr;
int namelen = sizeof(destAddr);



addr = inet_addr("172.21.37.109");
remoteHost = gethostbyaddr((char *) &destAddr.sin_addr.s_addr, 4, AF_INET);

// printf("host name %s",remoteHost->h_name);

int ret = getsockname(
hSock,
(struct sockaddr FAR *)&destAddr,
&namelen
);

error = WSAGetLastError();

remoteHost = gethostbyaddr((char *) &destAddr.sin_addr.s_addr, 4, AF_INET);


printf("host name %s",remoteHost->h_name);
#endif

//Winsock requires us to bind/name the socket before we become a member of group.
srcAddr.sin_family = AF_INET;
srcAddr.sin_port = htons (DESTINATION_PORT);
srcAddr.sin_addr.s_addr = htonl (INADDR_ANY);

nRet = bind (hSock,
(struct sockaddr FAR *)&srcAddr,
sizeof(srcAddr));
error = WSAGetLastError();

if (nRet == SOCKET_ERROR)
{

printf ("Bind call failed \n");

}

/* join the multicast group we want to receive datagrams from */
//Here initialize the group which we wish to join..

stIpMreq.imr_multiaddr.s_addr = inet_addr (DESTINATION_MCAST); /* group addr */
stIpMreq.imr_interface.s_addr = htonl (INADDR_ANY); /* use default */

//This calls creates and adds us as a member.
nRet = setsockopt (hSock, IPPROTO_IP, IP_ADD_MEMBERSHIP,
(char FAR *)&stIpMreq, sizeof (stIpMreq));

error = WSAGetLastError();

if (nRet == SOCKET_ERROR)
{
printf ("Set socket option failed \n");
return;
}


printf("\n Waiting for data \n");

for (int nIndex = 1; nIndex <= 5; nIndex++)
{

/* Recv the available data */
nSize = sizeof(destAddr);
nRet = recvfrom (hSock, (char FAR *)achInBuf,
BUFSIZE, 0,
(struct sockaddr *) &destAddr, &nSize );

if (nRet == SOCKET_ERROR)
{
printf ("receive from failure \n");

}
printf ("Received the %d Packet \n",nIndex);

achInBuf[nSize] = '\0';

char *recvIpAddressPtr = NULL;

recvIpAddressPtr = inet_ntoa(destAddr.sin_addr);

printf(" Received message::%s from IP:: %s \n",achInBuf,recvIpAddressPtr);

//drop the membership
} // for (int nIndex = 1; nIndex <= 5; nIndex++)


nRet = setsockopt (hSock, IPPROTO_IP, IP_DROP_MEMBERSHIP,
(char FAR *)&stIpMreq, sizeof (struct ip_mreq));

error = WSAGetLastError();

if (nRet == SOCKET_ERROR)
{
printf ("Drop membership socket option failed \n");
return;
}

closesocket(hSock);

}
GeneralRe: Problems with multicast Pin
Charlie Williams7-Dec-04 4:40
Charlie Williams7-Dec-04 4:40 
GeneralRe: Problems with multicast Pin
Anup_Shankar7-Dec-04 13:39
Anup_Shankar7-Dec-04 13:39 
GeneralRe: Problems with multicast Pin
Dave Kreskowiak7-Dec-04 5:06
mveDave Kreskowiak7-Dec-04 5:06 
GeneralSocket problem Pin
jtmtv187-Dec-04 1:21
jtmtv187-Dec-04 1:21 
GeneralRe: Socket problem Pin
J4amieC7-Dec-04 2:35
J4amieC7-Dec-04 2:35 
GeneralRe: Socket problem Pin
jtmtv187-Dec-04 11:42
jtmtv187-Dec-04 11:42 
GeneralSerialize Pin
andreas_farnstrand7-Dec-04 0:59
andreas_farnstrand7-Dec-04 0:59 
GeneralRe: Serialize Pin
jtmtv187-Dec-04 1:35
jtmtv187-Dec-04 1:35 
GeneralRe: Serialize Pin
Daniel Turini7-Dec-04 4:29
Daniel Turini7-Dec-04 4:29 
GeneralProcessController Question Pin
realmontanakid7-Dec-04 0:23
realmontanakid7-Dec-04 0:23 
GeneralRe: ProcessController Question Pin
Daniel Turini7-Dec-04 4:22
Daniel Turini7-Dec-04 4:22 
GeneralRe: ProcessController Question Pin
realmontanakid7-Dec-04 4:58
realmontanakid7-Dec-04 4:58 
GeneralStrings Constants Pin
Dirso6-Dec-04 23:51
Dirso6-Dec-04 23:51 
GeneralRe: Strings Constants Pin
Robin Panther7-Dec-04 3:17
Robin Panther7-Dec-04 3:17 
GeneralJIT Pin
Amirzeb6-Dec-04 23:19
Amirzeb6-Dec-04 23:19 
GeneralRe: JIT Pin
benjymous7-Dec-04 0:46
benjymous7-Dec-04 0:46 
GeneralMessage loop Pin
akarwa6-Dec-04 22:52
akarwa6-Dec-04 22:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.