For Programmers: Free Programming Magazines  


Home > Archive > Unix Programming > September 2005 > Message sender and receiver for unicast+multicast+Broadcast









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 Message sender and receiver for unicast+multicast+Broadcast
Yash

2005-09-20, 7:57 am

Hi,

I am trying to write an application which can send in any mode i.e.
unicast, multicast, Broadcast and get the ACK for every message sent.
In other side receiver should handle any type of incoming message i.e.
unicast, multicast, and broadcast and it should also send ACK every
message sent.

Is Anybody can suggest, what should be the approach.

I have written simple unicast , Multicast and Broadcast programs
seperatly and they are working fine for sending/receiving
strings/files.

I tried to combine all three programs to get all in one application,
But it is not working properly.

Pascal Bourguignon

2005-09-20, 7:57 am

"Yash" <yash_mbm@yahoo.com> writes:

> Hi,
>
> I am trying to write an application which can send in any mode i.e.
> unicast, multicast, Broadcast and get the ACK for every message sent.
> In other side receiver should handle any type of incoming message i.e.
> unicast, multicast, and broadcast and it should also send ACK every
> message sent.
>
> Is Anybody can suggest, what should be the approach.
>
> I have written simple unicast , Multicast and Broadcast programs
> seperatly and they are working fine for sending/receiving
> strings/files.
>
> I tried to combine all three programs to get all in one application,
> But it is not working properly.


Yeah! Things like that happen.


--
__Pascal Bourguignon__ http://www.informatimago.com/

Nobody can fix the economy. Nobody can be trusted with their finger
on the button. Nobody's perfect. VOTE FOR NOBODY.
Yash

2005-09-20, 7:02 pm

i Did'nt understand , what you mean by this.
Is there anybody who can answer more meaning fully.

Pascal Bourguignon

2005-09-20, 7:02 pm

"Yash" <yash_mbm@yahoo.com> writes:

> i Did'nt understand , what you mean by this.
> Is there anybody who can answer more meaning fully.


Be more precise: what doesn't work?
How did you "merge" the three working programs?

--
"What is this talk of "release"? Klingons do not make software
"releases". Our software "escapes" leaving a bloody trail of
designers and quality assurance people in its wake."
Yash

2005-09-21, 3:56 am

OK, Here is my code, I think it will make some sense for you ,so that
you can solve my problem.

My sender program is as follows:

/***---Unicast-Multicast-Broadcast Sender---Sending file --- ****/

#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/errno.h>
#include <netdb.h>
#include <sys/stat.h>
#include<stdlib.h>
#include <netinet/in.h>
#define MAXLINE 1448 /* max text line length */

void IfError(char *errorMessage);

int main(int argc, char *argv[])
{
int i, no_byte_f1, no_byte_f2, no_byte_f3, j, k, l, status, add,
rem1, rem2, rem3,socklen;
int sock_b, sock_u, sock_m;
int f2packets, f1packets, f3packets;
struct sockaddr_in broadcastAddr;
struct sockaddr_in cliaddr, UnicastAddr, MulticastAddr;
struct stat statbuff;
struct in_addr iaddr;
char *broadcastIP;
unsigned short broadcastPort, unicastPort, multicastPort;
unsigned int sendStringLen;
unsigned char ttl =3D 3;
unsigned char one =3D 1;
int broadcastPermission;
char *multicastIP;
char *unicastIP;
char buffer1[MAXLINE], buffer2[MAXLINE], buffer3[MAXLINE];
char store1[3], store2[3], store3[3];
char count;
char *sendString;
FILE *send_fp1, *send_fp2, *send_fp3;
char sendfile[MAXLINE];
const int on=3D1;

if (argc < 6) {
fprintf(stderr,"Usage: %s <broadcast IP Address><broadcast
port> <unicast IP addr> <unicast Port><multicast IP><multicast Port>
\n", argv[0]);
exit(errno);
}

broadcastIP =3D argv[1];
broadcastPort =3D atoi(argv[2]);
unicastIP =3D argv[3];
unicastPort =3D atoi(argv[4]);
multicastIP =3D argv[5];
multicastPort =3Datoi(argv[6]);

/******************** --- Multicast --- ****************************/
printf("\n----------------------Multicast
--------------------------\n");
// set content of struct MulticastAddr and imreq to zero
memset(&MulticastAddr, 0, sizeof(struct sockaddr_in));
memset(&iaddr, 0, sizeof(struct in_addr));

sock_m =3D socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
if ( sock_m < 0 )
IfError("Multicast : socket() failed");
else
printf("\n-------------Multicast socket created--------------\n\n");

if(setsockopt(sock_m, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) =3D=3D
-1)
IfError("Multicast : setsockopt() failed");

MulticastAddr.sin_family =3D AF_INET;
MulticastAddr.sin_port =3D htons(multicastPort);
MulticastAddr.sin_addr.s_addr =3D htonl(INADDR_ANY); // bind socket
to any interface

iaddr.s_addr =3D INADDR_ANY; // use DEFAULT interface

// Set the outgoing interface to DEFAULT
setsockopt(sock_m, IPPROTO_IP, IP_MULTICAST_IF,
&iaddr,sizeof(struct in_addr));
// Set multicast packet TTL to 3; default TTL is 1
setsockopt(sock_m, IPPROTO_IP, IP_MULTICAST_TTL,
&ttl,sizeof(unsigned char));
// send multicast traffic to myself too
status =3D setsockopt(sock_m, IPPROTO_IP, IP_MULTICAST_LOOP, &one,
sizeof(unsigned char));

// set destination multicast address
MulticastAddr.sin_family =3D AF_INET;
MulticastAddr.sin_addr.s_addr =3D inet_addr(multicastIP);
MulticastAddr.sin_port =3D htons(multicastPort);

send_fp3=3Dfopen("test3.doc","r");

if(stat("test3.doc",&statbuff)<0)
IfError("Multicast : stat( ) failed");
printf ("statbuff.st_size =3D %d \n",statbuff.st_size);

f3packets=3Dstatbuff.st_size/MAXLINE;
printf ("f3_packets =3D %d \n", f3packets);

rem3 =3D statbuff.st_size%MAXLINE;
printf("Rem3 =3D %d \n",rem3);

if(rem3 !=3D0 )
f3packets=3Df3packets+1;
printf("no of packets =3D %d\n\n", f3packets);

sprintf(store3,"%d",f3packets);
printf("store3 :%s \n",store3);

socklen =3D sizeof(struct sockaddr_in);
sendto(sock_m, store3, MAXLINE, 0,(struct sockaddr
*)&MulticastAddr, socklen);

printf("\n-----------------Multicast Sending !!!----------------\n");
for(l=3D0;l<f3packets ;l++) {
bzero(buffer3,MAXLINE);
fgets(buffer3, MAXLINE, send_fp3);

no_byte_f3 =3D sendto(sock_m,buffer3,MAXLINE,0,(struct sockaddr
*)&MulticastAddr, socklen);
if (no_byte_f3 < 0)
error("ERROR writing to socket");
}
bzero(buffer3,MAXLINE);

fclose(send_fp3);
shutdown(sock_m, 2);

close(sock_m);

/********************* --- Broadcast --- ***********************/
printf("\n---------------------Broadcast
--------------------------\n");

if ((sock_b =3D socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
IfError("Broadcast : socket() failed");
else
printf("\n-------------Broadcast socket created--------------\n\n");

broadcastPermission =3D 1;
if (setsockopt(sock_b, SOL_SOCKET, SO_BROADCAST, (void *)
& broadcastPermission,sizeof(broadcastPerm
ission)) < 0)
IfError("Broadcast : setsockopt() failed");

bzero(&broadcastAddr,sizeof(broadcastAddr));
broadcastAddr.sin_family =3D AF_INET;
broadcastAddr.sin_addr.s_addr =3D inet_addr(broadcastIP);
broadcastAddr.sin_port =3D htons(broadcastPort);

send_fp2=3Dfopen("test2.doc","r");

if(stat("test2.doc", &statbuff)<0)
IfError("Broadcast : stat() failed");

printf ("statbuff.st_size =3D %d \n",statbuff.st_size);

f2packets=3Dstatbuff.st_size/MAXLINE;
printf ("f2packets1 =3D %d \n",f2packets);

rem2 =3D statbuff.st_size%MAXLINE;
printf("Rem =3D %d \n",rem2);

if(rem2 !=3D 0)
f2packets++;
printf("no of f2-packets =3D %d\n\n", f2packets);

sprintf(store2,"%d",f2packets);
printf("store2 :%s \n",store2);

sendto(sock_b, store2, MAXLINE, 0,(struct sockaddr *)&broadcastAddr,
sizeof(broadcastAddr));

printf("\n-----------------Broadcast : Sending
!!!----------------\n");
for(j=3D0;j<f2packets ;j++) {
bzero(buffer2,MAXLINE);
fgets(buffer2, MAXLINE, send_fp2);

no_byte_f2 =3D sendto(sock_b,buffer2,MAXLINE,0,(struct sockaddr
*)&broadcastAddr, sizeof(broadcastAddr));
if (no_byte_f2 < 0)
IfError("Broadcast : sendto() failed");
}

fclose(send_fp2);

/******************* --- Unicast --- *********************/
printf("\n------------------------Unicast
---------------------------\n");
/* ---creat socket for sending unicast string---*/
if ((sock_u =3D socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
IfError("Unicast : socket() failed");
else
printf("\n-------------Unicast socket created--------------\n\n");


if(setsockopt(sock_u, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) =3D=3D
-1)
IfError("Unicast : setsockopt() failed");

bzero(&UnicastAddr,sizeof(UnicastAddr));
UnicastAddr.sin_family =3D AF_INET;
inet_pton(AF_INET, argv[3], &UnicastAddr.sin_addr);
UnicastAddr.sin_port =3D htons(unicastPort);

send_fp1=3Dfopen("test1.doc","r");
if(stat("test1.doc",&statbuf=E7=9A=9C)<0)
error("file not found\n");

printf ("statbuff.st_size =3D %d \n",statbuff.st_size);

f1packets=3Dstatbuff.st_size/MAXLINE;
printf ("f1packets1 =3D %d \n",f1packets);

rem1 =3D statbuff.st_size%MAXLINE;
printf("Rem =3D %d \n",rem1);

if(rem1 !=3D 0)
f1packets++;
printf("no of f1packets =3D %d\n\n", f1packets);

sprintf(store1,"%d",f1packets);
printf("store1 :%s \n",store1);

sendto(sock_u, store1, MAXLINE, 0,(struct sockaddr
*)&UnicastAddr, sizeof(UnicastAddr));

for(k=3D0;k<f1packets ;k++) {
bzero(buffer1,MAXLINE);
fgets(buffer1, MAXLINE, send_fp1);

no_byte_f1 =3D sendto(sock_u,buffer1,MAXLINE,0,(struct sockaddr
*)&UnicastAddr, sizeof(UnicastAddr));
if (no_byte_f1 < 0)
IfError("Unicast : sendto() failed");
}
bzero(buffer1,MAXLINE);

fclose(send_fp1);
return 0;
}

void IfError(char *errorMessage)
{
perror(errorMessage);
exit(errno);
}



My receiver program is as follows:

/*---Unicast Multicast Broadcast Receiver---*/

#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include<fcntl.h>
#include<sys/errno.h>

#define MAXLINE 1448

void IfError(char *errorMessage);

int main(int argc, char*argv[])
{
int sock_m,sock_b,sock_u;
int nos_packet_f1, nos_packet_f2, nos_packet_f3, count1, count2,
count3;
int status, socklen;
struct sockaddr_in broadcastAddr;
struct sockaddr_in cliaddr, UnicastAddr, MulticastAddr;
struct ip_mreq imreq;
unsigned int broadcastPort, unicastPort, multicastPort;
int flags;

FILE *recv_fp1;
FILE *recv_fp2;
FILE *recv_fp3;
char *multicastIP;
char recvfile_f1[MAXLINE], recvfile_f2[MAXLINE],
recvfile_f3[MAXLINE], recvpacket_f1[MAXLINE], recvpacket_f2[MAXLINE],
recvpacket_f3[MAXLINE];
int recvStringLen;

const int on=3D1;

if (argc<2 || argc>5) {
fprintf(stderr,"Usage: %s <MulticastIP> <MulticastPort>
<Broadcast Port> <UnicastPort> \n", argv[0]);
exit(errno);
}

if(argc =3D=3D 5){
multicastIP =3D argv[1];
multicastPort =3D atoi(argv[2]);
broadcastPort =3D atoi(argv[3]);
unicastPort =3D atoi(argv[4]);
}
else if (argc =3D=3D 3){
multicastIP=3Dargv[1];
multicastPort=3Datoi(argv[2]);
broadcastPort=3Datoi(argv[3]);
}
else{
multicastIP=3Dargv[1];
multicastPort=3Datoi(argv[2]);
}

/************************ --- Multicast --- ***********************/
printf("\n--------------------------Multicast
--------------------\n");
/*set content of struct MulticastAddr and imreq to zero*/
memset(&MulticastAddr, 0, sizeof(struct sockaddr_in));
memset(&imreq, 0, sizeof(struct ip_mreq));

// open a UDP socket
sock_m =3D socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
if ( sock_m < 0 )
IfError("Multicast : socket() failed");
else
printf("\n-------------Multicast socket created--------------\n\n");

MulticastAddr.sin_family =3D AF_INET;
MulticastAddr.sin_port =3D htons(multicastPort); // listen on port
4000
MulticastAddr.sin_addr.s_addr =3D htonl(INADDR_ANY); // bind socket
to any interface

if ( status =3D bind(sock_m, (struct sockaddr *)&MulticastAddr,
sizeof(struct sockaddr_in)) < 0 )
IfError("Multicast : bind() failed");

imreq.imr_multiaddr.s_addr =3D inet_addr(multicastIP);
imreq.imr_interface.s_addr =3D INADDR_ANY; // use DEFAULT interface

// JOIN multicast group on default interface
if(status =3D setsockopt(sock_m, IPPROTO_IP, IP_ADD_MEMBERSHIP,
(const void *)&imreq, sizeof(struct ip_mreq)) < 0)
IfError("Multicast : setsockopt() failed");

socklen =3D sizeof(struct sockaddr_in);
recv_fp3 =3D fopen("receive3.doc", "wb");

/*if ((flags =3D fcntl (sock_m, F_GETFL, 0)) < 0)
IfError("Multicast : F_GETFL error");
flags |=3D O_NONBLOCK;
if (fcntl(sock_m,F_SETFL,fl=E7=9A=9Cgs) < 0)
IfError("Multicast :F_SETFL error");*/

if(nos_packet_f3 =3D
recvfrom(sock_m,recvpacket_f3,sizeof(rec
vpacket_f3),0,NULL,0) < 0)
IfError("Multicast packet count : recvfrom() failed");
//nos_packet_f3 =3D
recvfrom(sock_m,recvpacket_f3,sizeof(rec
vpacket_f3),0,NULL,0);
count3 =3Datoi(recvpacket_f3);
printf("\nMulticast packet count : %d", count3);

printf("\n-----------------Multicast Data Receiving
!!!----------------\n");
while(count3--) {
bzero(recvfile_f3,MAXLINE);
if (recvfrom(sock_m,recvfile_f3,MAXLINE,0,N
ULL,0) < 0)
IfError("Multicast : recvfrom() failed");
// recvfrom(sock_m,recvfile_f3,MAXLINE,0,NU
LL,0);

fputs(recvfile_f3, recv_fp3);
printf(recvfile_f3);
}
fclose(recv_fp3);
shutdown(sock_m, 2);

close(sock_m);

if(argc =3D=3D 3)
exit(0);

/************************ --- Broadcast ---
******************************/
printf("\n-------------------------Broadcast --------------------\n");
if ((sock_b =3D socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
IfError("Broadcast : socket() failed");
else
printf("\n-----------------Broadcast Socket Created
!!----------------\n");

bzero(&broadcastAddr, sizeof(broadcastAddr));
broadcastAddr.sin_family =3D AF_INET;
broadcastAddr.sin_addr.s_addr =3D htonl(INADDR_ANY);
broadcastAddr.sin_port =3D htons(broadcastPort);

if (bind(sock_b, (struct sockaddr *) &broadcastAddr,
sizeof(broadcastAddr)) =3D=3D -1)
IfError("Broadcast : bind() failed");

recv_fp2 =3D fopen("receive2.doc", "wb");

/*if ((flags =3D fcntl (sock_b, F_GETFL, 0)) < 0)
IfError("Multicast : F_GETFL error");
flags |=3D O_NONBLOCK;
if (fcntl(sock_b,F_SETFL,flags) < 0)
IfError("Multicast :F_SETFL error");*/

/* to recv no of packets */
if(nos_packet_f2 =3D
recvfrom(sock_b,recvpacket_f2,sizeof(rec
vpacket_f2),0,NULL,0) <0)
IfError("Broadcast packet count :recvfrom() failed");
//nos_packet_f2 =3D
recvfrom(sock_b,recvpacket_f2,sizeof(rec
vpacket_f2),0,NULL,0);
count2 =3Datoi(recvpacket_f2);
printf("\nBroadcast packet count=3D%d\n",count2);
printf("\n-----------------Receiving broadcast data
!!!----------------\n");
while(count2--) {
bzero(recvfile_f2,MAXLINE);
if (recvfrom(sock_b,recvfile_f2,MAXLINE,0,N
ULL,0) < 0)
IfError("Broadcast : recvfrom() failed");
// recvfrom(sock_b,recvfile_f2,MAXLINE,0,NU
LL,0);

fputs(recvfile_f2,recv_fp2);
printf(recvfile_f2);
}
printf("\n");
fclose(recv_fp2);
close(sock_b);

if(argc =3D=3D 4)
exit(0);

/*********************** --- Unicast --- ***************************/
printf("\n--------------------Unicast
------------------------------\n");
if ((sock_u =3D socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
IfError("Unicast : socket() failed");
else
printf("\n-----------------Unicast Socket Created
!!----------------\n");

bzero(&UnicastAddr, sizeof(UnicastAddr));
UnicastAddr.sin_family =3D AF_INET;
UnicastAddr.sin_addr.s_addr =3D INADDR_ANY;
UnicastAddr.sin_port =3D htons(unicastPort);

if(bind(sock_u, (struct sockaddr*) &UnicastAddr, sizeof(UnicastAddr))
=3D=3D -1)
IfError("Unicast : bind() failed");

recv_fp1 =3D fopen("receive1.doc", "wb");

/*if ((flags =3D fcntl (sock_u, F_GETFL, 0)) < 0)
IfError("Multicast : F_GETFL error");
flags |=3D O_NONBLOCK;
if (fcntl(sock_u,F_SETFL,flags) < 0)
IfError("Multicast :F_SETFL error");*/

/* to recv no of packets */
if(nos_packet_f1 =3D
recvfrom(sock_m,recvpacket_f1,sizeof(rec
vpacket_f1),0,NULL,0) < 0)
IfError("unicast packet count : recvfrom() failed");
//nos_packet_f1 =3D
recvfrom(sock_m,recvpacket_f1,sizeof(rec
vpacket_f1),0,NULL,0);

count1 =3Datoi(recvpacket_f1);
printf("\n Unicast packet count=3D%d \n",count1);

printf("\n---------Receiving Unicast data!!!--------\n");
while(count=E7=9A=9C--) {
bzero(recvfile_f1,MAXLINE);
if (recvfrom(sock_u,recvfile_f1,MAXLINE,0,N
ULL,0) < 0)
IfError("Unicast : recvfrom() failed");
// recvfrom(sock_u,recvfile_f1,MAXLINE,0,NU
LL,0);
fputs(recvfile_f1,recv_fp1);
printf(recvfile_f1);
}
printf("\n");
fclose(recv_fp1);
=09
close(sock_u);

}


Now Can you suggest me some solution ?

Barry Margolin

2005-09-21, 9:57 pm

In article <1127291771.811611.234830@g44g2000cwa.googlegroups.com>,
"Yash" <yash_mbm@yahoo.com> wrote:

> OK, Here is my code, I think it will make some sense for you ,so that
> you can solve my problem.


All your underscores are missing. I doubt that's your problem, I think
you need a better newsreader that doesn't molest your posts.

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
Ulrich Hobelmann

2005-09-22, 3:57 am

Barry Margolin wrote:
> In article <1127291771.811611.234830@g44g2000cwa.googlegroups.com>,
> "Yash" <yash_mbm@yahoo.com> wrote:
>
>
> All your underscores are missing. I doubt that's your problem, I think
> you need a better newsreader that doesn't molest your posts.


If you're referring to field names like "sin_addr", then those appear
just fine over here...

--
My mouth says the words, my brain is thinking monstertrucks.
Joey (Friends)
Yash

2005-09-23, 7:58 am


Ulrich Hobelmann wrote:
> Barry Margolin wrote:
>
> If you're referring to field names like "sin_addr", then those appear
> just fine over here...
>
> --
> My mouth says the words, my brain is thinking monstertrucks.
> Joey (Friends)

Hi
I dont know, why are you discussing underscores.
It is very much visable and as far as my problem is concerned nobody
answered me yet.

Yash

2005-09-23, 7:58 am

I gave you code also, but you did'nt appeared again !!

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com