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

📄 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   4000net * pnet;char audio_play_buf[AUDIO_BUF_SIZE];char audio_rec_buf [AUDIO_BUF_SIZE];int sockfd;int sockfd_v;bool receive_stop;char audio_ins[]="audio interrupt between";void * receiver(void * data){	printf("audio net receive begin... \n");	int count = 0;	data = data;	while(!receive_stop)	{		if((count = recv(sockfd,audio_play_buf,AUDIO_BUF_SIZE,0))>0)		{			audio_play_buf[100]=0;			if(strstr(audio_play_buf,audio_ins) != NULL)				continue;			//printf("one audio frame sent play size : %d\n",count);			pnet->emit_play(count);					}			else 		{			//printf("audio receive error \n");			return NULL;		}		}	return NULL;}void net::emit_play(int len){	emit sig_play(len);}void senda(char * a ,int len){	send(sockfd,a,len,0);}void net::send_audio(int len){	senda(audio_rec_buf,len);	//printf("audio one frame sended size: %d \n",len);}char send_ins[]="one send is ended";void sendv(char * img,int len){	send(sockfd_v,img,len,0);	}char img[IMG_LEN];void net::trans_img(){	int count;	int fd;	fd=open("shot.jpeg",O_RDONLY);	do{		if((count =read(fd,img,IMG_LEN))>0)		{			sendv(img,count);			//printf("img's %d bypes sended \n",count);		}	}while(count >0);	//printf("one img sended \n");	//printf("%d \n",count);	close(fd);	sendv(send_ins,sizeof(send_ins)+1);}net::net(){	pnet = this;	mpaudio_play_buf = audio_play_buf;	mpaudio_rec_buf = audio_rec_buf;	receive_stop = true;}net::~net(){}void net::contact_consumer(){	emit s_contact_consumer(audio_play_buf,audio_rec_buf);}void net::start_receive(){	receive_stop = false;	pthread_create(&th_receiver,NULL,receiver,NULL);}void net::stop_receive(){	receive_stop = true;}void init_connection(){	printf("init ");	struct sockaddr_in serv_addr;	struct hostent * host;		host = gethostbyname("192.168.0.120");	sockfd = socket(AF_INET,SOCK_STREAM,0);	sockfd_v = socket(AF_INET,SOCK_STREAM,0);	serv_addr.sin_family = AF_INET;	serv_addr.sin_port=htons(SERVPORT);	serv_addr.sin_addr = *((struct in_addr *)host->h_addr);	bzero(&(serv_addr.sin_zero),8);		printf("connecting to PC server for audio... \n");			if(connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(struct sockaddr))==-1)		printf("connect to pc server error ... \n");	else printf("connected to pc server for audio \n");			for(int i=0;i<1000;i++)		for(int j=0;j<10000;j++);	serv_addr.sin_family = AF_INET;	serv_addr.sin_port=htons(SERVPORT_V);	serv_addr.sin_addr = *((struct in_addr *)host->h_addr);	bzero(&(serv_addr.sin_zero),8);		printf("connecting to pc video server \n");	if(connect(sockfd_v,(struct sockaddr *)&serv_addr,sizeof(struct sockaddr))==-1)		printf("connect to pc video server error ... \n");		else printf("connected  to pc server for video \n");}void net::init_net(){	init_connection();}

⌨️ 快捷键说明

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