每响应一个新的客户请求,就创建一个线程。
pthread_create(&pth,NULL,client_pro,(void*)&new_fd);
通过全局变量,管理客户端连接数。
net_user++;
从客户端接收数据
while(1){
len = recv(fd,data,1,0);
if(len==0 || len==-1){
net_user--;
close(fd);
g_dev->status[index] = 0;
pthread_exit("0");
}else{
g_dev->temp_data[index]= data[0];
}
sleep(1);
}
定时向服务端发送模拟的温度数据。
while(1){
sleep(2);
data[0]=rand()%100;//模拟产生温度数据
if((nread=send(sockfd,data,1,0))==-1){
printf("send error\n");
break;
}else{
//printf("%x,%x\n",data[0],data[1]);
}
}
头文件
#ifndef __TC9000_NET_H_
#define __TC9000_NET_H_
#define DEF_PORT_8848 8848
int socket_init(int port);//网络初始化
void* client_pro(void* arg);//响应网络客户线程
void* net_pro(void* arg);//服务端处理线程
#endif
网络线程
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <netdb.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <pthread.h>
#include "config.h"
#include "net_pro.h"
int net_user;//网络接入数
extern int g_net_port;//引用网络端口
extern struct st_sys g_dev[1]; //引用系统配置结构体全局变量
int socket_init(int port)
{
int sockfd;
struct sockaddr_in server_addr;
if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1){
fprintf(stderr,"Socket error:%s\n\a",strerror(errno));
exit(1);
}
printf("socket...\n");
bzero(&server_addr,sizeof(struct sockaddr_in));
server_addr.sin_family=AF_INET;
server_addr.sin_addr.s_addr=htonl(INADDR_ANY);
server_addr.sin_port=htons(g_net_port);
if(bind(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr))==-1)
{
fprintf(stderr,"Bind error:%s\n\a",strerror(errno));
exit(1);
}
printf("bind...\n");
if(listen(sockfd,10)==-1)
{
fprintf(stderr,"Listen error:%s\n\a",strerror(errno));
exit(1);
}
printf("listen...\n");
return sockfd;
}
void* client_pro(void* arg)
{
float temp;
int fd,*p;
int index = net_user+1;
int len;
unsigned char data[4];
p=(void*)arg;
fd = *p;
g_dev->status[index] = 1;
while(1){
len = recv(fd,data,1,0);
if(len==0 || len==-1){
net_user--;
close(fd);
g_dev->status[index] = 0;
pthread_exit("0");
}else{
g_dev->temp_data[index]= data[0];
}
sleep(1);
}
}
void* net_pro(void* arg)
{
pthread_t pth;
int sockfd,new_fd,sin_size;
struct sockaddr_in client_addr;
printf("net server pthread start...\n");
sockfd = socket_init(DEF_PORT_8848);
printf("sockfd=%d\n",sockfd);
while(1){
printf("accept before...\n");
sin_size=sizeof(struct sockaddr_in);
new_fd=accept(sockfd,(struct sockaddr*)(&client_addr),&sin_size);
if(new_fd==-1){
fprintf(stderr,"Accpet error:%s\n\a",strerror(errno));
pthread_exit("0");
}
fprintf(stderr,"Sever from %s\n",inet_ntoa(client_addr.sin_addr));
if(net_user<2){
pthread_create(&pth,NULL,client_pro,(void*)&new_fd);
net_user++;
}
}
}
功能:模拟数据采集,通过随机数产生温度数据。
#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<string.h>
#include<netdb.h>
#include<fcntl.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
int main(int argc, char *argv[])
{
int port;
int sockfd,nread;
struct sockaddr_in server_addr;
unsigned char data[5];
if(argc < 3){
printf("please input server IP,PORT.\n");
exit(0);
}
port = atoi(argv[2]);
if(port<1024){
printf("port error\n");
exit(0);
}
if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1){
fprintf(stderr,"Socket Error:%s\a\n",strerror(errno));
exit(1);
}
bzero(&server_addr,sizeof(server_addr));
server_addr.sin_family=AF_INET;
server_addr.sin_port=htons(port);
server_addr.sin_addr.s_addr=inet_addr(argv[1]);
if(connect(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr))==-1)
{
fprintf(stderr,"Connect Error:%s\a\n",strerror(errno));
exit(1);
}
while(1){
sleep(2);
data[0]=rand()%100;//模拟产生温度数据
if((nread=send(sockfd,data,1,0))==-1){
printf("send error\n");
break;
}else{
//printf("%x,%x\n",data[0],data[1]);
}
}
close(sockfd);
exit(0);
}
Copyright © 2016 www.91arm.com 【91创客学堂】