Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 13
I'm now doing a project regarding the link traffic. In my project i try to use popen command to get the number of bytes i received in c programming and ...
  1. #1
    Just Joined!
    Join Date
    Nov 2008
    Posts
    25

    how to get the updated result continuously by using popen command in c language?

    I'm now doing a project regarding the link traffic. In my project i try to use popen command to get the number of bytes i received in c programming and i successfully did so.
    However here comes my problem, when i want to process the data I obtained, I found that this command keep giving me the same value without updating it. Here I post the code i roughly did in order to process the data.

    Code:
    #include <stdio.h>
    #include <stdlib.h> /* required for atoi */
    
    long int calculate();
    
    main()
    {
    
    long int a[3];
    long int b[5];
    int i,j;
    
    for (j=0; j<5; j++)
    {
    b[j]=0;
    	for(i=0 ; i<3; i++)
    	{
    	a[i]=calculate();
    	b[j]+= a[i];
    	}
    b[j]=b[j]/3;
    printf("The number of bytes is: %d\n", b[j]);
    }
    
    }
    
    long int calculate()
    {
      FILE *fp;
      char line[130];			/* line of easa!from unix command*/
      int i;
     
      fp = popen("ifconfig eth0 |grep bytes|cut -c20-35", "r");		/* Issue the command.	*/
    
    					/* Read a line	*/
      while ( fgets( line, sizeof line, fp))
      {
        printf("%s", line);		/* %s means string */
        i = atoi(line);
      }
      pclose(fp);
      
     return i;
    }
    Does anyone please help me figure out my problems or correct me if there is error on my code since i still a newbie to c language and linux world.

    Thanks in advanced.

  2. #2
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    My system, Suse 11.0, doesn't have ifconfig..the information from "info ifconfig" says that ifconfig is obsolete and should not be used with kernels newer than 2.0. It also says that newer kernels should use ip...hope this helps

    Welcome to the forums

  3. #3
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    I'm running Slackware 12.1, which is the current released version. There is no hint in the documentation included with Slackware that ifconfig is obsolete. I googled "ifconfig obsolete" and found few references to such an allegation, and they don't convince me. Furthermore, it works.

    Furthermore, when I compile and run christyyim's program, that works too.

    That program runs ifconfig 15 times fast, and pulls the total number of bytes received on eth0 from ifconfig's output each time. Each group of three result figures is averaged (arithmetically). Each of the five averages is sent to stdout. This works just fine.

    My question, christyyim, is why you expect the number to change. ifconfig is run 15 times in very rapid succession, with no waiting between times. It's unlikely that a new packet will be completely received during this time. If I refresh a web page in my browser, say, and then run your program again, I get a slightly higher number, the same new one five times. This is what I would expect.

    What seems to be the problem with your program? It seems to work just fine!
    --
    Bill

    Old age and treachery will overcome youth and skill.

  4. #4
    Just Joined!
    Join Date
    Nov 2008
    Posts
    25
    Hi wje_lf,

    actually if i run ifconfig command in the terminal, everytime it will give me different number of bytes that received, hence i think of averaging them to get a smoother graph. And this will be a start up point for me to get the wireless signal strength by using iwconfig command next. As you know, the wireless signal strength will vary much more than the number of bytes i received here since wireless is less stable than wired.

    And from what u have observed is that ifconfig is run 15 times in very rapid succession, with no waiting between times. It's unlikely that a new packet will be completely received during this time.

    Is it if i insert wait state before getting a new value I'll get different number of bytes? What i scare is once at the first time i execute the popen command, the number of bytes will be read at the FILE *fp and will not change even after the popen command execute again. And this is not what I wish.

    and is it i can insert wait states by using sleep() command?

    Thanks for your advices.

  5. #5
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    if i run ifconfig command in the terminal, everytime it will give me different number of bytes that received
    Yes, but it's unlikely that you can type "ifconfig eth0" as fast (or even anywhere near as fast) as your program can run 15 of those. If you could type that fast, it would be likely that you would get the same number 15 times in a row. But you also have another problem:
    hence i think of averaging them to get a smoother graph
    I think you're under the impression that each time you run ifconfig, you're going to get the number of bytes received since the previous time you ran ifconfig. That's not true. You're going to get the number of bytes received since eth0 most recently came up.

    Let's take an oversimplified example. Let's say eth0 is receiving three bytes per second, and that you're running ifconfig once every five seconds. That means you've received 15 new bytes each time you run ifconfig. But if you run ifconfig by hand at the prompt every five seconds, you're not going to get numbers like these:
    Code:
    15
    15
    15
    15
    15
    15
    15
    15
    15
    15
    Instead, you're going to get numbers like these:
    Code:
    246515
    246530
    246545
    246560
    246575
    246590
    246605
    246620
    246635
    246650
    So if you want to do this, you have to take each number you get and subtract from it the previous number. Of course, you have to not report the first number you get, because there's no previous number to subtract.
    Is it if i insert wait state before getting a new value I'll get different number of bytes?
    Yes.
    What i scare is once at the first time i execute the popen command, the number of bytes will be read at the FILE *fp and will not change even after the popen command execute again.
    That's exactly what's happening now. But it's not because of the way popen() works; it's because you haven't waited long enough for something new to happen.
    is it i can insert wait states by using sleep() command?
    Yes.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  6. #6
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714

    in my defence

    File: *manpages*, Node: ifconfig, Up: (dir)

    IFCONFIG( Linux Programmer's Manual IFCONFIG(

    NAME
    ifconfig - configure a network interface

    SYNOPSIS
    ifconfig [interface]
    ifconfig interface [aftype] options | address ...

    DESCRIPTION
    Ifconfig is used to configure the kernel-resident network interfaces.
    It is used at boot time to set up interfaces as necessary. After that,
    it is usually only needed when debugging or when system tuning is
    needed.

    WARNING: Ifconfig is obsolete on system with Linux kernel newer than
    2.0. On this system you should use ip. See the ip manual page for
    details

    If no arguments are given, ifconfig displays the status of the cur-
    rently active interfaces. If a single interface argument is given, it
    displays the status of the given interface only; if a single -a argu-
    ment is given, it displays the status of all interfaces, even those
    that are down. Otherwise, it configures an interface.

    Address Families
    If the first argument after the interface name is recognized as the
    name of a supported address family, that address family is used for
    decoding and displaying all protocol addresses. Currently supported
    address families include inet (TCP/IP, default), inet6 (IPv6), ax25
    (AMPR Packet Radio), ddp (Appletalk Phase 2), ipx (Novell IPX) and
    netrom (AMPR Packet radio).

  7. #7
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192

    dueling man pages

    This is quite interesting. What you're getting, of course, is the man page, infoized.

    The one distributed with slackware 12.1, in its entirety:
    Code:
    IFCONFIG(8)                Linux Programmer's Manual               IFCONFIG(8)
    
    
    
    NAME
           ifconfig - configure a network interface
    
    SYNOPSIS
           ifconfig [-v] [-a] [-s] [interface]
           ifconfig [-v] interface [aftype] options | address ...
    
    DESCRIPTION
           Ifconfig is used to configure the kernel-resident network interfaces.
           It is used at boot time to set up interfaces as necessary.  After that,
           it is usually only needed when debugging or when system tuning is
           needed.
    
           If no arguments are given, ifconfig displays the status of the cur-
           rently active interfaces.  If a single interface argument is given, it
           displays the status of the given interface only; if a single -a argu-
           ment is given, it displays the status of all interfaces, even those
           that are down.  Otherwise, it configures an interface.
    
    
    Address Families
           If the first argument after the interface name is recognized as the
           name of a supported address family, that address family is used for
           decoding and displaying all protocol addresses.  Currently supported
           address families include inet (TCP/IP, default), inet6 (IPv6), ax25
           (AMPR Packet Radio), ddp (Appletalk Phase 2), [B]ipx]B\ (Novell IPX) and
           [B]netrom[B] (AMPR Packet radio).
    
    OPTIONS
           -a     display all interfaces which are currently available, even if
                  down
    
           -s     display a short list (like netstat -i)
    
           -v     be more verbose for some error conditions
    
           interface
                  The name of the interface.  This is usually a driver name fol-
                  lowed by a unit number, for example eth0 for the first Ethernet
                  interface. If your kernel supports alias interfaces, you can
                  specify them with eth0:0 for the first alias of eth0. You can
                  use them to assign a second address. To delete an alias inter-
                  face use ifconfig eth0:0 down.  Note: for every scope (i.e. same
                  net with address/netmask combination) all aliases are deleted,
                  if you delete the first (primary).
    
           up     This flag causes the interface to be activated.  It is implic-
                  itly specified if an address is assigned to the interface.
    
           down   This flag causes the driver for this interface to be shut down.
    
           [-]arp Enable or disable the use of the ARP protocol on this interface.
    
           [-]promisc
                  Enable or disable the promiscuous mode of the interface.  If
                  selected, all packets on the network will be received by the
                  interface.
    
           [-]allmulti
                  Enable or disable all-multicast mode.  If selected, all multi-
                  cast packets on the network will be received by the interface.
    
           metric N
                  This parameter sets the interface metric.
    
           mtu N  This parameter sets the Maximum Transfer Unit (MTU) of an inter-
                  face.
    
           dstaddr addr
                  Set the remote IP address for a point-to-point link (such as
                  PPP).  This keyword is now obsolete; use the pointopoint keyword
                  instead.
    
           netmask addr
                  Set the IP network mask for this interface.  This value defaults
                  to the usual class A, B or C network mask (as derived from the
                  interface IP address), but it can be set to any value.
    
           add addr/prefixlen
                  Add an IPv6 address to an interface.
    
           del addr/prefixlen
                  Remove an IPv6 address from an interface.
    
           tunnel aa.bb.cc.dd
                  Create a new SIT (IPv6-in-IPv4) device, tunnelling to the given
                  destination.
    
           irq addr
                  Set the interrupt line used by this device.  Not all devices can
                  dynamically change their IRQ setting.
    
           io_addr addr
                  Set the start address in I/O space for this device.
    
           mem_start addr
                  Set the start address for shared memory used by this device.
                  Only a few devices need this.
    
           media type
                  Set the physical port or medium type to be used by the device.
                  Not all devices can change this setting, and those that can vary
                  in what values they support.  Typical values for type are
                  10base2 (thin Ethernet), 10baseT (twisted-pair 10Mbps Ethernet),
                  AUI (external transceiver) and so on.  The special medium type
                  of auto can be used to tell the driver to auto-sense the media.
                  Again, not all drivers can do this.
    
           [-]broadcast [addr]
                  If the address argument is given, set the protocol broadcast
                  address for this interface.  Otherwise, set (or clear) the
                  IFF_BROADCAST flag for the interface.
    
           [-]pointopoint [addr]
                  This keyword enables the point-to-point mode of an interface,
                  meaning that it is a direct link between two machines with
                  nobody else listening on it.
                  If the address argument is also given, set the protocol address
                  of the other side of the link, just like the obsolete dstaddr
                  keyword does.  Otherwise, set or clear the IFF_POINTOPOINT flag
                  for the interface.
    
           hw class address
                  Set the hardware address of this interface, if the device driver
                  supports this operation.  The keyword must be followed by the
                  name of the hardware class and the printable ASCII equivalent of
                  the hardware address.  Hardware classes currently supported
                  include ether (Ethernet), ax25 (AMPR AX.25), ARCnet and netrom
                  (AMPR NET/ROM).
    
           multicast
                  Set the multicast flag on the interface. This should not nor-
                  mally be needed as the drivers set the flag correctly them-
                  selves.
    
           address
                  The IP address to be assigned to this interface.
    
           txqueuelen length
                  Set the length of the transmit queue of the device. It is useful
                  to set this to small values for slower devices with a high
                  latency (modem links, ISDN) to prevent fast bulk transfers from
                  disturbing interactive traffic like telnet too much.
    
    NOTES
           Since kernel release 2.2 there are no explicit interface statistics for
           alias interfaces anymore. The statistics printed for the original
           address are shared with all alias addresses on the same device. If you
           want per-address statistics you should add explicit accounting rules
           for the address using the ipchains(8) or iptables(8) command.
    
           Since net-tools 1.60-4 ifconfig is printing byte counters and human
           readable counters with IEC 60027-2 units. So 1 KiB are 2^10 byte. Note,
           the numbers are truncated to one decimal (which can by quite a large
           error if you consider 0.1 PiB is 112.589.990.684.262 bytes :)
    
           Interrupt problems with Ethernet device drivers fail with EAGAIN (SIOC-
           SIIFLAGS: Resource temporarily unavailable) it is most likely a inter-
           rupt conflict. See http://www.scyld.com/expert/irq-conflict.html for
           more information.
    
    FILES
           /proc/net/socket
           /proc/net/dev
           /proc/net/if_inet6
    
    BUGS
           While appletalk DDP and IPX addresses will be displayed they cannot be
           altered by this command.
    
    SEE ALSO
           route(8), netstat(8), arp(8), rarp(8), ipchains(8), iptables(8),
           ifup(8), interfaces(5).
           http://physics.nist.gov/cuu/Units/binary.html - Prefixes for binary
           multiples
    
    AUTHORS
           Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org>
           Alan Cox, <Alan.Cox@linux.org>
           Phil Blundell, <Philip.Blundell@pobox.com>
           Andi Kleen
           Bernd Eckenfels, <net-tools@lina.inka.de>
    
    
    
    net-tools                         2007-12-02                       IFCONFIG(8)
    One would think that the Slackware man page is older than the Suse one, except for two things:
    1. The notes in the Slackware man page refer to how the behavior of ifconfig changed in Linux kernel 2.2. This implies that ifconfig has been maintained after the introduction of kernel 2.0.
    2. The synopsis in the Slackware man page refers to a -v option, but the synopsis in the Suse man page does not. This implies that the Slackware man page is aware of a later state of affairs. It could be that the Suse man page accurately documents an older version of ifconfig, and the Slackware man page accurately documents a newer version of ifconfig.

    Gerard, this is fascinating. Could you please do a couple of things?

    Could you please post the entire man page? I'm interested in the list of authors and the date at the very end, as well as other details in the middle.

    Could you also please do this at the command line, and post the response? (Substitute something else for eth0 if eth0 is not up on your system.) If your ifconfig objects to the -v, then we know you're running an ifconfig as documented by the Suse man page; if it takes the -v without complaint, it's more likely you're running an ifconfig as documented by the Slackware man page.
    Code:
    ifconfig -v eth0
    --
    Bill

    Old age and treachery will overcome youth and skill.

  8. #8
    Just Joined!
    Join Date
    Nov 2008
    Posts
    25

    Thanks!!!

    Thanks wje_lf and gerard4143!!!

    wje_lf, I think i get what u mean and i really misunderstood the meaning of bytes received. Thanks for your information. And after I insert the wait states, I can get the varying number of bytes (increasing) if i refresh the webpages. I'll modify my source code to take it into consideration. Thanks!!!

    and thanks to gerard4143 as well for giving me the updated info. Thanks!!!

  9. #9
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    IFCONFIG( Linux Programmer's Manual IFCONFIG(

    DESCRIPTION
    Ifconfig is used to configure the kernel-resident network interfaces.
    It is used at boot time to set up interfaces as necessary. After that,
    it is usually only needed when debugging or when system tuning is
    needed.

    WARNING: Ifconfig is obsolete on system with Linux kernel newer than
    2.0. On this system you should use ip. See the ip manual page for
    details

    ...

    AUTHORS
    Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org>
    Alan Cox, <Alan.Cox@linux.org>
    Phil Blundell, <Philip.Blundell@pobox.com>
    Andi Kleen



    net-tools 14 August 2000 IFCONFIG(

  10. #10
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714

    Here's the complete file

    This is the complete man file for ifconfig

    "ifconfig -v eth0" is not recognized on my system...so it can't
    be included with the standard install...Hope this helps...Gerard4143
    Attached Files Attached Files

Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...