TA的每日心情 | 难过 2017-4-18 09:57 |
---|
签到天数: 7 天 连续签到: 1 天 [LV.3]偶尔看看II
|
系列阅读:https://jingyan.eeboard.com/category/53
本次试用也快接近尾声了,最近就想在BBB平台上多试试各种应用。首先让我想到的就是socket网络方面的应用
这里先简单把调试过程简述一下,目的只是通过SOCKET编程,将BBB和PC建立连接,传送数据。
这里,PC作为服务端,而BBB自然就是客户端了
1,服务端的调试,以下是修改拿来代码后的样子,原来的代码经过gcc seve.cpp -o servNet 会类型装换无效的错,
将accept()函数中参数&lengt,经过强制类型装换成 (socklen_t*)&length就可以了
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include<stdio.h>
#include<string.h>
#include <unistd.h>
#define MY_IP "192.168.1.106"
#define MY_PORT 10010
int main(void)
{
int length = 0;
char server_buffer[128] = {0};
char client_buff[128] = {12,22,33,66};
struct sockaddr_in server_addr = {0};
struct sockaddr_in client_addr = {0};
int socket_fd = socket(AF_INET, SOCK_STREAM, 0);
if(socket_fd < 0){
perror("socket_fd");
return -1;
}
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(MY_PORT);
server_addr.sin_addr.s_addr = inet_addr(MY_IP);
int ret = bind(socket_fd, (struct sockaddr *)&server_addr, sizeof(server_addr));
if(ret < 0){
perror("bind");
return -1;
}
ret = listen(socket_fd, 10);
if(ret < 0){
perror("listen");
return -1;
}
printf("Server is OK...\n");
length = sizeof(client_addr);
int client_fd = accept(socket_fd, (struct sockaddr *)&client_addr, (socklen_t*)&length);
if(client_fd < 0){
perror("accept error.\n");
return -1;
}
printf("Connected from %s, Port is %d\n",inet_ntop(AF_INET,
&client_addr.sin_addr, client_buff, sizeof(client_buff)),
ntohs(client_addr.sin_port));
while(1){
memset(server_buffer, 0, sizeof(server_buffer));
ret = recv(client_fd, server_buffer, sizeof(server_buffer), 0);
printf("[%s].\n", server_buffer);
}
close(socket_fd);
return 0;
}
2,客户端的调试,将拿来的代码,修改各自的IP地址就可以了,对于buffer内的数据,可以用动态CPU温度值或者其它自定义数据做一下模拟,基于这个模型可以产生很多应用,视应用情况而定。
- #include<stdio.h>
- #include<string.h>
- #include<strings.h>
- #include <ctype.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <time.h>
- #include <sys/socket.h>
- #include <arpa/inet.h>
- #include <netinet/in.h>
- #include <sys/types.h>
- #include <sys/time.h>
- #include <fcntl.h>
- #include <sys/stat.h>
- #define SERVER_IP "192.168.1.106"
- #define SERVER_PORT 10010
- #define MY_IP "192.168.1.106"
- #define MY_PORT 10010
- #define FILE_NAME "/dev/ttyO0"
- #define MAX_LEN 128
- int main(void)
- {
- int serial_buffer[MAX_LEN] = {0};
- char data2send[4096] = {0};
- int numbers2send = 0;
- int i = 0;
- int serial_fd = open(FILE_NAME, O_RDWR);
- if(serial_fd < 0){
- printf("Serial port open error.\n");
- return -1;
- }
- int socket_fd = socket(AF_INET, SOCK_STREAM, 0);
- if(socket_fd < 0){
- perror("socket_fd");
- return -1;
- }
- printf("Ready to connect the server...\n");
- struct sockaddr_in server_addr = {0};
- server_addr.sin_family = AF_INET;
- server_addr.sin_port = htons(SERVER_PORT);
- server_addr.sin_addr.s_addr = inet_addr(SERVER_IP);
- int ret = connect(socket_fd, (struct sockaddr *)&server_addr, sizeof(server_addr));
- if(ret < 0){
- perror("connect");
- return -1;
- }
- printf("connect success.\n");
- while(1){
- for(i=0; i<MAX_LEN; i++){
- serial_buffer = 0;
- }
- read(serial_fd, serial_buffer, MAX_LEN);
- printf("[%s].\n", serial_buffer);
- send(socket_fd, serial_buffer, MAX_LEN, 0);
- }
- close(serial_fd);
- close(socket_fd);
- return 0;
- }
|
|