linux proc net tcp,linux(RedHat)从/proc/net/tcp 中读取tcp连接信息

#includeusing namespace std;

//struct: Property of a tcp connection

struct ConnectionProperty

{

string local_addr;

string remote_addr;

string state;

};

//Change hexadecimal number to decimal number

int HexToInt(char h)

{

if(h >= '0' && h <= '9')

{

return h - '0';

}

else if(h >= 'A' && h <= 'F')

{

return h - 'A' + 10;

}

else

{

cerr << "Error: Illegal Hex Number!" << endl;

return -1;

}

}

//Get Ip address and port number from string XXXXXXXX:XXXX

string GetIpAddress(char* str)

{

int a, b, c, d, e;

a = HexToInt(str[0]) * 16 + HexToInt(str[1]);

b = HexToInt(str[2]) * 16 + HexToInt(str[3]);

c = HexToInt(str[4]) * 16 + HexToInt(str[5]);

d = HexToInt(str[6]) * 16 + HexToInt(str[7]);

e = HexToInt(str[9]) * 16 * 16 * 16 +

HexToInt(str[10]) * 16 * 16 +

HexToInt(str[11]) * 16 +

HexToInt(str[12]);

//change int to string

string sa, sb, sc, sd, se;

ostringstream oss;

oss << a;

sa = oss.str();

oss.str(""); //clear the content in oss

oss << b;

sb = oss.str();

oss.str("");

oss << c;

sc = oss.str();

oss.str("");

oss << d;

sd = oss.str();

oss.str("");

oss << e;

se = oss.str();

oss.str("");

//return by order: d.c.b.a:e

return sd + '.' + sc + '.' + sb + '.' + sa + ':' + se;

}

//Get tcp connection state

string GetConnectionState(char* str)

{

if(str[0] == '0' && str[1] == '0') return "ERROR_STATUS";

if(str[0] == '0' && str[1] == '1') return "TCP_ESTABLISHED";

if(str[0] == '0' && str[1] == '2') return "TCP_SYN_SENT";

if(str[0] == '0' && str[1] == '3') return "TCP_SYN_RECV";

if(str[0] == '0' && str[1] == '4') return "TCP_FIN_WAIT1";

if(str[0] == '0' && str[1] == '5') return "TCP_FIN_WAIT2";

if(str[0] == '0' && str[1] == '6') return "TCP_TIME_WAIT";

if(str[0] == '0' && str[1] == '7') return "TCP_CLOSE";

if(str[0] == '0' && str[1] == '8') return "TCP_CLOSE_WAIT";

if(str[0] == '0' && str[1] == '9') return "TCP_LAST_ACK";

if(str[0] == '0' && str[1] == 'A') return "TCP_LISTEN";

if(str[0] == '0' && str[1] == 'B') return "TCP_CLOSING";

return "UNKNOWN_STATE";

}

int main()

{

//read from file /proc/net/tcp

ifstream infile("/proc/net/tcp", ios :: in);

if(!infile)

{

cerr << "open error!" << endl;

exit(1);

}

string s;

char s_local_addr[20], s_remote_addr[20], s_state[20];

getline(infile, s); //title: every column's name

while(getline(infile, s))

{

sscanf(s.c_str(), "%*s%s%s%s", s_local_addr, s_remote_addr, s_state);

printf("%s\t%s\t%s\n", s_local_addr, s_remote_addr, s_state);

string ip_local = GetIpAddress(s_local_addr);

string ip_remote = GetIpAddress(s_remote_addr);

string conn_state = GetConnectionState(s_state);

cout << ip_local << "\t" << ip_remote << "\t" << conn_state << endl;

}

return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值