title: CAN bus communication with System.Net.Sockets
body: <p>I want to convert some C/C++ code to C# so that I can use it on Linux with MONO instead of using a DLLImport with a *.so.</p>
  
  <p>It seems that System.Net.Sockets doesn't support CAN-bus communication but I'm not sure if that means we cannot solve it anyway!?</p>
  
  <pre><code>// Create a socket with the CAN-protocol.
  int _canSocket = socket(PF_CAN, SOCK_RAW, CAN_RAW);
  
  // Get current flags for the socket and add non-blocking to it.
  int flags = fcntl(_canSocket, F_GETFL, 0);
  fcntl(_canSocket, F_SETFL, flags | O_NONBLOCK);
  
  // Name the interface structure "can0" so that we can get the interface index for the interface named "can0".
  struct ifreq ifr;
  strcpy(ifr.ifr_name, "can0");
  ioctl(_canSocket, SIOCGIFINDEX, &amp;ifr);
  
  // Create a local address for the socket.
  struct sockaddr_can addr;
  addr.can_family = AF_CAN;
  addr.can_ifindex = ifr.ifr_ifindex;
  
  // Bind the socket to the address and we're ready to receive / send information on the CAN-bus.
  bind(_canSocket, (struct sockaddr *)&amp;addr, sizeof(addr));
  </code></pre>
  
  <p>I can get the interface using this code</p>
  
  <pre><code>NetworkInterface can = NetworkInterface.GetAllNetworkInterfaces().First(i =&gt; i.Name == "can0");
  </code></pre>
  
  <p>But how do I setup a socket, that speaks "CAN" if .NET doesn't support it. I'm thinking since the CAN is in fact speaking RAW in this case, it's just a matter of pushing bytes in the correct order!?</p>
  
  <p>Or have I misunderstood everything?</p>
  
  <p>Could someone please shed some light over this.</p>