【socket】在Linux下socket温度上报--客户端
socket函数
int socket(int domain,int type,int protocol);
参数:
domain:指定发送通信的域
可取值:AF_UNIX:本地主机通信,与IPC类似
AF_INET:Internet地址IPV4协议
AF_INET6:Internet地址IPV6协议
type:指定socket类型
可取值:SOCK_STREAM(流套接字)SOCK_DGRAM(数据报套接字)、SOCK_RAW(原始套接字)
protocol:指定该套接字描述符上的一个特殊的协议,如TCP,UDP等,一般设为0,会自动选择type的类型对应的协议
补充:SOCK_STREAM(流套接字)应用TCP协议,提供顺序的,可靠的,基于字节流的双向链接
SOCK_DGRAM(数据报套接字)应用UDP协议,无链接,不可靠,不固定
SOCK_RAW(原始套接字)提供访问互联网协议和Internal Network Interfaces的权限,只有超级用户才可使用。
int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
功能:
服务器端连接指定计算机端口
参数
sockfd: 客户端的socket()创建的描述字
addr: 要连接的服务器的socket地址信息,这里面包含有服务器的IP地址和端口等信息
addrlen: socket地址的长度
更多socket函数
代码实现
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <getopt.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <libgen.h>
#include <syslog.h>
#include <dirent.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/stat.h>
#include <unistd.h>
#include <ctype.h>
#include <time.h>
#include <netdb.h>
int socket_init(char *ip, int serv_port);
int get_temperature(float *temp);
void signal_stop(int signum);
static inline void print_usage(char *progname);
int get_ipaddr(char *interface,char *ipaddr, int ipaddr_size);
int get_time(char *datime,int len);
static int g_sigstop = 0;
int main (int argc, char **argv)
{
float temp;
int sock_fd = -1;
char buf[2048];
int serv_port = 0;
int daemon_run = 0;
char *progname = NULL;
char *sock_ip;
int opt;
struct sockaddr_in serv_addr;
char ipaddr[16];
char *interface = "eth0";
char datime[32];
int rv;
int log_fd;
int s_time;
char *hostname;
struct hostent *getname = NULL;
struct option long_options[]=
{
{"daemon",no_argument,NULL,'b'},
{"port", required_argument,NULL,'p'},
{"time", required_argument,NULL,'t'},
{"ip", required_argument,NULL,'i'},
{"help",no_argument,NULL,'h'},
{NULL,0,NULL,0}
};
progname = basename(argv[0]);
if(daemon(1,1)< 0)
{
printf("Program daemon() failure:%s\n",strerror(errno));
return -1;
}
log_fd = open("t.log", O_CREAT|O_RDWR,0666);
if(log_fd < 0)
{
printf("Failed to open file/create:%s\n",strerror(errno));
return -1;
}
dup2(log_fd,STDOUT_FILENO);
dup2(log_fd,STDERR_FILENO);
while((opt = getopt_long(argc,argv,"bp:t:i:h",long_options,NULL))!= -1)
{
switch(opt)
{
case 'b':
daemon_run = 1;
break;
case 'p':
serv_port = atoi(optarg);
break;
case 't':
s_time = atoi(optarg);
break;
case 'i':
hostname = optarg;
break;
case 'h':
print_usage(progname);
return EXIT_SUCCESS;
default:
break;
}
}
if(!serv_port || !hostname)
{
print_usage(progname);
return -2;
}
getname = gethostbyname(hostname);
if(getname == NULL)
{
printf("Get hostname failure : %s\n", strerror(h_errno));
return 0;
}
sock_ip = inet_ntoa(*(struct in_addr *)getname->h_addr);
if(sock_ip == NULL)
{
printf("Get ser_ip failure : %s\n", strerror(errno));
return 0;
}
printf("sock_ip[%s]\n",sock_ip);
signal(SIGUSR1, signal_stop);
signal(SIGPIPE, signal_stop);
memset(buf,0,sizeof(buf));
while(!g_sigstop)
{
get_temperature(&temp);
if(temp < 0)
{
printf("Failed to read temperature:%s\n",strerror(errno));
return -1;
}
memset(ipaddr,0,sizeof(ipaddr));
get_ipaddr(interface,ipaddr,sizeof(ipaddr));
if(ipaddr < 0)
{
printf("Failed to obtain the ID:%s\n",strerror(errno));
return -2;
}
get_time(datime,sizeof(datime));
if(datime < 0)
{
printf("Time acquisition failure:%s\n",strerror(errno));
return -3;
}
snprintf(buf,sizeof(buf),"%s/%s/%f",ipaddr,datime,temp);
if(sock_fd<0)
{
sock_fd = socket(AF_INET,SOCK_STREAM,0);
if(sock_fd<0)
{
printf("Failed to connect to server:%s\n",strerror(errno));
return -2;
}
printf("Server successful!\n");
memset(&serv_addr,0,sizeof(&serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(serv_port);
inet_aton(sock_ip,&serv_addr.sin_addr);
if(connect(sock_fd,(struct sockaddr*)&serv_addr,sizeof(serv_addr))<0)
{
printf("Server connection failure:%s\n",strerror(errno));
return -2;
}
printf("Server[%d] connection successful!\n",serv_port);
}
if(sock_fd >= 0)
{
rv = write(sock_fd,buf,sizeof(buf));
if(rv >= 0)
{
printf("Send messege to server successfully!\n");
}
else
{
printf("Send messege to server failure:%s\n",strerror(errno));
close(sock_fd);
sock_fd = -1;
}
}
sleep(s_time);
}
close(sock_fd);
return 0;
}
static inline void print_usage(char *progname)
{
printf("-b[daemon] set progname running background\n");
printf("-p[port] socket servr port address\n");
printf("-t[time] How often are they monitored\n");
printf("-i[ip] Monitor the Ip address of the server\n");
printf("-p[help] display this heip information\n");
printf("\n exampla:%s -b -p 3400 -i 127.0.0.1 -t 30s \n",progname);
return ;
}
void signal_stop(int signum)
{
if(SIGUSR1 == signum)
{
printf("SIGTERM signal detected\n");
g_sigstop = 1;
}
else if(SIGPIPE == signum)
{
printf("SIGINT signal detected\n");
g_sigstop = 1;
}
}
int get_temperature(float *temp)
{
int file_fd = -1;
char buf[1024];
char *ptr = NULL;
DIR *dirp = NULL;
struct dirent *direntp = NULL;
char w1_path[64]= "/sys/bus/w1/devices/";
char chip_sn[64];
int found = 0;
dirp = opendir(w1_path);
if(!dirp)
{
printf("Failed to open folder:%s\n",strerror(errno));
return -1;
}
while(NULL !=(direntp = readdir(dirp)))
{
if(strstr(direntp->d_name,"28-"))
{
strncpy(chip_sn,direntp->d_name,sizeof(chip_sn));
found = -1;
}
}
closedir(dirp);
if(!found)
{
printf("can not find ds18b20 chipset\n");
return -2;
}
strncat(w1_path,chip_sn,sizeof(w1_path)-strlen(w1_path));
strncat(w1_path,"/w1_slave",sizeof(w1_path)-strlen(w1_path));
if((file_fd = open(w1_path,O_RDONLY))<0)
{
printf("File opening failure:%s\n",strerror(errno));
return -3;
}
memset(buf,0,sizeof(buf));
if(read(file_fd,buf,sizeof(buf))<0)
{
printf("read data from fd=%d failure:%s\n",file_fd,strerror(errno));
return -4;
}
ptr = strstr(buf,"t=");
if(!ptr)
{
printf("t=string\n");
return -5;
}
ptr+=2;
*temp = atof(ptr)/1000;
close(file_fd);
return 0;
}
int get_ipaddr(char *interface,char *ipaddr, int ipaddr_size)
{
char buf[1024];
char *ptr;
char *ip_start;
char *ip_end;
FILE *fd;
int len;
int rv;
if(!interface || !ipaddr || ipaddr_size <16)
{
printf("Invalid input argument\n");
}
memset(buf,0,sizeof(buf));
snprintf(buf,sizeof(buf),"ifconfig %s",interface);
fd = popen(buf,"r");
if(!fd)
{
printf("popen() to extern command failure:%s\n",strerror(errno));
}
rv = -3;
while(fgets(buf,sizeof(buf),fd))
{
if(strstr(buf,"netmask"))
{
ptr = strstr(buf,"inet");
if(!ptr)
{
break;
}
ptr +=strlen("inet");
while(isblank(*ptr))
ptr++;
ip_start = ptr;
while(!isblank(*ptr))
ptr++;
ip_end = ptr;
memset(ipaddr,0,sizeof(ipaddr));
len = ip_end - ip_start;
len = len > ipaddr_size ? ipaddr_size:len;
memcpy(ipaddr,ip_start,len);
rv = 0;
break;
}
}
pclose(fd);
return rv ;
}
int get_time(char *datime,int len)
{
time_t t;
struct tm *p;
time(&t);
p = gmtime(&t);
snprintf(datime, 32, "%d-%d-%d %d:%d:%d", 1900+p->tm_year,1+p->tm_mon, p->tm_mday, (p->tm_hour + 8), p->tm_min, p->tm_sec);
return 0 ;
}