UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

  • Index
  • » C
  • » Problem with Sendto() in gcc4.1

#1 2007-09-18 02:58 PM

mehdiizadeh
Member
From: Malaysia
Registered: 2007-09-18
Posts: 10

Re: Problem with Sendto() in gcc4.1

Dear All

I have a program to generate UDP IPv6 packet.
It works on Redhat 9 (gcc3.2.2) but is not working on Fedora core 5 (gcc 4.1)
I think the problem is in "Sendto()" but i dont know how to rectify it.
Any suggestion or help will be very appreciated.

here's the program:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/select.h>
#include <sys/time.h>

int main(int argc, char  **argv)
{
	int pkt_size, pkt_interval_msec;
	struct sockaddr_in6 a;
	int s;
	int ser_num;

	if (argc != 5) {
		fprintf(stderr, "usage: unicast_gen pkt_size 
                                   pkt_interval_msec addr udp_port\n");
		exit(-1);
	}
	
	pkt_size = atoi(argv[1]);
	if (pkt_size < 20 || pkt_size > 1400) {
		fprintf(stderr, "packet size must be at least 20 bytes, and 
                                                 at most 1400 bytes!\n");
		exit(-1);
	}
	
	pkt_interval_msec = atoi(argv[2]);
	if (pkt_interval_msec <= 0) {
		fprintf(stderr, "packet interval must be greater than 0!\n");
		exit(-1);
	}

	s = socket(AF_INET6, SOCK_DGRAM, 0);
	if (s < 0) {
		perror("socket");
		exit(-1);
	}
	// filling sendto address struct
	memset(&a, 0, sizeof(a));
	a.sin6_port = htons(atoi(argv[4]));
	if (inet_pton(AF_INET6, argv[3], a.sin6_addr.s6_addr) <= 0) {
		perror("inet_pton");
		exit(-1);
	}

	for (ser_num = 0; ; ser_num++) {
		struct timeval t, now;
		char buffer[1400];
		t.tv_sec = pkt_interval_msec / 1000;
		t.tv_usec = (pkt_interval_msec * 1000) % 1000000;
		
		//Put serial no and current time as payload in every packet
		gettimeofday(&now, NULL);
		memset(buffer, 0, 1400);
		sprintf(buffer, "%d", ser_num);
		sprintf(buffer+8, "%ld.%06ld", now.tv_sec, now.tv_usec);
		
	    if (sendto(s,buffer,1400,0,(struct sockaddr*)&a,sizeof(a))==-1)
                          {
			fprintf(stderr, "Couldn't send packet\n");
		}
		
		// wait pkt_interval time beofre sending next packet
		select(0, NULL, NULL, NULL, &t);
	}
}

Thanks in Advance
A.Mehdizadeh

Offline

#2 2007-09-18 04:20 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,230

Re: Problem with Sendto() in gcc4.1

Shouldn't

inet_pton(AF_INET6, argv[3], a.sin6_addr.s6_addr)

be:

inet_pton(AF_INET6, argv[3], &a.sin6_addr)

A good check to see if it's correct would be to call
inet_ntop() and print out the address.

Offline

#3 2007-09-18 05:52 PM

mehdiizadeh
Member
From: Malaysia
Registered: 2007-09-18
Posts: 10

Re: Problem with Sendto() in gcc4.1

Thanks i3839
Same problem, i changed it but still couldn't to send packet.
I add this line and it prints the contents of argv[1],argv[2], argv[3] and argv[4].
        printf("%s %s %s %s", argv[1], argv[2], argv[3], argv[4]);

Any suggestion?

Offline

#4 2007-09-18 06:00 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,230

Re: Problem with Sendto() in gcc4.1

Yes, use perror() instead of fprintf and show us the error message.

Offline

#5 2007-09-18 06:16 PM

mehdiizadeh
Member
From: Malaysia
Registered: 2007-09-18
Posts: 10

Re: Problem with Sendto() in gcc4.1

here is the output with perror():

[root@localhost unicast generator]# ./unicast_gen 100 1000 ::1 9999
stderr: Destination address required
stderr: Destination address required
stderr: Destination address required
stderr: Destination address required
stderr: Destination address required
stderr: Destination address required
stderr: Destination address required
stderr: Destination address required
stderr: Destination address required
stderr: Destination address required

Offline

#6 2007-09-18 07:27 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,826
Website

Re: Problem with Sendto() in gcc4.1

Oh, man, it took me forever to find that trivial little mistake!  I can't believe it didn't
smack me in the face immediately!  You forgot one important line:

a.sin6_family = AF_INET6;

So, your sockaddr was marked as AF_UNSPEC, which is why it thought there was
no address specified...  If it worked on previous systems, then you were just lucky,
and they were a bit sloppy...  In any case, it would've been a kernel change that bit
you, not anything GCC-related...

Offline

#7 2007-09-19 01:00 PM

mehdiizadeh
Member
From: Malaysia
Registered: 2007-09-18
Posts: 10

Re: Problem with Sendto() in gcc4.1

Hi Robseace

Thanks alot :smile: , you were right.
I added that line, now is working.
I really appreciate your help and guide from i3839.

Regards
A.Mehdizadeh

Offline

#8 2016-11-06 10:23 AM

punithkumar
Member
Registered: 2016-11-06
Posts: 1

Re: Problem with Sendto() in gcc4.1

Hi Guys,

Also found similar bug in my case while using busybox-1.17.1.
Great and very helpful to narrow the problem

Regards,
Punith Kumar

Offline

  • Index
  • » C
  • » Problem with Sendto() in gcc4.1

Board footer

Powered by FluxBB