智能家具(五)—— 网络服务器线程控制

网络线程控制代码

mainpro.c

#include "ctrDevice.h"
#include <string.h>
#include <pthread.h>
#include <errno.h>

#include "inputCmd.h"

struct ctrDevice* phead;

struct inputCmd* inputHead= NULL;	//寻找链表

struct inputCmd* socketHandler= NULL;	//为线程提供socket的结构体


void *socketControlHandler2(void *data)
{
	int nread = 0;
	while(1){
		memset(socketHandler->getCmd,'\0',sizeof(socketHandler->getCmd));
		nread = socketHandler->inputGetCmd(socketHandler);
		if(nread==-1){
				perror("read");
			}else{
			printf("nread =%d,socketcmd=%s\n",nread,socketHandler->getCmd);
			ctrCmd(socketHandler);
				}
	}

}

struct inputCmd* findInputFunc(struct inputCmd* phead,char *cmd)
{
	struct inputCmd* tmp;
	tmp = phead;
	if(tmp == NULL){
		printf("no found!\n");
		return NULL;
	}else{
		while(tmp != NULL){
			if(strcmp(tmp->cmdName,cmd)==0)
			{
					return tmp;
			}
			tmp = tmp->next;
		}
		return NULL;
	}
}

//socket的第一个线程,用来接收新的客户端
void *socketControlHandler(void *data)
{
	int nread=0;
	
   pthread_t socketControl_thread2;
    socketHandler =  findInputFunc(inputHead,"socket");
 
    if(socketHandler == NULL){
        printf("find socketHandler error\n");
        pthread_exit(NULL);
    }else{
        if(socketHandler->inputCmdInit(socketHandler) < 0){       //“网络模块”功能初始化
            printf("socketHandler init error\n");
            pthread_exit(NULL);
        }else{
            printf("socketHandler init success\n");
        }
		while(1){
				socketHandler->cfd = accept(socketHandler->sfd,NULL,NULL);
				
        		printf("connect done!\n");
			
			pthread_create(&socketControl_thread2,NULL,socketControlHandler2,NULL);
    	}

	}
}

int main(){
			
		
		pthread_t socketControl_thread;
		if (wiringPiSetup () == -1) { 
	        fprintf (stdout, "Unable to start wiringPi: %s\n", strerror (errno)) ; 
	        return 1 ; 
	   		}
		//指令工厂初始化
		inputHead = socketLink(inputHead);
		
		 //创造线程
 		pthread_create(&socketControl_thread,NULL,socketControlHandler,NULL);
   		 
		 pthread_join(socketControl_thread, NULL);//主函数等待线程退出
	return 0;
}

inputCommand.h

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>      
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <wiringPi.h>
#include <wiringSerial.h>




struct inputCmd {
	char deviceName[32];	//设备名
	char cmdName[32];		//指令工厂的名字
	char getCmd[16];	//存放的指令
	int fd;			//文件描述符
	int sfd;   //服务器套接字描述符
	int cfd;
	int port;	//端口
	char address[32];	//ip地址
	int (*inputCmdInit)(struct inputCmd* voice);	//初始化函数
	int (*inputGetCmd)(struct inputCmd* voice);		//获取指令函数
	struct inputCmd *next;		

};

struct inputCmd* voiceLink(struct inputCmd* PVoice);
struct inputCmd* socketLink(struct inputCmd* Psocket);

socketControl.c

#include"inputCmd.h"

/*struct inputCmd {
	char deviceName[32];
	char cmdName[32];
	char getCmd[16];
	int fd;
	int sfd;
	char port[12];
	char address[32];
	int (*inputCmdInit)(struct inputCmd* voice);
	int (*inputCmdGetCmd)(struct inputCmd* voice);
	struct inputCmd *next;

}*/

int InitSocket(struct inputCmd* Psocket)
{
	//1.socket
	struct sockaddr_in s_addr;
	memset(&s_addr,0,sizeof(struct sockaddr_in));	//清空数据
	int s_fd = socket(AF_INET,SOCK_STREAM,0);	//创建套接字,ipv4 tcp协议
	 //若创建套接字失败
        if(s_fd == -1){
                                                                      
                perror("socket");
                exit(-1);
        }
       //为套接字添加信息。
        s_addr.sin_family = AF_INET;	//协议族,TCP协议

        s_addr.sin_port = htons(Psocket->port);

        inet_aton(Psocket->address,&s_addr.sin_addr);
		//2.bind
        bind(s_fd, (struct sockaddr *)&s_addr,sizeof(struct sockaddr));
		
		//3.listen
		listen(s_fd,10);//监听10个连接
	    printf("socket Server listening ...\n");
		
    Psocket->sfd = s_fd;
    return s_fd;

}
int socketGetCmd(struct inputCmd* Psocket)
{
	int n_read=0;
	n_read = read(Psocket->cfd,Psocket->getCmd,sizeof(Psocket->getCmd));
	return n_read;
}


struct inputCmd socket1 = {
	.cmdName="socket",
	.inputCmdInit=InitSocket,
	.next=NULL,
	.sfd=0,
	.port=8808,
	.address="192.168.1.246",
	.cfd = 0,
	.getCmd='\0',
	.inputGetCmd=socketGetCmd

};

struct inputCmd* socketLink(struct inputCmd* Psocket)
{
	if(Psocket==NULL){
		return &socket1;
	}else{
		socket1.next = Psocket;
		Psocket = &socket1;
		return Psocket;
	}

}

测试结果

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值