LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   <sys/time.h> gettimeofday conflict with Eth socket sendto (https://www.linuxquestions.org/questions/programming-9/sys-time-h-gettimeofday-conflict-with-eth-socket-sendto-811941/)

legendbb 06-03-2010 10:11 AM

<sys/time.h> gettimeofday conflict with Eth socket sendto
 
Whenever I enable gettimeofday in my code, previous working sendto(raw_eth, buffer, ...)

stopped working and

Error: Bad file descriptor.

Why is that?

the only thing I'm sure is it's not name pollution. Since all the variable defined in main() works if I make the wrap of gettimeofday an empty function.

Thanks,

posixculprit 06-03-2010 11:01 AM

In the absence of code the only explanation I can come up with is: wizards did it! If you don't wish to share the code of your program, write a different, smaller program which does little more but exemplify the problem you are facing; and post that program instead.

paulsm4 06-03-2010 11:06 AM

Hi -

It sounds like you might be calling "gettimeofday()" incorrectly?

Does your code resemble this:
Code:

#include <sys/time.h>
...
timeval t;
gettimeofday(&t,NULL);
...


legendbb 06-03-2010 12:32 PM

Thank you guys for reply,

I have gettimeofday(tv, NULL) wrapped with a function:

int SetTimer(struct timeval *tv, time_t sec)
{
gettimeofday(tv, NULL);

tv->tv_sec+=sec;

return 0;
}

in main()
{
struct timeval *tv;

SetTimer(tv, TIME_OUT);

/* Socket stuff */
...
sendto(...)

}

if I comment out contents in SetTimer, sendto works.

wje_lq 06-03-2010 12:36 PM

Thank you for posting some code. Unfortunately, it doesn't show enough to exhibit any problem.

So would you be so kind as to do these two things?
  1. Post a tiny, complete program which misbehaves, so we can compile it ourselves and watch it misbehave.
  2. Surround the code you post with CODE tags. Do this by:
    1. selecting all the code in your post, so it's highlighted; and
    2. pressing the octothorpe (#) icon above your posted text.

paulsm4 06-03-2010 02:40 PM

legendbb -

Please do what I asked you to above!!!!!

You're declaring a pointer to "struct timeval *" ... but your never ALLOCATING the timeval record that's being pointed to!!!!

Guaranteed segmentation violation ;)

Just declare the variable, and pass "&addressof" into the function. It will make you much happier ;)

PS:
Regrettably, a buffer overrun in one part of the program (e.g. your call time "gettimeofday()" with a bogus pointer) can (and usually will) cause a crash in a completely *different* part of the program (here, "sendto()". But just as easily "printf()", "malloc()" or just about any other standard library function).

legendbb 06-04-2010 09:03 AM

My problematic code is 600 lines.

I'll tailor it down until the problem just repeats, and post here is I don't find the answer.

Thank you everyone for your help and time.

legendbb 06-04-2010 10:17 AM

Quote:

Originally Posted by paulsm4 (Post 3991649)
legendbb -

Please do what I asked you to above!!!!!

You're declaring a pointer to "struct timeval *" ... but your never ALLOCATING the timeval record that's being pointed to!!!!

Guaranteed segmentation violation ;)

Just declare the variable, and pass "&addressof" into the function. It will make you much happier ;)

paulsm4 is definitely right. I didn't initialize the pointer after declaration.

BTW, what is the proper way of initializing the pointer of such struct if in some scenario I prefer to use pointer.

Code:

struct timeval *tv;

... How to initialize the tv?

gettimeofday(tv, NULL);


paulsm4 06-04-2010 10:46 AM

Quote:

BTW, what is the proper way of initializing the pointer of such struct if in some scenario I prefer to use pointer.
Simple - you declare the object (variable, struct, malloc() a block of memory - whatever) *first*. Then you can make as many or as few pointers as you want.

If you don't declare anything but a pointer ... well, then, it doesn't have anything to "point to", does it?

'Hope that helps .. PSM

PS:
An equivalent example:
Code:

  struct timeval *t_p = malloc (sizeof (struct timeval));
  ...

But *why*? Much simpler:
Code:

  struct timeval t;
  struct timeval *t_p = &t;

Make sense?

legendbb 06-04-2010 12:39 PM

Great thanks,

m_pahlevanzadeh 06-15-2012 03:30 AM

Code:

struct timeval start0={0,0};
    struct timeval t0={0,0};
    struct timeval t1={0,0};

        char *from, *to,filename;
        int port=53;
        int FTIME=0;
    int itmp=0;
    int wait_time=0;  jcounter=0;
  gettimeofday(&t0,NULL);
  start0.tv_sec=t0.tv_sec;
  start0.tv_usec=t0.tv_usec;
   

      jcounter++;
    if ((jcounter-(rint(jcounter/COUNTMAX)*COUNTMAX))==0)
          {
          gettimeofday(&t1,NULL);
          if (t0.tv_sec!=t1.tv_sec)
          { t0.tv_usec=t1.tv_usec;
            t0.tv_sec=t1.tv_sec;
            usleep(wait_time);
            tmp=t1.tv_sec-start0.tv_sec;
          printf("Feed target total %d frequency:%d/sec in pasted %d\n\n",jcounter,jcounter/(tmp?tmp:1),tmp);}
            }



All times are GMT -5. The time now is 12:33 AM.