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.
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
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.