Linux-UDP-Socket编程
接收CAN总线上的数据并将其发送出去
创建客户端:
/********************************************************************
* copyright (C) 2018 all rights reserved
* @file: server.c
* @Created: 2018-4-16 13:22nd
* @Author: Yinrui Zhu
* @Description: test user eth0
* @Modify Date: 2018-4-13
*********************************************************************/ #include <limits.h>
#include <stdint.h>
#include <getopt.h>
#include <libgen.h>
#include <signal.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <linux/can.h>
#include <linux/can/raw.h> #include <sys/types.h>
#include <sys/socket.h>
#include <pthread.h>
#include <arpa/inet.h>
#include <errno.h>
#include <netinet/in.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h> #define SERVER_PORT 8000
#define BUFFER_SIZE 1024
#define ipaddr "192.168.200.110"
#define FILE_NAME_MAX_SIZE 512 int main()
{
int nbytes,i,addrlen;
char abuf[];
char bbuf[];
char cbuf[];
char dbuf[];
char ebuf[]; int s;
struct sockaddr_can addr;
struct ifreq ifr;
struct can_frame frame;
struct can_filter rfilter[];
int family = PF_CAN, type = SOCK_RAW, proto = CAN_RAW; system("ip link set can0 type can bitrate 125000 triple-sampling on");
sleep();
system("ip link set can0 up"); s = socket(family, type, proto); //创建套接字 strcpy(ifr.ifr_name, "can0");
ioctl(s, SIOCGIFINDEX,&ifr); //指定can0设备 addr.can_family = family;
//addr.can_family = AF_CAN;
addr.can_ifindex = ifr.ifr_ifindex;
bind( s, (struct sockaddr *)&addr, sizeof(addr) ); //将套接字与can0设备绑定 /*定义接收规则*/
rfilter[].can_id = 0x10; //接收ID为0x10的报文
rfilter[].can_mask = CAN_SFF_MASK; rfilter[].can_id = 0x20; //接收ID为0x20的报文
rfilter[].can_mask = CAN_SFF_MASK; rfilter[].can_id = 0x40; //接收ID为0x40的报文
rfilter[].can_mask = CAN_SFF_MASK; rfilter[].can_id = 0x80; //接收ID为0x80的报文
rfilter[].can_mask = CAN_SFF_MASK; rfilter[].can_id = 0x800; //接收ID为0x800的报文
rfilter[].can_mask = CAN_EFF_MASK; if( setsockopt( s,SOL_CAN_RAW, CAN_RAW_FILTER, &rfilter,
sizeof(rfilter) ) != ) //设置过滤规则
{
perror("setsockopt");
exit();
} /*服务端地址*/
struct sockaddr_in server_addr;
bzero( &server_addr, sizeof(server_addr) );
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = inet_addr(ipaddr);
server_addr.sin_port = htons(SERVER_PORT);
addrlen = sizeof(server_addr); /*创建socket*/
int client_socket_fd = socket(AF_INET, SOCK_DGRAM, );
if( client_socket_fd == - )
{
perror("Creat Socket Failed!");
exit();
} /*数据传输*/
while()
{
/*can接收数据*/
if( nbytes = read(s, &frame, sizeof(frame)) < )
{
perror("read");
return ;
} switch(frame.can_id)
{
case 0x10:
abuf[] = ((frame.can_id>>)&0xFF);
abuf[] = ((frame.can_id>>)&0xFF);
abuf[] = ((frame.can_id>>)&0xFF);
abuf[] = (frame.can_id&0xFF);
for( i=; i<frame.can_dlc+; i++)
{
abuf[i] = frame.data[i-];
printf("ID=0x%x DLC=%d abuf[%d]=0x%x\n", frame.can_id, frame.can_dlc, i, abuf[i]);
}
break; case 0x20:
bbuf[] = ((frame.can_id>>)&0xFF);
bbuf[] = ((frame.can_id>>)&0xFF);
bbuf[] = ((frame.can_id>>)&0xFF);
bbuf[] = (frame.can_id&0xFF);
for( i=; i<frame.can_dlc+; i++)
{
bbuf[i] = frame.data[i-];
printf("ID=0x%x DLC=%d bbuf[%d]=0x%x\n", frame.can_id, frame.can_dlc, i, bbuf[i]);
}
break; case 0x40:
cbuf[] = ((frame.can_id>>)&0xFF);
cbuf[] = ((frame.can_id>>)&0xFF);
cbuf[] = ((frame.can_id>>)&0xFF);
cbuf[] = (frame.can_id&0xFF);
for( i=; i<frame.can_dlc+; i++)
{
cbuf[i] = frame.data[i-];
printf("ID=0x%x DLC=%d cbuf[%d]=0x%x\n", frame.can_id, frame.can_dlc, i, cbuf[i]);
}
break; case 0x80:
dbuf[] = ((frame.can_id>>)&0xFF);
dbuf[] = ((frame.can_id>>)&0xFF);
dbuf[] = ((frame.can_id>>)&0xFF);
dbuf[] = (frame.can_id&0xFF);
for( i=; i<frame.can_dlc+; i++)
{
dbuf[i] = frame.data[i-];
printf("ID=0x%x DLC=%d dbuf[%d]=0x%x\n", frame.can_id, frame.can_dlc, i, dbuf[i]);
}
break; case 0x80000800:
ebuf[] = ((frame.can_id>>)&0xFF);
ebuf[] = ((frame.can_id>>)&0xFF);
ebuf[] = ((frame.can_id>>)&0xFF);
ebuf[] = (frame.can_id&0xFF);
for( i=; i<frame.can_dlc+; i++)
{
ebuf[i] = frame.data[i-];
printf("ID=0x%x DLC=%d ebuf[%d]=0x%x\n", frame.can_id, frame.can_dlc, i, ebuf[i]);
}
break; default :
printf("The recive can indetifer ID is wrong!\n");
break;
} sendto(client_socket_fd,abuf,,,(struct sockaddr*)&server_addr, addrlen);
sendto(client_socket_fd,bbuf,,,(struct sockaddr*)&server_addr, addrlen);
sendto(client_socket_fd,cbuf,,,(struct sockaddr*)&server_addr, addrlen);
sendto(client_socket_fd,dbuf,,,(struct sockaddr*)&server_addr, addrlen);
sendto(client_socket_fd,ebuf,,,(struct sockaddr*)&server_addr, addrlen);
printf("Hello.\n"); /*接收数据*/
/*
size_t recv_len = 0;
char buffer[BUFFER_SIZE];
bzero(buffer, BUFFER_SIZE);
recv_len = recvfrom(server_socket_fd, buffer, BUFFER_SIZE, 0 ,(struct sockaddr*)&client_addr, &client_addr_length);
if( recv_len == -1 )
{
perror("Receive Data Failed:");
exit(1);
}
*/
/*从buffer中拷贝出file_name*/
/*
char file_name[FILE_NAME_MAX_SIZE +1];
bzero(file_name,FILE_NAME_MAX_SIZE+1);
strncpy(file_name, buffer, strlen(buffer)>FILE_NAME_MAX_SIZE ? FILE_NAME_MAX_SIZE:strlen(buffer));
printf("%s\n",file_name);
printf("%s\n",buffer);
*/ }
system("ip link set can0 down");
close(client_socket_fd);
return ;
}
编译生成client_can 使用说明:
、运行可执行文件client_can(./client_can)
、利用网络调试工具Socket_Tool.exe(F:\嵌入式资料\AM335X\BeagleBone\UDP_test\Socket_Tool.exe)
创建服务其端口:
a、192.168.200.110()
、另一快开发板can0设置:
a、ip link set can0 down
b ip link set can0 type can bitrate triple-sampling on
c ip link set can0 up
d cansend can0 -i 0x80 0xaa 0x11 0x55 0x44 0x11 0x22 0x33 0x66
、当can0的CANL、CANH上面有数据时:运行。/client_can的开发板就可以接收的相应的数据(
ID=0x80 DLC= dbuf[]=0x55
ID=0x80 DLC= dbuf[]=0x55
ID=0x80 DLC= dbuf[]=0x55
ID=0x80 DLC= dbuf[]=0x55
ID=0x80 DLC= dbuf[]=0xaa
ID=0x80 DLC= dbuf[]=0xaa
ID=0x80 DLC= dbuf[]=0xaa
ID=0x80 DLC= dbuf[]=0xac
Hello. ),同时将数据通过网络发送给PC机的server服务器端
(
:: 收到数据:{ AA AA AA AC }
:: 收到数据:{ 0D FE B6 }ii
:: 收到数据:{ FD F7 BE }$
:: 收到数据:{ AA AA AA AC }
:: 收到数据:{B8 7B FC B6 4D 8C }竰
)。
PC端的服务器端口配置:
服务器端口:
/********************************************************************
* copyright (C) 2018 all rights reserved
* @file: server.c
* @Created: 2018-4-16 13:22nd
* @Author: Yinrui Zhu
* @Description: test user eth0
* @Modify Date: 2018-4-13
*********************************************************************/ #include <limits.h>
#include <stdint.h>
#include <getopt.h>
#include <libgen.h>
#include <signal.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <linux/can.h>
#include <linux/can/raw.h> #include <sys/types.h>
#include <sys/socket.h>
#include <pthread.h>
#include <arpa/inet.h>
#include <errno.h>
#include <netinet/in.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h> #define SERVER_PORT 8000
#define BUFFER_SIZE 1024
#define ipaddr "192.168.200.110"
#define FILE_NAME_MAX_SIZE 512 int main()
{
int nbytes,i,addrlen;
char abuf[];
char bbuf[];
char cbuf[];
char dbuf[];
char ebuf[]; int s;
struct sockaddr_can addr;
struct ifreq ifr;
struct can_frame frame;
struct can_filter rfilter[];
int family = PF_CAN, type = SOCK_RAW, proto = CAN_RAW; system("ip link set can0 type can bitrate 125000 triple-sampling on");
sleep();
system("ip link set can0 up"); s = socket(family, type, proto); //创建套接字 strcpy(ifr.ifr_name, "can0");
ioctl(s, SIOCGIFINDEX,&ifr); //指定can0设备 addr.can_family = family;
//addr.can_family = AF_CAN;
addr.can_ifindex = ifr.ifr_ifindex;
bind( s, (struct sockaddr *)&addr, sizeof(addr) ); //将套接字与can0设备绑定 /*定义接收规则*/
rfilter[].can_id = 0x10; //接收ID为0x10的报文
rfilter[].can_mask = CAN_SFF_MASK; rfilter[].can_id = 0x20; //接收ID为0x20的报文
rfilter[].can_mask = CAN_SFF_MASK; rfilter[].can_id = 0x40; //接收ID为0x40的报文
rfilter[].can_mask = CAN_SFF_MASK; rfilter[].can_id = 0x80; //接收ID为0x80的报文
rfilter[].can_mask = CAN_SFF_MASK; rfilter[].can_id = 0x800; //接收ID为0x800的报文
rfilter[].can_mask = CAN_EFF_MASK; if( setsockopt( s,SOL_CAN_RAW, CAN_RAW_FILTER, &rfilter,
sizeof(rfilter) ) != ) //设置过滤规则
{
perror("setsockopt");
exit();
} /*创建UDP套接口*/
struct sockaddr_in server_addr;
bzero( &server_addr, sizeof(server_addr) );
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
server_addr.sin_port = htons(SERVER_PORT); /*创建socket*/
int server_socket_fd = socket(AF_INET, SOCK_DGRAM, );
if( server_socket_fd == - )
{
perror("Creat Socket Failed!");
exit();
} /*绑定套接口*/
if(- == ( bind( server_socket_fd, (struct sockaddr*)&server_addr, sizeof(server_addr) ) ) )
{
perror("Server Bind Failed: ");
exit();
} /*数据传输*/
while()
{
/*can接收数据*/
if( nbytes = read(s, &frame, sizeof(frame)) < )
{
perror("read");
return ;
} switch(frame.can_id)
{
case 0x10:
abuf[] = ((frame.can_id>>)&0xFF);
abuf[] = ((frame.can_id>>)&0xFF);
abuf[] = ((frame.can_id>>)&0xFF);
abuf[] = (frame.can_id&0xFF);
for( i=; i<frame.can_dlc+; i++)
{
abuf[i] = frame.data[i-];
printf("ID=0x%x DLC=%d abuf[%d]=0x%x\n", frame.can_id, frame.can_dlc, i, abuf[i]);
}
break; case 0x20:
bbuf[] = ((frame.can_id>>)&0xFF);
bbuf[] = ((frame.can_id>>)&0xFF);
bbuf[] = ((frame.can_id>>)&0xFF);
bbuf[] = (frame.can_id&0xFF);
for( i=; i<frame.can_dlc+; i++)
{
bbuf[i] = frame.data[i-];
printf("ID=0x%x DLC=%d bbuf[%d]=0x%x\n", frame.can_id, frame.can_dlc, i, bbuf[i]);
}
break; case 0x40:
cbuf[] = ((frame.can_id>>)&0xFF);
cbuf[] = ((frame.can_id>>)&0xFF);
cbuf[] = ((frame.can_id>>)&0xFF);
cbuf[] = (frame.can_id&0xFF);
for( i=; i<frame.can_dlc+; i++)
{
cbuf[i] = frame.data[i-];
printf("ID=0x%x DLC=%d cbuf[%d]=0x%x\n", frame.can_id, frame.can_dlc, i, cbuf[i]);
}
break; case 0x80:
dbuf[] = ((frame.can_id>>)&0xFF);
dbuf[] = ((frame.can_id>>)&0xFF);
dbuf[] = ((frame.can_id>>)&0xFF);
dbuf[] = (frame.can_id&0xFF);
for( i=; i<frame.can_dlc+; i++)
{
dbuf[i] = frame.data[i-];
printf("ID=0x%x DLC=%d dbuf[%d]=0x%x\n", frame.can_id, frame.can_dlc, i, dbuf[i]);
}
break; case 0x80000800:
ebuf[] = ((frame.can_id>>)&0xFF);
ebuf[] = ((frame.can_id>>)&0xFF);
ebuf[] = ((frame.can_id>>)&0xFF);
ebuf[] = (frame.can_id&0xFF);
for( i=; i<frame.can_dlc+; i++)
{
ebuf[i] = frame.data[i-];
printf("ID=0x%x DLC=%d ebuf[%d]=0x%x\n", frame.can_id, frame.can_dlc, i, ebuf[i]);
}
break; default :
printf("The recive can indetifer ID is wrong!\n");
break;
} /*定义一个地址,用于蒱获客户端地址*/
struct sockaddr_in client_addr;
socklen_t client_addr_length = sizeof(client_addr); /*接收数据*/
size_t recv_len = ;
char buffer[BUFFER_SIZE];
bzero(buffer, BUFFER_SIZE);
recv_len = recvfrom(server_socket_fd, buffer, BUFFER_SIZE, ,(struct sockaddr*)&client_addr, &client_addr_length);
if( recv_len == - )
{
perror("Receive Data Failed:");
exit();
} /*从buffer中拷贝出file_name*/
char file_name[FILE_NAME_MAX_SIZE +];
bzero(file_name,FILE_NAME_MAX_SIZE+);
strncpy(file_name, buffer, strlen(buffer)>FILE_NAME_MAX_SIZE ? FILE_NAME_MAX_SIZE:strlen(buffer));
printf("%s\n",file_name);
printf("%s\n",buffer); sendto(server_socket_fd,buffer,recv_len,,(struct sockaddr*)&client_addr, client_addr_length); sendto(server_socket_fd,abuf,,,(struct sockaddr*)&client_addr, client_addr_length);
sendto(server_socket_fd,bbuf,,,(struct sockaddr*)&client_addr, client_addr_length);
sendto(server_socket_fd,cbuf,,,(struct sockaddr*)&client_addr, client_addr_length);
sendto(server_socket_fd,dbuf,,,(struct sockaddr*)&client_addr, client_addr_length);
sendto(server_socket_fd,ebuf,,,(struct sockaddr*)&client_addr, client_addr_length);
printf("Hello.\n"); }
system("ip link set can0 down");
close(server_socket_fd);
return ;
}
PC端客户端配置:
编译生成server.can 使用说明:
、运行可执行文件server_can(./server_can)
、利用网络调试工具Socket_Tool.exe(F:\嵌入式资料\AM335X\BeagleBone\UDP_test\Socket_Tool.exe)
创建客户端口:
a、192.168.200.70()
、另一快开发板can0设置:
a、ip link set can0 down
b ip link set can0 type can bitrate triple-sampling on
c ip link set can0 up
d cansend can0 -i 0x80 0xaa 0x11 0x55 0x44 0x11 0x22 0x33 0x66
、当can0的CANL、CANH上面有数据时:运行。/server_can的开发板就可以接收的相应的数据(
ID=0x80 DLC= dbuf[]=0x55
ID=0x80 DLC= dbuf[]=0x55
ID=0x80 DLC= dbuf[]=0x55
ID=0x80 DLC= dbuf[]=0x55
ID=0x80 DLC= dbuf[]=0xaa
ID=0x80 DLC= dbuf[]=0xaa
ID=0x80 DLC= dbuf[]=0xaa
ID=0x80 DLC= dbuf[]=0xac
Hello. ),但是接收到的数据只能发送一次。因为该板子作为服务器端口,需要客户端给该板子发送一个信号(any)。最为客户端的PC机才能接收服务器端(开发板)发过来的数据
(
:: 收到数据:{ DD D4 BE }$
:: 收到数据:{ C5 8F }(
:: 收到数据:{B8 3B F1 B6 8F }?穸
:: 发送数据:猍1次]
:: 收到数据:{AA }
:: 收到数据:{ AA AA AA AC }
:: 收到数据:{ 0D F3 B6 }ii
:: 收到数据:{ DD D4 BE }$
:: 收到数据:{ C5 8F }(
:: 收到数据:{B8 3B F1 B6 8F }?穸
)。
Linux-UDP-Socket编程的更多相关文章
- LInux下socket编程学习笔记
1.socket套接字: socket起源于Unix,而Unix/Linux基本哲学之一就是“一切皆文件”,都可以用“打开open –> 读写write/read –> 关闭close”模 ...
- Linux的SOCKET编程详解(转)
Linux的SOCKET编程详解 1. 网络中进程之间如何通信 进 程通信的概念最初来源于单机系统.由于每个进程都在自己的地址范围内运行,为保证两个相互通信的进 程之间既互不干扰又协调一致工作,操作系 ...
- 【ARM-Linux开发】Linux的SOCKET编程详解
Linux的SOCKET编程详解 1. 网络中进程之间如何通信 进 程通信的概念最初来源于单机系统.由于每个进程都在自己的地址范围内运行,为保证两个相互通信的进 程之间既互不干扰又协调一致工作,操作系 ...
- Linux下socket编程基本知识
本文档主要讲解了Linux下socket编程的一些基本知识,主要包括套接字和字节序的概念,以及一些常用的结构体和函数. 本文是在网易云课堂学习过程中的记录,这个老师讲得很不错,推荐大家围观. Linu ...
- Linux下Socket编程的端口问题( Bind error: Address already in use )
Linux下Socket编程的端口问题( Bind error: Address already in use ) 在进行linux网络编程时,每次修改了源代码并再次编译运行时,常遇到下面的地使用错误 ...
- linux下socket编程实例
linux下socket编程实例一.基本socket函数Linux系统是通过提供套接字(socket)来进行网络编程的.网络的socket数据传输是一种特殊的I/O,socket也是一种文件描述符.s ...
- 3、linux下Socket编程-TCP/UDP
1.什么是Socket 网络的 Socket数据传输是一种特殊的I/O,Socket也是一种文件描述符.Socket也具有一个类似于打开文件的函数调用Socket(),该函数返 回一个整型的Socke ...
- linux下socket编程
相关结构 //下边这两个结构定义在<sys/types.h>里 //一般的地址结构,只能用于覆盖(把其他地址转换为此类型),且只能引用该地址的sa_family字段 struct sock ...
- Linux的SOCKET编程详解
1. 网络中进程之间如何通信 进 程通信的概念最初来源于单机系统.由于每个进程都在自己的地址范围内运行,为保证两个相互通信的进 程之间既互不干扰又协调一致工作,操作系统为进程通信提供了相应设施,如 U ...
- [转]Linux的SOCKET编程详解
From : http://blog.csdn.net/hguisu/article/details/7445768 1. 网络中进程之间如何通信 进 程通信的概念最初来源于单机系统.由于每个进程都在 ...
随机推荐
- 基本控件文档-UITextField属性
CHENYILONG Blog 基本控件文档-UITextField属性 Fullscreen UITextField属性技术博客http://www.cnblogs.com/ChenYilong ...
- jquery对不同id的按钮执行同一类型的操作
不同id执行相同操作: $("#id1,#id2,#id3,#id4") 获取相同class的text值: $(".className").each(funct ...
- layui-laypage模块代码详解
/** layui-v2.4.0 MIT License By https://www.layui.com */;layui.define(function(e) { "use strict ...
- 使用python开发一个能够计算带括号的复杂表达式的计算器(只支持加减乘除)
使用到了模块re,正则,字典等 # 实现简单的加减乘除括号等运算 # Calculator def calculator(expression): print(expression) import r ...
- bzoj 3236: 洛谷 P4396: [AHOI2013]作业 (莫队, 分块)
题目传送门:洛谷P4396. 题意简述: 给定一个长度为\(n\)的数列.有\(m\)次询问,每次询问区间\([l,r]\)中数值在\([a,b]\)之间的数的个数,和数值在\([a,b]\)之间的不 ...
- nvm npm node.js的关系
nvm npm node.js都是用来构建reactNativ的项目 nvm管理node.j和npm版本的 node.js管理reactNative开发中所需要的代码库的 npm管理对应node ...
- 【小程序开发】上拉加载更多demo
wxml: <scroll-view class='swiper-scroll' scroll-y="{{true}}" bindscrolltolower="lo ...
- Android studio 安装过程中遇到的问题
之前用eclipse,想换下studio试试,安装时遇到问题,参考:http://www.cnblogs.com/csulennon/p/4178404.html
- 工具类DateHandler
package com.ctid.rachel.core.util; import java.math.BigDecimal;import java.util.Calendar;import java ...
- Linux下的输入/输出重定向
Linux环境中支持输入输出重定向,用符号<和>来表示.0.1和2分别表示标准输入.标准输出和标准错误信息输出,可以用来指定需要重定向的标准输入或输出,比如 2>lee.dat 表示 ...