完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
自己临时写的一个小应用,手机端发送一个字符,服务器进行判断,然后控制led,差不多像是第一次接触51单片机的那个点灯程序。 应用程序 #include #include #include #include #include #include #include #include #include #include #include #include #include #define MYPORT 8887 #define QUEUE 20 #define BUFFER_SIZE 1024 char buffer1[]="test successn" ; int change,test_flag=0; int fd; int server_sockfd; char buffer[BUFFER_SIZE]; int conn; int function1() { change=8; printf("enter function1n"); //int fd; /* 1.打开设备节点 */ fd = open("/dev/xiaoqikeji", O_RDWR | O_NONBLOCK); //注意fd 下面将用到 既读又写模式 非阻塞模式 if (fd < 0) { printf("can't open!n"); return -1; } ///定义sockfd server_sockfd = socket(AF_INET,SOCK_STREAM, 0); ///定义sockaddr_in struct sockaddr_in server_sockaddr; server_sockaddr.sin_family = AF_INET; server_sockaddr.sin_port = htons(MYPORT); server_sockaddr.sin_addr.s_addr = htonl(INADDR_ANY); // 设置套接字选项避免地址使用错误 int on=1; if((setsockopt(server_sockfd,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on)))<0) { perror("setsockopt failed"); exit(1); } ///bind,成功返回0,出错返回-1 if(bind(server_sockfd,(struct sockaddr *)&server_sockaddr,sizeof(server_sockaddr))==-1) { //test_flag++; //标志位 perror("bind"); //system("mysocket1"); //printf("try again"); //if(test_flag==3) exit(1); //如果连了三次 还是如此 则退出整个函数 //return 0; } ///listen,成功返回0,出错返回-1 if(listen(server_sockfd,QUEUE) == -1) { perror("listen"); exit(1); } ///客户端套接字 //char buffer[BUFFER_SIZE]; struct sockaddr_in client_addr; socklen_t length = sizeof(client_addr); ///成功返回非负描述字,出错返回-1 conn = accept(server_sockfd, (struct sockaddr*)&client_addr, &length); if(conn<0) { perror("connect"); exit(1); } change=1; printf("exit function1n"); //return 0; } int function2() { printf("enter function2n"); while(1) { test_flag=0; //标志位清零 printf("enter while functionn"); memset(buffer,0,sizeof(buffer)); int len = recv(conn, buffer, sizeof(buffer),0); if(!(strncmp(buffer,"exit",4 ))) //if(buffer[0]=='0') { printf("exit whole functionn"); change=8; //退出整个函数 break; } if(buffer[0]==' |