CrossCore XA
Revision: 1.0
Programmers Guide
Date: Jul 1, 10
Syntax
int socket(
int domain,
int type,
int protocol
)
Parameters
domain
Specifies communication domain
type
Specifies socket type
protocol
Specifies a particular protocol to be used with the socket
Return value
Returns a non-negative integer, the socket file descriptor, on success. In case of errors, -1 is
returned and errno is set appropriately.
Example
The following example shows how to use SocketCAN for initial scope of CAN_RAW socket
communication. Note that this is an example; all possible errors are not handled.
#include
<sys/types.h>
#include
<sys/socket.h>
#include
<sys/ioctl.h>
#include
<net/if.h>
#include
<linux/can.h>
#include
<linux/can/raw.h>
#include
<string.h>
/* Define constants, if not defined in the headers */
#ifndef PF_CAN
#define PF_CAN 29
#endif
#ifndef AF_CAN
#define AF_CAN PF_CAN
#endif
/* ... */
/* Somewhere in your app */
/* Create the socket */
int
skt = socket( PF_CAN, SOCK_RAW, CAN_RAW );
/* Locate the interface you wish to use */
struct ifreq ifr;
strcpy(ifr.ifr_name,
"can0"
);
ioctl(skt, SIOCGIFINDEX, &ifr);
/* ifr.ifr_ifindex gets filled
* with that device's index */
www.crosscontrol.com
13