problem with sendto and recvfrom calls..

problem with sendto and recvfrom calls..

Post by xoink » Thu, 18 May 2006 21:08:58



hi experts,
i am trying to send an array of data and print it after recieving from
recvfrom call. the program says tat it has successfully recieved some
data but it is not printing anythin please help me..
thanking u in advance..
XOINKI..

code:

#include <fcntl.h>
#include <errno.h>
#include <sys/socket.h>
#include <resolv.h>
#include <netdb.h>
#include <netinet/in.h>
#include <netinet/ip_icmp.h>
#include <sys/cdefs.h>
#include <sys/types.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>

int main()
{
int val=1,bytes;
struct sockaddr_in addr;
struct protoent *proto;
struct hostent *hname;
int sd;
char pckt[20];
char buf[40];
hname=gethostbyname("192.168.15.2");
proto=getprotobyname("OSPFIGP");
strcpy(pckt,"hello");
printf("the %s\n",pckt);
bzero(&addr, sizeof(addr));
                addr.sin_family = hname->h_addrtype;
                addr.sin_port = 0;
                addr.sin_addr.s_addr = *(long*)hname->h_addr;

                sd = socket(PF_INET, SOCK_RAW, proto->p_proto);

                if ( sd < 0 )
        {
                perror("socket");
                return 0;
        }

        if ( setsockopt(sd, SOL_SOCKET, SO_BROADCAST, &val, sizeof(val)) != 0)
                perror("Set TTL option");

                if ( sendto(sd, &pckt, sizeof(pckt), 0, (struct sockaddr*)&addr,
sizeof(addr)) <= 0 )
                        perror("sendto");
        for(;;)
                {
                int len=sizeof(addr);
                sleep(2);
                bytes = recvfrom(sd, buf, sizeof(buf), 0, (struct sockaddr*)&addr,
&len);

                if(bytes>0)
                {
                buf[bytes]=(char)NULL;
                goto ther;
                //printf("it worked %s\n",buf);
                }
                else
                printf("didn work\n");
                }
                ther:
                printf("it worked.. %s",buf);
                return 0;
                }

 
 
 

1. Q: Linux sendto/recvfrom problems

I've been trying to get two Linux (2.1.7) boxes to communicate using IP
broadcast, but I've been unable to make it work. The central parts of the
two programs are quoted below; any suggestions are much welcomed :-)

Q1: Why does the SIOCGIFADDR in program 1 return a completely bogus IP
address? If I insert a SIOCGIFBRDADDR just before it as indicated by the
comment everything works OK.

Q2: Using a packet sniffer on the computer running program 1, I've been
able to determine that program 2 sends out packets the way I intended it
to; they are broadcast as expected. Still program 1 never receives
anything at all, and strace'ing it reveals no errors. Replacing the
sendto address with the address of computer 1 makes everything work.
What am I missing here?

TIA
Mikkel Lauritsen

Computer 1 - recieve:

  ip_socket = socket(AF_INET,SOCK_DGRAM,0);

  strcpy(if_data.ifr_name,dev);
  /* ioctl(ip_socket,SIOCGIFBRDADDR,&if_data); */
  ioctl(ip_socket,SIOCGIFADDR,&if_data);
  ((struct sockaddr_in *)&if_data.ifr_addr)->sin_port = htons(6666);
  bind(ip_socket,&(if_data.ifr_addr),sizeof(struct sockaddr));

  fromlen = sizeof(struct sockaddr);
  recvfrom(ip_socket,&buffer,sizeof(struct packet),0,
           (struct sockaddr *)&source,&fromlen);

Computer 2 - transmit:

  ip_socket = socket(AF_INET,SOCK_DGRAM,0);

  setsockopt(ip_socket,SOL_SOCKET,SO_BROADCAST,&opt,sizeof(int));

  strcpy(if_data.ifr_name,dev);

  ioctl(ip_socket,SIOCGIFBRDADDR,&if_data);
  broadcast = *(struct sockaddr_in *)&if_data.ifr_broadaddr;

  ioctl(ip_socket,SIOCGIFADDR,&if_data);
  ((struct sockaddr_in *)&if_data.ifr_addr)->sin_port = htons(6666);
  bind(ip_socket,&(if_data.ifr_addr),sizeof(struct sockaddr));

  broadcast.sin_port = htons(6666);
  sendto(ip_socket,(void *)&buffer,sizeof(struct packet),0,
         (struct sockaddr *)&broadcast,sizeof(struct sockaddr));

2. Lan browsing with Konqueror

3. sockets - latency between sendto() - recvfrom()

4. Lotus CC:Mail

5. recvfrom() & sendto()

6. Where can I get Java v. 1.1.8?

7. recvfrom makes sendto slower

8. No Serial Ports!

9. bug in link-level recvfrom/sendto

10. sendto( ) and recvfrom( ) Broadcasting

11. connected UDP socket - read/write or sendto/recvfrom is faster?

12. Impact of bind() on recvfrom() and sendto()

13. SunOS sendto() && recvfrom()