LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 06-03-2010, 11:11 AM   #1
legendbb
LQ Newbie
 
Registered: Apr 2007
Posts: 12

Rep: Reputation: 0
Question <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,

Last edited by legendbb; 06-04-2010 at 11:19 AM.
 
Old 06-03-2010, 12:01 PM   #2
posixculprit
Member
 
Registered: May 2010
Posts: 136

Rep: Reputation: 42
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.
 
1 members found this post helpful.
Old 06-03-2010, 12:06 PM   #3
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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);
...
 
Old 06-03-2010, 01:32 PM   #4
legendbb
LQ Newbie
 
Registered: Apr 2007
Posts: 12

Original Poster
Rep: Reputation: 0
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.
 
Old 06-03-2010, 01:36 PM   #5
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: Debian lenny, Slackware 12
Posts: 809

Rep: Reputation: 178Reputation: 178
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.
 
Old 06-03-2010, 03:40 PM   #6
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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).

Last edited by paulsm4; 06-04-2010 at 01:39 AM.
 
1 members found this post helpful.
Old 06-04-2010, 10:03 AM   #7
legendbb
LQ Newbie
 
Registered: Apr 2007
Posts: 12

Original Poster
Rep: Reputation: 0
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.
 
Old 06-04-2010, 11:17 AM   #8
legendbb
LQ Newbie
 
Registered: Apr 2007
Posts: 12

Original Poster
Rep: Reputation: 0
Talking

Quote:
Originally Posted by paulsm4 View Post
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);

Last edited by legendbb; 06-04-2010 at 11:18 AM.
 
Old 06-04-2010, 11:46 AM   #9
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Wink

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?
 
1 members found this post helpful.
Old 06-04-2010, 01:39 PM   #10
legendbb
LQ Newbie
 
Registered: Apr 2007
Posts: 12

Original Poster
Rep: Reputation: 0
Great thanks,
 
Old 06-15-2012, 04:30 AM   #11
m_pahlevanzadeh
Member
 
Registered: Sep 2002
Location: Iran/Tehran
Distribution: Debian
Posts: 94

Rep: Reputation: 26
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);}
     	}
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
output time in ms from gettimeofday liqxpil Programming 3 04-23-2010 02:34 PM
gettimeofday() - execution time and underlying implementation details cagri_balkesen Linux - Kernel 1 02-08-2009 02:27 PM
Multithread udp socket client fails to sendto data dilin Programming 0 04-06-2007 10:07 AM
sendto error (socket) payal_shah Linux - Networking 0 02-23-2005 01:18 PM
time, gettimeofday h/w Programming 13 12-08-2003 06:17 PM


All times are GMT -5. The time now is 06:29 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration