___________________________________
主机操作系统:Centos 6.5
交叉编译器环境:arm-linux-gcc-4.5.4
开发板平台: FL2440
Linux内核版本: 3.0
作者:songyong<[email protected]>
___________________________________
RT3070无线上网卡
Ds18b20温度传感器
实现功能:开发板通过无线上网卡连入路由器,并以开发板为服务器,接收当前VLAN局域网内请求连接的多个客户端,并与之进行通信
Server服务器端:
/********************************************************************************* * Copyright: (C) 2015 songyong<handy_skyoutlook.com> * All rights reserved. * * Filename: inet_server.c * Description: This file * * Version: 1.0.0(2015年09月18日) * Author: sky <[email protected]> * ChangeLog: 1, Release initial version on "2015年09月18日 22时32分02秒" * ********************************************************************************/ #include <stdio.h> #include <fcntl.h> #include <stdlib.h> #include <string.h> #include <sys/socket.h> #include <netinet/in.h> #include <unistd.h> #include <error.h> #include <errno.h> #include <pthread.h> #define LEN 1024 #define LE 124 #define DEFAULT_PORT 2222 #define CONVERT 0.0625 #define TWO 2 void *chat(void *arg); /******************************************************************************** * Description: * Input Args: * Output Args: * Return Value: ********************************************************************************/ int main (int argc, char **argv) { int socket_fd,connect_fd; struct sockaddr_in servaddr; char data_buf[LEN]; int n; int flag = 1,len = sizeof(int); pthread_t tid; if( (socket_fd = socket(AF_INET, SOCK_STREAM,0)) == -1) { printf("create socket error: %s(errno: %d)\n",strerror(errno),errno); exit(0); } if(setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR, &flag,len)) { perror("setsockopt"); exit(1); } memset(&servaddr,0,sizeof(LEN)); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(DEFAULT_PORT); servaddr.sin_addr.s_addr = htonl(INADDR_ANY); if( bind(socket_fd,(struct sockaddr *)&servaddr, sizeof(servaddr)) == -1) { printf("bind socket error: %s(errno: %d)\n",strerror(errno),errno); exit(0); } if( listen(socket_fd,10) == -1) { printf("listen socket error: %s(errno: %d)\n",strerror(errno),errno); exit(0); } printf("Please waiting for client's request.....\n"); while(1) { struct sockaddr_in client_addr; socklen_t length = sizeof(client_addr); if( (connect_fd = accept(socket_fd, (struct sockaddr *)(&client_addr), &length)) < 0) { printf("accept socket error: %s(errno: %d)",strerror(errno),errno); exit(0); } if(0 != pthread_create(&tid, NULL, chat, (void *)&connect_fd) ) { printf("pthread create fail!\n"); return -1; } } close(socket_fd); return 0; } /* ----- End of main() ----- */ void *chat(void *arg) { char recv_buf[LEN]; char send_buf[LEN]; int choice_fd; int ret; int connect_fd = *((int *)arg); int temp_fd; int data =0; unsigned char result[TWO]; float temperature = 0; char tem_buf[LE]; #if 0 struct timeval tv; tv.tv_sec = 0; tv.tv_usec =700; #endif printf("ADD NEW Client%d !\n",connect_fd); while(1) { fd_set server_fd_set; FD_ZERO(&server_fd_set); FD_SET(STDIN_FILENO,&server_fd_set); FD_SET(connect_fd,&server_fd_set); ret = select(connect_fd+1,&server_fd_set,NULL,NULL, NULL); if(ret == 0) { printf("time out.\n"); return 0; } if(ret < 0) { printf("select error\n"); return 0; } if(FD_ISSET(connect_fd, &server_fd_set)) { if(recv(connect_fd, recv_buf, sizeof(recv_buf),0) < 0) { printf("recv fail\n"); return 0; } if(strcmp("close",recv_buf) == 0) exit(0); else if(strcmp("TT",recv_buf) == 0) { if((temp_fd = open("/dev/ds18b20",O_RDWR|O_NONBLOCK)) < 0) { perror("open ds18b20 device fail.\n"); return NULL; } if(2 != read(temp_fd, result, sizeof(result)) ) { printf("read temperature wrong!!\n"); } data = (int)result[1]; data <<=8; data |= result[0]; temperature = data * CONVERT; printf("Temperature:%.2f\n",temperature); sprintf(tem_buf,"Temperature:%.2f℃",temperature); write(connect_fd,tem_buf,sizeof(tem_buf)); } else{ printf("recv msg from client%d:\n",connect_fd); printf("%s\n",recv_buf); } } if(FD_ISSET(STDIN_FILENO,&server_fd_set)) { printf("You will send msg to client:\n"); fflush(stdin); scanf("%s",send_buf); printf("%s\n",send_buf); if(strcmp(send_buf,"close") == 0) exit(0); printf("爹,您想发信息给哪个儿子?\n"); scanf("%d",&choice_fd); write(choice_fd,send_buf,sizeof(send_buf)); } } //close(connect_fd); }Client客户端:
/********************************************************************************* * Copyright: (C) 2015 songyong<handy_skyoutlook.com> * All rights reserved. * * Filename: inet_sclient.c * Description: This file * * Version: 1.0.0(2015年09月29日) * Author: sky <[email protected]> * ChangeLog: 1, Release initial version on "2015年09月29日 14时53分19秒" * ********************************************************************************/ #include <stdio.h> #include <unistd.h> #include <fcntl.h> #include <stdlib.h> #include <sys/socket.h> #include <pthread.h> #include <netinet/in.h> #include <string.h> #include <error.h> #include <errno.h> #define DEFAULLT_PORT 2222 #define LEN 1024 void *chat(void *arg); /******************************************************************************** * Description: * Input Args: * Output Args: * Return Value: ********************************************************************************/ int main (int argc, char **argv) { struct sockaddr_in clientaddr; int sockfd; if(argc != 2) { printf("Usage: ./client <ipaddress>\n"); exit(0); } if( (sockfd = socket(AF_INET, SOCK_STREAM,0)) < 0) { printf("create socket error:%s(errno: %d)\n",strerror(errno),errno); exit(0); } memset(&clientaddr,0,sizeof(clientaddr)); clientaddr.sin_family = AF_INET; clientaddr.sin_port = htons(DEFAULLT_PORT); if(inet_pton(AF_INET, argv[1], &clientaddr.sin_addr) <= 0) { printf("inet_pton error for %s\n",argv[1]); exit(0); } if(connect(sockfd, (struct sockaddr *)&clientaddr, sizeof(clientaddr)) < 0) { printf("error:%s(errno:%d)\n",strerror(errno),errno); //printf("connect to %s fail!\n",argv[1]); exit(0); } pthread_t tid; if( (pthread_create(&tid, NULL, chat, (void *)&sockfd)) != 0) { printf("error:%s(errno:%d)\n",strerror(errno),errno); exit(0); } printf("now con\n"); while(1); close(sockfd); return 0; } /* ----- End of main() ----- */ void *chat(void *arg) { char recv_buf[LEN]; char send_buf[LEN]; int ret; int sockfd = *((int *)arg); struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 600; while(1) { fd_set client_set_fd; FD_ZERO(&client_set_fd); FD_SET(STDIN_FILENO, &client_set_fd); FD_SET(sockfd, &client_set_fd); ret = select(sockfd+1,&client_set_fd, NULL, NULL, NULL); if(ret == 0) { printf("time out!\n"); exit(0); } if(ret < 0) { printf("select failed\n"); exit(0); } if(FD_ISSET(sockfd, &client_set_fd)) { if(recv(sockfd, recv_buf, sizeof(recv_buf),0) >0) { printf("recv msg from Server:\n"); printf("%s\n",recv_buf); if(strcmp(recv_buf,"quit") == 0) exit(0); } } if(FD_ISSET(STDIN_FILENO,&client_set_fd)) { printf("You will send msg to Server:\n"); fflush(stdin); scanf("%s",send_buf); printf("%s\n",send_buf); if( strcmp(send_buf,"quit") == 0) exit(0); write(sockfd,send_buf,sizeof(send_buf)); } } }
server服务器端:
client端:
套套命令可获取服务器端当前温度。
close可以在客户端让服务器断开。quit可以关闭客户端。实现端口复用,但是还没有使用心跳包检测机制。很简陋,图形界面也没有美化,不过程序实现了功能这些都是虚的。10月份找到工作以后再来修复和记录文档吧。偏头痛,睡觉去。。。