Unix Programming - problem with sendto and recvfrom calls..

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > May 2006 > problem with sendto and recvfrom calls..





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 problem with sendto and recvfrom calls..
xoinki

2006-05-17, 7:16 pm

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;
}

William Ahern

2006-05-17, 7:16 pm

On Wed, 17 May 2006 11:33:37 -0700, xoinki wrote:

> 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..

<snip>
> ther:
> printf("it worked.. %s",buf);

^
Add a linefeed ("\n") here ------------/



Renzj

2006-05-18, 7:21 am

try this:

for(int i=0; i<bytes; i++)
printf("%02x ",buf[i]);
printf("\n");

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com