Linux SocketCan client server demo hacking
/***********************************************************************
* Linux SocketCan client server demo hacking
* 说明:
* 本文主要是解读Linux上的SocketCan的基本使用方法,内容和Linux上的
* 网络编程差不多。
*
* 2016-3-28 深圳 南山平山村 曾剑锋
**********************************************************************/ 一、cat client.c
#include <sys/ioctl.h>
#include <net/if.h>
#include <linux/can.h> #ifndef PF_CAN
#define PF_CAN 29
#endif #ifndef AF_CAN
#define AF_CAN PF_CAN
#endif int main()
{
int s;
unsigned long nbytes;
struct sockaddr_can addr;
struct ifreq ifr;
struct can_frame frame; // 创建一个SocketCan
s = socket(PF_CAN,SOCK_RAW,CAN_RAW); // 设置一些参数,这个是使用的can网络的设备号
strcpy((char *)(ifr.ifr_name),"can0");
ioctl(s,SIOCGIFINDEX,&ifr);
printf("can0 can_ifindex = %x\n",ifr.ifr_ifindex); // 设置使用CAN协议
addr.can_family = AF_CAN;
addr.can_ifindex = ifr.ifr_ifindex;
bind(s,(struct sockaddr*)&addr,sizeof(addr)); // 设置发送到的设备id
frame.can_id = 0x123;
strcpy((char *)frame.data,"hello");
frame.can_dlc = strlen(frame.data); printf("Send a CAN frame from interface %s\n",ifr.ifr_name); // 发送数据
nbytes = sendto(s,&frame,sizeof(struct can_frame),,(struct sockaddr*)&addr,sizeof(addr)); return ;
} 二、cat server.c
#include <sys/ioctl.h>
#include <net/if.h>
#include <linux/can.h> #ifndef PF_CAN
#define PF_CAN 29
#endif #ifndef AF_CAN
#define AF_CAN PF_CAN
#endif int main()
{
int s;
unsigned long nbytes,len;
struct sockaddr_can addr;
struct ifreq ifr;
struct can_frame frame; // 创建一个SocketCan
s = socket(PF_CAN,SOCK_RAW,CAN_RAW); // 指定使用的Can网络
strcpy(ifr.ifr_name,"can0");
ioctl(s,SIOCGIFINDEX,&ifr);
printf("can0 can_ifindex = %x\n",ifr.ifr_ifindex); // 指定使用的协议,并绑定
//bind to all enabled can interface
addr.can_family = AF_CAN;
addr.can_ifindex =;
bind(s,(struct sockaddr*)&addr,sizeof(addr)); // 获取数据
nbytes = recvfrom(s,&frame,sizeof(struct can_frame),,(struct sockaddr *)&addr,&len); /*get interface name of the received CAN frame*/
ifr.ifr_ifindex = addr.can_ifindex;
ioctl(s,SIOCGIFNAME,&ifr);
printf("Received a CAN frame from interface %s\n",ifr.ifr_name);
printf("frame message\n"
"--can_id = %x\n"
"--can_dlc = %x\n"
"--data = %s\n",frame.can_id,frame.can_dlc,frame.data); return ;
}
Linux SocketCan client server demo hacking的更多相关文章
- am335x Qt SocketCAN Demo hacking
/*********************************************************************************** * am335x Qt Soc ...
- linux watchdog demo hacking
/********************************************************************** * linux watchdog demo hackin ...
- linux SPI bus demo hacking
/********************************************************************** * linux SPI bus demo hacking ...
- Linux socket program Demo1(client & server)
client and server Demo of socket. client send data to server. server send data to client. // this ...
- C Socket Programming for Linux with a Server and Client Example Code
Typically two processes communicate with each other on a single system through one of the following ...
- 深入浅出 Redis client/server交互流程
综述 最近笔者阅读并研究redis源码,在redis客户端与服务器端交互这个内容点上,需要参考网上一些文章,但是遗憾的是发现大部分文章都断断续续的非系统性的,不能给读者此交互流程的整体把握.所以这里我 ...
- Network client/server
<Beginning Linux Programming_4th> chapter 15 Sockets 1 A simple local client/server 1) clie ...
- Linux 搭建SVN server
一. SVN 简单介绍 Subversion(SVN) 是一个开源的版本号控制系統, 也就是说 Subversion 管理着随时间改变的数据. 这些数据放置在一个中央资料档案库 (repository ...
- Linux下HTTP Server
想在Linux下实现一个简单的web Server并不难.一个最简单的HTTP Server不过是一个高级的文件服务器,不断地接收客户端(浏览器)发送的HTTP请求,解析请求,处理请求,然后像客户端回 ...
随机推荐
- Delphi中的四舍五入函数
一.Delphi中的四舍五入法 四舍五入是一种应用非常广泛的近似计算方法,针对不同的应用需求,其有算术舍入法和银行家舍入法两种. 所谓算术舍入法,就是我们通常意义上的四舍五入法.其规则 ...
- easyui 文本框 显示提示信息data-options="prompt:'格式:水箱支架-京东汽配店铺-图集(大图/图集6)'"
<tr> <td>图集6:</td> <td> <input class="easyui-textbox" data-opti ...
- xcode 6.3 打包crash问题--参考
xcode升级6.3之后,有些项目会出现打包crash的问题,只要选择偏好设置,把source control全部禁用掉就可以了.
- iOS基本网络请求
常见的网络请求有同步GET, 同步POST, 异步GET, 异步POST. GET请求和POST请求的区别: 1. GET请求的接口会包含参数部分,参数会作为网址的一部分,服务器地址与参数之间通过 ? ...
- SDIBT 3237 Boring Counting( 划分树+二分枚举 )
http://acm.sdibt.edu.cn/JudgeOnline/problem.php?id=3237 Problem H:Boring Counting Time Limit: 3 Sec ...
- 强连通分量Tarjan模板
#include<iostream> #include<stdio.h> #include<string.h> #include<stack> #inc ...
- 在PyCharm里配置SubVersion
1.如果PyCharm不支持svn ,那么下载svn命令行安装包,下载地址:http://sourceforge.net/projects/win32svn/ 例如:安装到 D:\software\s ...
- sentos 上安装vnc图形界面
一.安装gnome图形化桌面 CentOS 6.3 64位 #yum groupinstall -y "X Window System" #yum groupinstall - ...
- springMVC数据封装成POJO
springMVC把前台的数据封装为POJO与struts2的封装形式不同.struts2需要在控制器声明需封装的POJO,而springMVC不需要任何准备工作,只需在相应的方法的参数中加上需封装的 ...
- Spring+MyBatis实践—MyBatis数据库访问
关于spring整合mybatis的工程配置,已经在Spring+MyBatis实践—工程配置中全部详细列出.在此,记录一下几种通过MyBatis访问数据库的方式. 通过sqlSessionTempl ...