录像socket服务端 udp socket: 简单的数据包与视频流传输

udp socket: 简单的数据包与视频流传输


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <arpa/inet.h>

#define RCV_BUF_LEN 200

int check_cmd(char *cmd)
{
	int i,g=0,k;
	char dest[3]={};
	char quest[sizeof(cmd)-2];
	if(strncmp(cmd,"shutdown",8) ==0)
	{
		printf("now is shutdown!\n");
		system("halt -p");
		return 0;
	}
	if(strncmp(cmd,"halt",4) ==0)
	{
		printf("now is reboot!\n");
		system("reboot");
		return 0;
	}
	strncpy(dest,cmd,2);
	//dest[3]='\0';//没有会乱码
//******输入qu231 取后面231的值
	if(strncmp(dest,"qu",2) ==0){
		FILE *fq;
		strcpy(quest,cmd+2);//取后两位
		int w=atoi(quest);	
		printf("w:%d\n",w);
		fq=fopen("/home/root/retime.txt","wb");
		fprintf(fq,"%d\n",w);
		fclose(fq);
		return 0;
	}
//开机是否自动启动录像

	if(strncmp(cmd,"shi",3) ==0)
	{
		FILE *fp;
		//int l=1;
		fp=fopen("/home/root/h264.c","wb");
		fprintf(fp,"%d\n",1);
		fclose(fp);
		system("(./hello &)");
		return 0;
	}
	if(strncmp(cmd,"fou",3) ==0)
	{
		
		FILE *fp;
		//int p=0;
		fp=fopen("/home/root/h264.c","wb");
		fprintf(fp,"%d\n",0);
		fclose(fp);
		system("killall hello");
		usleep(200);
		system("killall H264_UVC_TestAP");
		usleep(200);
		system("(./hello &)");
		return 0;
	}

	/*//输入录像时间,存在文件夹下
	for(i=0;i<(strlen(cmd));i++){
	  if(isdigit(cmd[i]))
		g++;	
	else
		break;
	}
	if(g==strlen(cmd))
		{k=atoi(cmd);
		FILE *fp;	
		fp=fopen("/home/root/retime.txt","wb");
		fprintf(fp,"%d\n",k);
		fclose(fp);
		return 0;	}	*/
}

int main( int ac, char **av )
{
	char dest1[3]={};
        int sock;
        int client_Sock;
        int ret;
        char inputBuf[200];
        char recvBuf[RCV_BUF_LEN];
        struct sockaddr_in svrAddr;
	
	int i;
        int val;


        /** 创建监听套接字 **/
        if ( (sock = socket(AF_INET, SOCK_STREAM, 0)) < 0 ){
                printf("socket error!\n");
                return -1;
        }

        /** 指定监听端口和地址 **/
        memset( &svrAddr, 0x00, sizeof(svrAddr) );
        svrAddr.sin_family =AF_INET;
        svrAddr.sin_addr.s_addr = htonl( INADDR_ANY );
        svrAddr.sin_port = htons( 5555 );

        setsockopt(sock,SOL_SOCKET,SO_REUSEADDR, (char *)&val, sizeof(val) );

        /** 绑定监听套接字和端口 **/
        if ( bind(sock, (struct sockaddr *)&svrAddr, sizeof(svrAddr)) != 0 ){
                printf("bind error!\n");
                return -1;
        }

        /** 监听套接字 */
        if ( listen( sock, 10 ) != 0 ){
                printf("listen error!\n");
                return -1;
        }

        while( 1 ){
                /** 等待连接请求 **/
                printf("Waitting Client connecting request..\n");
               /* if ( (client_Sock = accept( sock, NULL, 0 )) < 0 ){
                        printf("accept error!\n");
                        return -1;
                }*/
		client_Sock = accept( sock, NULL, 0 );
                printf("Client Connected..\n");

                while(client_Sock)
		{
                        memset( recvBuf, 0x00, RCV_BUF_LEN );
                        ret = recv( client_Sock, recvBuf, RCV_BUF_LEN, 0 );
                       // recvBuf[strlen(recvBuf) - 1] = 0;
			//recvBuf[strlen(recvBuf)] = '\0';
                        if ( ret > 0 )
			{
	        if(strncmp(recvBuf,"shi",3) ==0)
		    ret = send( client_Sock,"设置为录像模式", 40, 0 );
	        if(strncmp(recvBuf,"fou",3) ==0)
	            ret = send( client_Sock,"设置为非录像模式", 40, 0 );
	            strncpy(dest1,recvBuf,2);
	        if(strncmp(dest1,"qu",2) ==0)
		     ret = send( client_Sock,"设置录像时间Success", 40, 0 );
	        if(strncmp(recvBuf,"shutdown",8) ==0)
	 	    ret = send( client_Sock,"正在关机...", 20, 0 );
	        if(strncmp(recvBuf,"halt",4) ==0)
		    ret = send( client_Sock,"正在重启...", 20, 0 );
                printf("Receive: %s\n", recvBuf);
                check_cmd(recvBuf);

   }
			if(ret <= 0)
			{
				close(client_Sock);
				client_Sock =0 ;
			}
                }
		//close(client_Sock);
		printf("Client close..\n");
        }

	printf("it is done..\n");
        return 0;
}

猜你喜欢

转载自blog.csdn.net/u011426247/article/details/80552205