3

Is there a way to get a list of tcp, udp connections and processes they are associated with?

I did this in windows by using "GetExtendedTcpTable()" and "GetExtendedUdpTable()" And i want to achieve the same thing in linux.

Can someone help?

thank you.

user2389323
  • 769
  • 2
  • 10
  • 22

1 Answers1

2

You can do stuff like this:

#include <fstream>
#include <iostream>

int main()
{
    std::ifstream tcp("/proc/net/tcp");

    std::string line;
    while(std::getline(tcp, line))
        std::cout << line << '\n';
}

To discover what other names are available try this at the command line:

ls -l /proc/net/

Not sure the best way to associate process id (pid) with sockets but you can cross reference /proc/<pid>/fd, where <pid> is the actual number, with /proc/net/tcp.

Galik
  • 47,303
  • 4
  • 80
  • 117