基于Nanopi NEO开发板的套接字编程!
生活随笔
收集整理的這篇文章主要介紹了
基于Nanopi NEO开发板的套接字编程!
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
基于Nanopi 開發板的套接字編程!
用到友善之臂Friendarm的開發包Matrix,
下載地址:https://github.com/friendlyarm/matrix.git
?
#include <stdio.h> #include <stdlib.h> #include <strings.h> #include <sys/types.h> #include <sys/socket.h> #include <memory.h> #include <unistd.h>#include <netinet/in.h> #include <arpa/inet.h> #include <signal.h> #include "libfahw.h"#define PORT 8002 #define BACKLOG 5 #define buflen 1024 //void process_conn_server(int s); /*void sig_pipe(int signo);*/ int ss,sc; char buffer[buflen];int main(int argc, char ** argv) {//GPIO//Rightint pin_1 = GPIO_PIN(16);int pin_2 = GPIO_PIN(18);//Leftint pin_3 = GPIO_PIN(7);int pin_4 = GPIO_PIN(22);int pin_light = GPIO_PIN(12);int board;//Init boardif ((board = boardInit()) < 0) {printf("Fail to init board\n");return -1;}int ret;//Set GPIO ----->OUTPUTif ((ret = exportGPIOPin(pin_1)) == -1) { //通知系統需要導出控制的GPIO引腳編號 printf("exportGPIOPin(%d) failed\n", pin_1);}if ((ret = exportGPIOPin(pin_2)) == -1) { printf("exportGPIOPin(%d) failed\n", pin_2);}if ((ret = exportGPIOPin(pin_3)) == -1) { printf("exportGPIOPin(%d) failed\n", pin_3);}if ((ret = exportGPIOPin(pin_4)) == -1) { printf("exportGPIOPin(%d) failed\n", pin_4);}if ((ret = exportGPIOPin(pin_light)) == -1) { printf("exportGPIOPin(%d) failed\n", pin_light);}///if ((ret = setGPIODirection(pin_1, GPIO_OUT)) == -1) {printf("setGPIODirection(%d) failed\n", pin_1);}if ((ret = setGPIODirection(pin_2, GPIO_OUT)) == -1) {printf("setGPIODirection(%d) failed\n", pin_2);}if ((ret = setGPIODirection(pin_3, GPIO_OUT)) == -1) {printf("setGPIODirection(%d) failed\n", pin_3);}if ((ret = setGPIODirection(pin_4, GPIO_OUT)) == -1) {printf("setGPIODirection(%d) failed\n", pin_4);}if ((ret = setGPIODirection(pin_light, GPIO_OUT)) == -1) {printf("setGPIODirection(%d) failed\n", pin_light);}//Aount Socketstruct sockaddr_in server_addr; // server socket struct sockaddr_in client_addr; // client socketint err; //return /*并不是上面的type和protocol可以隨意組合的,如SOCK_STREAM不可以跟IPPROTO_UDP組合。當protocol為0時,會自動選擇type類型對應的默認協議。*/ss = socket(AF_INET,SOCK_STREAM,0); //Build socket,當socket的返回值為-1,時創建套接字失敗if(ss<0) { printf("server : server socket create error\n"); return -1; }memset(&server_addr,0,sizeof(server_addr)); //服務端:bind()server_addr.sin_family = AF_INET; //協議族決定了socket的地址類型為IPV4 /*服務器要綁定bind到本地的IP地址上進行監聽listen,但是你的機器上有好多網卡,也就是多個IP地址,這時候你要選擇綁定到哪個IP上面,如果指定為INADDR_ANY,系統將綁定默認的網卡(及IP地址)*/ server_addr.sin_addr.s_addr = htonl(INADDR_ANY); /*#define PORT 8002(端口號),服務器將套接字綁定到上面指定的IP和8002的端口上,服務器就在這個端口等待請求*/server_addr.sin_port = htons(PORT);err = bind(ss,(struct sockaddr *)&server_addr,sizeof(server_addr)); if(err<0) { printf("server : bind error\n"); return -1; }else{//printf("server : bind at %s\n", server_addr.sin_addr.s_addr); }err = listen(ss,BACKLOG); //Listen #define BACKLOG 5 (第二個參數為相應socket可以排隊的最大連接個數) if(err < 0) { printf("server : listen error\n"); return -1; }else{printf("server : listen at %d\n", PORT); }//main whileint size;setGPIOValue(pin_3, GPIO_LOW);setGPIOValue(pin_4, GPIO_LOW);setGPIOValue(pin_1, GPIO_LOW);setGPIOValue(pin_2, GPIO_LOW);setGPIOValue(pin_light, GPIO_LOW);while(1){size = 0;socklen_t addrlen = sizeof(client_addr);/*accept的第一個參數:參數sockfd就是上面解釋中的監聽套接字第二個參數:這是一個結果參數,它用來接受一個返回值,這返回值指定客戶端的地址。第三個參數:addrlen用來接受上述addr的結構的大小的,它指明addr結構所占有的字節個數。它也可以被設置為NULL。*/sc = accept(ss,(struct sockaddr *)&client_addr,&addrlen);/*accept的返回值:成功時:返回非負整數,該整數是接收到套接字的描述符出錯時:返回-1*/if(sc < 0) { continue; //如果sc小于0則繼續執行accept()} /*如果accept成功返回,則服務器與客戶已經正確建立連接了,此時服務器通過accept返回的套接字來完成與客戶的通信。*/else { printf("server : connected\n"); } for(;;){/*read函數是負責從fd中讀取內容.當讀成功時,read返回實際所讀的字節數,如果返回的值是0表示已經讀到文件的結束了,小于0表示出現了錯誤。如果錯誤為EINTR說明讀是由中斷引起的,如果是ECONNREST表示網絡連接出了問題。*/size = read(sc,buffer,buflen);printf("Size : %d\n", size);if(size > 0){//memset(buffer,'',sizeof(buffer));printf("From main:%s\n",buffer);if(buffer[0] == '1') {setGPIOValue(pin_1, GPIO_HIGH);setGPIOValue(pin_2, GPIO_LOW);setGPIOValue(pin_3, GPIO_HIGH);setGPIOValue(pin_4, GPIO_LOW);printf("GO!\n");}else if(buffer[0] == '2'){setGPIOValue(pin_1, GPIO_LOW);setGPIOValue(pin_2, GPIO_HIGH);setGPIOValue(pin_3, GPIO_LOW);setGPIOValue(pin_4, GPIO_HIGH);printf("Back!\n");}else if(buffer[0] == '3'){setGPIOValue(pin_1, GPIO_HIGH);setGPIOValue(pin_2, GPIO_LOW);setGPIOValue(pin_3, GPIO_LOW);setGPIOValue(pin_4, GPIO_HIGH);printf("Right!\n");}else if(buffer[0] == '4'){setGPIOValue(pin_3, GPIO_HIGH);setGPIOValue(pin_4, GPIO_LOW);setGPIOValue(pin_1, GPIO_LOW);setGPIOValue(pin_2, GPIO_HIGH);printf("Left!\n");}else{setGPIOValue(pin_3, GPIO_LOW);setGPIOValue(pin_4, GPIO_LOW);setGPIOValue(pin_1, GPIO_LOW);setGPIOValue(pin_2, GPIO_LOW);printf("Stop!\n");}if(buffer[1] == '1'){setGPIOValue(pin_light, GPIO_HIGH);printf("Open light.....\n");}else{setGPIOValue(pin_light, GPIO_LOW);printf("Close light.....\n");}size = 0;}else{printf("Loss connect......\n");break;}}} }//通知系統需要導出控制的GPIO引腳編號 printf("exportGPIOPin(%d) failed\n", pin_1);}if ((ret = exportGPIOPin(pin_2)) == -1) { printf("exportGPIOPin(%d) failed\n", pin_2);}if ((ret = exportGPIOPin(pin_3)) == -1) { printf("exportGPIOPin(%d) failed\n", pin_3);}if ((ret = exportGPIOPin(pin_4)) == -1) { printf("exportGPIOPin(%d) failed\n", pin_4);}if ((ret = exportGPIOPin(pin_light)) == -1) { printf("exportGPIOPin(%d) failed\n", pin_light);}///if ((ret = setGPIODirection(pin_1, GPIO_OUT)) == -1) {printf("setGPIODirection(%d) failed\n", pin_1);}if ((ret = setGPIODirection(pin_2, GPIO_OUT)) == -1) {printf("setGPIODirection(%d) failed\n", pin_2);}if ((ret = setGPIODirection(pin_3, GPIO_OUT)) == -1) {printf("setGPIODirection(%d) failed\n", pin_3);}if ((ret = setGPIODirection(pin_4, GPIO_OUT)) == -1) {printf("setGPIODirection(%d) failed\n", pin_4);}if ((ret = setGPIODirection(pin_light, GPIO_OUT)) == -1) {printf("setGPIODirection(%d) failed\n", pin_light);}//Aount Socketstruct sockaddr_in server_addr; // server socket struct sockaddr_in client_addr; // client socketint err; //return /*并不是上面的type和protocol可以隨意組合的,如SOCK_STREAM不可以跟IPPROTO_UDP組合。當protocol為0時,會自動選擇type類型對應的默認協議。*/ss = socket(AF_INET,SOCK_STREAM,0); //Build socket,當socket的返回值為-1,時創建套接字失敗if(ss<0) { printf("server : server socket create error\n"); return -1; }memset(&server_addr,0,sizeof(server_addr)); //服務端:bind()server_addr.sin_family = AF_INET; //協議族決定了socket的地址類型為IPV4 /*服務器要綁定bind到本地的IP地址上進行監聽listen,但是你的機器上有好多網卡,也就是多個IP地址,這時候你要選擇綁定到哪個IP上面,如果指定為INADDR_ANY,系統將綁定默認的網卡(及IP地址)*/ server_addr.sin_addr.s_addr = htonl(INADDR_ANY); /*#define PORT 8002(端口號),服務器將套接字綁定到上面指定的IP和8002的端口上,服務器就在這個端口等待請求*/server_addr.sin_port = htons(PORT);err = bind(ss,(struct sockaddr *)&server_addr,sizeof(server_addr)); if(err<0) { printf("server : bind error\n"); return -1; }else{//printf("server : bind at %s\n", server_addr.sin_addr.s_addr); }err = listen(ss,BACKLOG); //Listen #define BACKLOG 5 (第二個參數為相應socket可以排隊的最大連接個數) if(err < 0) { printf("server : listen error\n"); return -1; }else{printf("server : listen at %d\n", PORT); }//main whileint size;setGPIOValue(pin_3, GPIO_LOW);setGPIOValue(pin_4, GPIO_LOW);setGPIOValue(pin_1, GPIO_LOW);setGPIOValue(pin_2, GPIO_LOW);setGPIOValue(pin_light, GPIO_LOW);while(1){size = 0;socklen_t addrlen = sizeof(client_addr);/*accept的第一個參數:參數sockfd就是上面解釋中的監聽套接字第二個參數:這是一個結果參數,它用來接受一個返回值,這返回值指定客戶端的地址。第三個參數:addrlen用來接受上述addr的結構的大小的,它指明addr結構所占有的字節個數。它也可以被設置為NULL。*/sc = accept(ss,(struct sockaddr *)&client_addr,&addrlen);/*accept的返回值:成功時:返回非負整數,該整數是接收到套接字的描述符出錯時:返回-1*/if(sc < 0) { continue; //如果sc小于0則繼續執行accept()} /*如果accept成功返回,則服務器與客戶已經正確建立連接了,此時服務器通過accept返回的套接字來完成與客戶的通信。*/else { printf("server : connected\n"); } for(;;){/*read函數是負責從fd中讀取內容.當讀成功時,read返回實際所讀的字節數,如果返回的值是0表示已經讀到文件的結束了,小于0表示出現了錯誤。如果錯誤為EINTR說明讀是由中斷引起的,如果是ECONNREST表示網絡連接出了問題。*/size = read(sc,buffer,buflen);printf("Size : %d\n", size);if(size > 0){//memset(buffer,'',sizeof(buffer));printf("From main:%s\n",buffer);if(buffer[0] == '1') {setGPIOValue(pin_1, GPIO_HIGH);setGPIOValue(pin_2, GPIO_LOW);setGPIOValue(pin_3, GPIO_HIGH);setGPIOValue(pin_4, GPIO_LOW);printf("GO!\n");}else if(buffer[0] == '2'){setGPIOValue(pin_1, GPIO_LOW);setGPIOValue(pin_2, GPIO_HIGH);setGPIOValue(pin_3, GPIO_LOW);setGPIOValue(pin_4, GPIO_HIGH);printf("Back!\n");}else if(buffer[0] == '3'){setGPIOValue(pin_1, GPIO_HIGH);setGPIOValue(pin_2, GPIO_LOW);setGPIOValue(pin_3, GPIO_LOW);setGPIOValue(pin_4, GPIO_HIGH);printf("Right!\n");}else if(buffer[0] == '4'){setGPIOValue(pin_3, GPIO_HIGH);setGPIOValue(pin_4, GPIO_LOW);setGPIOValue(pin_1, GPIO_LOW);setGPIOValue(pin_2, GPIO_HIGH);printf("Left!\n");}else{setGPIOValue(pin_3, GPIO_LOW);setGPIOValue(pin_4, GPIO_LOW);setGPIOValue(pin_1, GPIO_LOW);setGPIOValue(pin_2, GPIO_LOW);printf("Stop!\n");}if(buffer[1] == '1'){setGPIOValue(pin_light, GPIO_HIGH);printf("Open light.....\n");}else{setGPIOValue(pin_light, GPIO_LOW);printf("Close light.....\n");}size = 0;}else{printf("Loss connect......\n");break;}}} }?
?
?
?
?
總結
以上是生活随笔為你收集整理的基于Nanopi NEO开发板的套接字编程!的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 在QT的LineEdit框中输入特定字符
- 下一篇: MySQL笔记13:查询结果集