废话不多说,直接上代码:
客户端:
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/shm.h>
#include <sys/un.h>
#include <string.h>
#include <stdio.h>
#include <ifaddrs.h>
#include <sys/ioctl.h>
#include <iostream>
#include <net/if.h>
#include <net/if_arp.h>
using namespace std;#define PORT 8890
#define BUFFER_SIZE 1024
#define MAXINTERFACES 64
const char *filename="/tmp/sss";
struct net{
char name[128];
char mac[30];
char ip[30];
char state[30];
unsigned long long rx_bytes;
unsigned long long rx_packets;
unsigned long long rx_errs;
unsigned long long rx_drop;
unsigned long long tx_bytes;
unsigned long long tx_packets;
unsigned long long tx_errs;
unsigned long long tx_drop;
};
enum if_item
{
RX_BYTES = 0,
RX_PACKETS,
RX_ERRS,
RX_DROP,
RX_FIFO,
RX_FRAME,
RX_COMPRESSED,
RX_MULTICAST,
TX_BYTES,
TX_PACKETS,
TX_ERRS,
TX_DROP,
TX_FIFO,
TX_COLLS,
TX_CARRIER,
TX_COMPRESSED,
IF_ITEM_MAX
};int AdapterCount(struct net* Adapter)
{
int nCount=0;
FILE* fp = fopen("/proc/net/dev", "r");
if(!fp)
{
perror("fopen /proc/net/dev");
exit(0);
}
char szline[1024] = {0};
fgets(szline, sizeof(szline), fp);
fgets(szline, sizeof(szline), fp);
int i=0;
struct ifreq ifr_mac_ip;
int s = socket(AF_UNIX, SOCK_STREAM, 0);
memset(szline, 0, sizeof(szline));
//
while(fgets(szline, sizeof(szline), fp) != NULL)
{
//
string name = szline;
int p1 = name.find_first_of(':', 0);
name = name.substr(0, p1);
int p2 = name.rfind(' ', -1);
if(p2 != -1)
{
name = name.substr(p2+1, -1);
}
memcpy(Adapter[i].name, name.c_str(), strlen(name.c_str()));
unsigned long long data[32] = {0};
sscanf(szline+p1+1, "%llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu",
data + RX_BYTES,
data + RX_PACKETS,
dat