⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 net.cpp

📁 嵌入式LINUX的智能家居系统源码
💻 CPP
字号:
#include"net.h"#include<stdio.h>#include<pthread.h>#include<sys/types.h>#include<netinet/in.h>#include<sys/socket.h>#include<unistd.h>#include<fcntl.h>#include<sys/stat.h>#include<netdb.h>#include<string.h>#define SERVPORT  3333#define SERVPORT_V 3334#define IMG_LEN 4000#define BACKLOG 3#define filename "shot.jpeg"net * n;char  audio_play_buf[AUDIO_BUF_SIZE];char  audio_rec_buf [AUDIO_BUF_SIZE];int sockfd;int sockfd_v;bool receive_stop;bool receive_v_stop;void * receiver(void * data){	data = data;	int count;	while(!receive_stop)	{		if((count = recv(sockfd,audio_play_buf,AUDIO_BUF_SIZE,0))>0)		{			n->emit_play(count);		}		else 		{			//printf("audio receive error \n");			return NULL;		}	}	return NULL;}void net::emit_play(int count){	emit sig_play(count);}void senda(char * a,int len){	send(sockfd,a,len,0);}void net::send_audio(int count){	senda(audio_rec_buf,count);}char img[IMG_LEN];char send_ins[]="one send is ended";void * receive_v(void * data){	data = data;	int  fd;	int count;	while(!receive_v_stop)	{		if((fd = open(filename,O_WRONLY))<0)				printf("open img file error \n");		while(1)		{			if((count=recv(sockfd_v,img,IMG_LEN,0))>0)			{				//printf("received bytes %d \n",count);			}			if(strstr(img,send_ins)!=NULL)			{				break;			}			if((count = write(fd,img,count))<0)				printf("write imge error \n");					}		//printf("one img received \n");		close(fd);		n->emit_refresh_img();			}	return NULL;}void net::emit_refresh_img(){	emit sig_refresh_img();}void net::start_receive_v(){	receive_v_stop = false;	pthread_create(&th_v_receiver,NULL,receive_v,NULL);}void net::stop_receive_v(){	receive_v_stop = true;}net::net(){	n = this;	mpaudio_play_buf = audio_play_buf;	mpaudio_rec_buf= audio_rec_buf;	receive_stop = true;	receive_v_stop = true;}net::~net(){}void net::contact_consumer(){	emit s_contact_consumer(audio_play_buf,audio_rec_buf);}void init_connection(){       printf("connecting... \n");    int ser_fd,sin_size;    int ser_fd_v;    void * retval;    struct sockaddr_in my_addr,their_addr;    if((ser_fd = socket(AF_INET,SOCK_STREAM,0))==-1)	printf("ser socket create error ... \n");    my_addr.sin_family = AF_INET;    my_addr.sin_port = htons(SERVPORT);    my_addr.sin_addr.s_addr = INADDR_ANY;    bzero(&(my_addr.sin_zero),8);    if(bind(ser_fd,(struct sockaddr *)&my_addr,sizeof(struct sockaddr))==-1)	printf("audio bind error \n");    if(listen(ser_fd,BACKLOG)==-1)	printf("audio listen error \n");    if((sockfd = accept(ser_fd,(struct sockaddr *)&their_addr,(socklen_t*)&sin_size))==-1)	printf("audio accept error \n");    else printf("audio connected ...\n");        if((ser_fd_v = socket(AF_INET,SOCK_STREAM,0))==-1)	printf(" video ser socket create error ... \n");    my_addr.sin_family = AF_INET;    my_addr.sin_port = htons(SERVPORT_V);    my_addr.sin_addr.s_addr = INADDR_ANY;    bzero(&(my_addr.sin_zero),8);    if(bind(ser_fd_v,(struct sockaddr *)&my_addr,sizeof(struct sockaddr))==-1)	printf("video bind error \n");    if(listen(ser_fd_v,BACKLOG)==-1)	printf("video listen error \n");    if((sockfd_v = accept(ser_fd_v,(struct sockaddr *)&their_addr,(socklen_t*)&sin_size))==-1)	printf("video accept error \n");    else printf("video connected video ...\n");    }void net::init_net(){	init_connection();}void net::start_receive(){	receive_stop = false;	pthread_create(&th_receiver,NULL,receiver,NULL);}void net::stop_receive(){	receive_stop = true;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -