Find the answer to your Linux question:
Results 1 to 7 of 7
Hi all, I am programming on Linux middleware so I need to get a list of all active TCP connections. Is there any API to get such information? I know ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Oct 2010
    Posts
    7

    How to find the active TCP connections and PIDs corresponding to them?


    Hi all,

    I am programming on Linux middleware so I need to get a list of all active TCP connections. Is there any API to get such information? I know I can find the connections in /proc/net/tcp, but how to find the PIDs? Apparently ps or netstat command is not an option since it is a middleware. Any help will be appreciated!

    Cheers
    Tete

  2. #2
    Trusted Penguin Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    3,399
    Hi and welcome

    As root
    Code:
    netstat -anp --ip
    Also this might help
    Code:
    man netstat
    You must always face the curtain with a bow.

  3. #3
    Just Joined!
    Join Date
    Oct 2010
    Posts
    7
    Quote Originally Posted by Irithori View Post
    Hi and welcome

    As root
    Code:
    netstat -anp --ip
    Also this might help
    Code:
    man netstat
    Thanks for the quick reply. But as I said, if it is a program, is it a little bit ugly to use system("netstat -any --ip") in the program? Is there any library or function to call to do the same job? Or actually I am new to linux programming so I don't really know how they usually do it? Just this system call? Thanks again!

  4. #4
    Trusted Penguin Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    3,399
    Sorry, missed that part.
    I am not a dev, but from the look of the man page and also a quick strace, netstat actually parses /proc.

    From the top of my head, I am not aware of a library or API, but maybe someone else here has an idea.
    You must always face the curtain with a bow.

  5. #5
    Trusted Penguin Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    3,399
    There is a perl module
    Linux::Proc::Net::TCP - search.cpan.org

    Which language are you using?
    You must always face the curtain with a bow.

  6. #6
    Just Joined!
    Join Date
    Oct 2010
    Posts
    7
    Quote Originally Posted by Irithori View Post
    Which language are you using?
    I am using C/C++. In fact in the /proc/net/tcp, I can only find the tcp transmission information as well as the UID. But my problem is how to trace which process or program is using which TCP connection? Thanks for your reply!

  7. #7
    Trusted Penguin Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    3,399
    hmm,
    as you know C, a look into netstatīs code might provide more insight.
    Index of /phil/net-tools/

    Disclaimer:
    This is GPL2 software, so be careful to actually re-implement, not copy, if your licence is not GPL2.
    You must always face the curtain with a bow.

Posting Permissions

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