|
本帖最后由 blackbear008 于 2014-4-15 15:49 编辑
一直想用RaspberryPI做一个家用的NAS,但苦于没有合适的机箱。感谢朋友老冯送了我一个ODF箱子,比我以前的盒子强多了。以前的盒子是这样的:
老冯送的是 这个样子的:
哈哈,比我的漂亮多了。不说了,直接 上图,全家福和小RPI的特写:
盒子的空间比较小,怕硬盘会发生共振,产生噪音,就用自行车内胎粘在了硬盘盒外面,起到减震的作用。
组装好后的图:
为了外接网络方便 与速度 ,这里我选了一个有线网线接口,没用无线网络,因为想用RPI做下载器用,无线太慢了,查了一个,USB无线网卡的速度一直在54M左右 ,可能是我的无线路由是54M的原因。所以直接 用的有线。
硬盘要比盒子的高度高2mm,所以上了上盖就可以固定,其它配件基本不能特别固定就可以在盒子里待住 ,所以就没用粘带或螺钉。
安装完成的效果是这样的:
哈哈,感觉效果还成,放在了沙发后面与墙面的缝里,刚刚好。就是盒子有点重,大概全下来要1KG左右 。
在RPI上安装了Samba和Vsftpd,并打开了路由器的dhcp保留IP,这样RPI就能在我家里的路由下有固定的IP地址分配,同时又在路由器上打开了端口转发,这样外网可以直接进RPI。
又再了一段C语言程序 ,可以让RPI定时向我的服务器连接,以得到我家的IP公网地址。client.c 和server.c分别如下:- /*************************************
- 文件名: client+timer.c
- linux 下socket网络编程简例 - 客户端程序
- 服务器端口设为 8888 (端口和地址可根据实际情况更改,或者使用参数传入)
-
- 作者:blackbear080#163.com (将#换为@)
- */
-
- #include <stdlib.h>
- #include <sys/types.h>
- #include <stdio.h>
- #include <sys/socket.h>
- #include <linux/in.h>
- #include <string.h>
- #include <signal.h>
- #include <sys/time.h>
-
- #include <errno.h>
-
- char *Server_IP;
-
- void sigFunc()
- {
- //static int iCnt = 0;
- //printf("The %d Times: Hello world\n", iCnt++);
- //return 0;
- int cfd; /* 文件描述符 */
- int recbytes;
- int sin_size;
- char buffer[1024]={0}; /* 接受缓冲区 */
- struct sockaddr_in s_add,c_add; /* 存储服务端和本端的ip、端口等信息结构体 */
- unsigned short portnum=8888; /* 服务端使用的通信端口,可以更改,需和服务端相同 */
-
- printf("Hello,welcome to client !\r\n");
- /* 建立socket 使用因特网,TCP流传输 */
- cfd = socket(AF_INET, SOCK_STREAM, 0);
- if(-1 == cfd)
- {
- printf("socket fail ! \r\n");
- //return -1;
- }
- printf("socket ok !\r\n");
- /* 构造服务器端的ip和端口信息,具体结构体可以查资料 */
- bzero(&s_add,sizeof(struct sockaddr_in));
- s_add.sin_family=AF_INET;
- s_add.sin_addr.s_addr= inet_addr(Server_IP); /* ip转换为4字节整形,使用时需要根据服务端ip进行更改 */
- s_add.sin_port=htons(portnum); /* 这里htons是将short型数据字节序由主机型转换为网络型,其实就是
- 将2字节数据的前后两个字节倒换,和对应的ntohs效果、实质相同,只不过名字不同。htonl和ntohl是
- 操作的4字节整形。将0x12345678变为0x78563412,名字不同,内容两两相同,一般情况下网络为大端,
- PPC的cpu为大端,x86的cpu为小端,arm的可以配置大小端,需要保证接收时字节序正确。
- */
-
- printf("s_addr = %#x ,port : %#x\r\n",s_add.sin_addr.s_addr,s_add.sin_port); /* 这里打印出的是小端
- 和我们平时看到的是相反的。 */
-
- /* 客户端连接服务器,参数依次为socket文件描述符,地址信息,地址结构大小 */
- if(-1 == connect(cfd,(struct sockaddr *)(&s_add), sizeof(struct sockaddr)))
- {
- printf("connect fail !\r\n");
- //return -1;
- }
- printf("connect ok !\r\n");
-
- //向服务端提交identify code
-
-
- if(-1 == write(cfd,"bearhome2233",sizeof("bearhome2233")))
- {
- printf("Send identify code fail!\r\n");
- //return -1;
- }
- printf("Send identify code ok!\r\n");
-
-
-
-
- /*连接成功,从服务端接收字符*/
- if(-1 == (recbytes = read(cfd,buffer,1024)))
- {
- printf("read data fail !\r\n");
- //return -1;
- }
- printf("read ok\r\nREC:\r\n");
-
- buffer[recbytes]='\0';
- printf("%s\r\n",buffer);
-
- sleep(1); /* 此句为使程序暂停在此处,可以使用netstat查看当前的连接 */
- close(cfd); /* 关闭连接,本次通信完成 */
- //return 0;
-
-
- }
-
- int main(int argc , char *argv[])
- {
- Server_IP = argv[1];
- struct itimerval tv, otv;
- signal(SIGALRM, sigFunc);
- //how long to run the first time
- tv.it_value.tv_sec = 3;
- tv.it_value.tv_usec = 0;
- //after the first time, how long to run next time
- tv.it_interval.tv_sec = 300;
- tv.it_interval.tv_usec = 0;
-
- if (setitimer(ITIMER_REAL, &tv, &otv) != 0)
- printf("setitimer err %d\n", errno);
-
- while(1)
- {
- sleep(1);
- //printf("otv: %d, %d, %d, %d\n", otv.it_value.tv_sec, otv.it_value.tv_usec, otv.it_interval.tv_sec, otv.it_interval.tv_sec);
- }
- }
复制代码- /*************************************
- 文件名: server.c
- linux 下socket网络编程简例 - 服务端程序
- 服务器端口设为 8888 (端口和地址可根据实际情况更改,或者使用参数传入)
- 作者:kikilizhm#163.com (将#换为@)
- */
-
- #include <stdlib.h>
- #include <sys/types.h>
- #include <stdio.h>
- #include <sys/socket.h>
- #include <linux/in.h>
- #include "string.h"
- #include <time.h>
-
- int main()
- {
- int sfp,nfp; /* 定义两个描述符 */
- struct sockaddr_in s_add,c_add,old_add;
- int sin_size;
- unsigned short portnum=8888; /* 服务端使用端口 */
- FILE *logfile;
-
- //用于提取服务器时间
- time_t t_secs; /*计数秒数*/
- struct tm *t_info; /*数据元素:年月日时分秒等*/
- char *t_str; /*不能使用数组名接收返回值*/
-
-
-
- printf("Hello,welcome to my server !\r\n");
- sfp = socket(AF_INET, SOCK_STREAM, 0);
- if(-1 == sfp)
- {
- printf("socket fail ! \r\n");
- return -1;
- }
- printf("socket ok !\r\n");
-
- /* 填充服务器端口地址信息,以便下面使用此地址和端口监听 */
- bzero(&s_add,sizeof(struct sockaddr_in));
- s_add.sin_family=AF_INET;
- s_add.sin_addr.s_addr=htonl(INADDR_ANY); /* 这里地址使用全0,即所有 */
- s_add.sin_port=htons(portnum);
-
- old_add.sin_addr.s_addr=htonl(INADDR_ANY);
-
- /* 使用bind进行绑定端口 */
- if(-1 == bind(sfp,(struct sockaddr *)(&s_add), sizeof(struct sockaddr)))
- {
- printf("bind fail !\r\n");
- return -1;
- }
- printf("bind ok !\r\n");
- /* 开始监听相应的端口 */
- if(-1 == listen(sfp,5))
- {
- printf("listen fail !\r\n");
- return -1;
- }
- printf("listen ok\r\n");
-
-
- //用于接收client 识别码
- char identify_code[20] = {'\0'};
-
- while(1)
- {
- sin_size = sizeof(struct sockaddr_in);
- /* accept服务端使用函数,调用时即进入阻塞状态,等待用户进行连接,在没有客户端进行连接时,程序停止在此处,
- 不会看到后面的打印,当有客户端进行连接时,程序马上执行一次,然后再次循环到此处继续等待。
- 此处accept的第二个参数用于获取客户端的端口和地址信息。
- */
- nfp = accept(sfp, (struct sockaddr *)(&c_add), &sin_size);
- if(-1 == nfp)
- {
- printf("accept fail !\r\n");
- return -1;
- }
- //printf("accept ok!\r\nServer start get connect from %s : %#x\r\n",ntohl(c_add.sin_addr.s_addr),ntohs(c_add.sin_port));
- printf("accept ok!\r\nServer start get connect from %s : %d\r\n",
- inet_ntoa(c_add.sin_addr.s_addr),
- ntohs(c_add.sin_port));
-
-
- memset(identify_code,'\0',sizeof(identify_code));
-
- if(-1 != read(nfp,identify_code,sizeof(identify_code)))
- {
- printf("identify_code is:%s\r\n",identify_code);
- char *tempA=identify_code;
- char *tempB="bearhome2233";
- if(strcmp(tempA,tempB) == 0)
- {
- //写logfile日志
- //printf("loging\r\n");
- if(old_add.sin_addr.s_addr != c_add.sin_addr.s_addr )
- {
- printf("New Client IP address\r\n");
- old_add.sin_addr.s_addr = c_add.sin_addr.s_addr ;
- logfile=fopen("index.php","w+");
- //fprintf(logfile,"accept ok! Server start get connect from %s:%d\r\n",
-
- //取服务器时间
- time(&t_secs);
- fprintf(logfile,"<?php header("refresh:5;url=http://%s/")?> \
- <html>Home IP is %s, and will Jump in 5 seconds.
- \
- Client IP update at :%s
- \
- Client Port: %d</html>",
- inet_ntoa(c_add.sin_addr.s_addr),
- inet_ntoa(c_add.sin_addr.s_addr),
- ctime(&t_secs),
- ntohs(c_add.sin_port));
- fflush(logfile);
- fclose(logfile);
- //printf("logfile flush\r\n");
- }
- }
- else printf("Identify Code error!");
- }
- else{printf("read identify_code error!\r\n");}
-
- /* 这里使用write向客户端发送信息,也可以尝试使用其他函数实现 */
- if(-1 == write(nfp,"hello,welcome to my server \r\n",32))
- {
- printf("write fail!\r\n");
- return -1;
- }
- printf("write ok!\r\n");
- close(nfp);
-
- }
- close(sfp);
- return 0;
- }
复制代码 |
-
-
|