element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Members
    Members
    • Achievement Levels
    • Benefits of Membership
    • Feedback and Support
    • Members Area
    • Personal Blogs
    • What's New on element14
  • Learn
    Learn
    • eBooks
    • Learning Center
    • Learning Groups
    • STEM Academy
    • Webinars, Training and Events
  • Technologies
    Technologies
    • 3D Printing
    • Experts & Guidance
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Arduino Projects
    • Design Challenges
    • element14 presents
    • Project14
    • Project Groups
    • Raspberry Pi Projects
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Or choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
Test & Tools
  • Technologies
  • More
Test & Tools
Blog SCPI on a Linux Board - Part 3: TCP/IP Socket C++ programming
  • Blog
  • Forum
  • Documents
  • Polls
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Test & Tools requires membership for participation - click to join
Blog Post Actions
  • Subscribe by email
  • More
  • Cancel
  • Share
  • Subscribe by email
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: Jan Cumps
  • Date Created: 24 Jun 2018 1:31 PM Date Created
  • Views 4145 views
  • Likes 5 likes
  • Comments 4 comments
  • boost
  • tcpip
  • Ubuntu
  • sockets
  • raspberry_pi
  • c++
  • linux
Related
Recommended

SCPI on a Linux Board - Part 3: TCP/IP Socket C++ programming

Jan Cumps
Jan Cumps
24 Jun 2018

I'm building a SCPI electronics lab instrument for Linux.

This post is an object oriented one again: testing if I can create a stream-like socket server in C++.

TL;DR yes

image

If you are interested in object oriented design, you may want to stay and read.

 

This blog post is a preparation for the SCPI programmable instrument I'm building for the Raspberry Pi and the PiFace Digital hat.

The SCPI parser will be in plain C: SCPI on a Linux Board - Part 1: Proof of Concept.

The instrument itself - a digital I/O device that's controlled by that SPI engine, will be written in C++. It's basically to feed my own interest image.

 

The instrument will run as a service on the Pi, listening on a port for commands from the SCPI parser (that one will run a s a service listening to some port too).

In C++, input and output is typically done to and from streams. I'm going to test an example that provides a stream wrapper over TCP/IP sockets.

 

Sockets as Streams

 

The common way to create a socket listener in C is to use the network APIs.

 

#include <sys/socket.h>
#include <netinet/in.h>

// ...

  //Create socket
  socket_desc = socket(AF_INET, SOCK_STREAM, 0);
  if (socket_desc == -1) {
    printf("Could not create socket");
  }
  puts("Socket created");


  //Prepare the sockaddr_in structure
  server.sin_family = AF_INET;
  server.sin_addr.s_addr = INADDR_ANY;
  server.sin_port = htons(portno);


  //Bind
  if (bind(socket_desc, (struct sockaddr *) &server, sizeof(server)) < 0) {
    //print the error message
    perror("bind failed. Error");
    return 1;
  }
  puts("bind done");
  while (1) {


    //Listen
    listen(socket_desc, 3);

// ...

 

This works perfectly. The code above is part of the SCPI parser for my project.

 

But how to switch to a C++ io stream? The libraries have stream implementations for files and other OS resources, but not for TCP/IP traffic.

The boost.org C++ libraries do have a TCP/IP IO stream. I'm investigating their example here.

This is how a C++ TCP/IP socket server looks like:

 

  try
  {
    boost::asio::io_service io_service;

    tcp::endpoint endpoint(tcp::v4(), 2223);
    tcp::acceptor acceptor(io_service, endpoint);

    for (;;)
    {
      tcp::iostream stream;
      acceptor.accept(*stream.rdbuf());
      stream << make_daytime_string();
    }
  }
  catch (std::exception& e)
  {
    std::cerr << e.what() << std::endl;
  }

(I changed the port from boost's example from 13 to 2223, because 13 is in a range that needs elevated rights (sudo) access)

 

The program above is just an example. It accepts a request. Upon connection, it sends the current time.

Because the loop creates a new stream each time, the original connection closes and you get a new one.

 

image

 

That's behaviour I may want to change or not. I don't know yet if I want to keep the communication between my SCPI parser and the instrument service open or not.

That's a decision I'll make once I integrate the two parts.

I'll create a side blog on how to get and build the BOOST libraries, and how to include them in your project.

 

related blog
SCPI on a Linux Board - Letter of Intent

SCPI on a Linux Board - Part 1: Proof of Concept

SCPI on a Linux Board - Part 2a: PiFace Digital C programming

SCPI on a Linux Board - Part 2b: PiFace Digital C++ programming
SCPI on a Linux Board - Part 3: TCP/IP Socket C++ programming
SCPI on a Linux Board - Part 4: TCP/IP SCPI and Instrument Service
SCPI on a Linux Board - Part 4b: TCP/IP SCPI and Instrument Service 100% Working
SCPI on a Linux Board - Part 4c: TCP/IP SCPI and Instrument Service - Run as a Daemon
SCPI on a Linux Board - Part 5a: LabVIEW Driver for LAB Switch: Open, Close and Switch functions
Attachments:
linux_socket_cpp.zip
  • Sign in to reply

Top Comments

  • DAB
    DAB over 5 years ago +1
    Nice update. DAB
  • Jan Cumps
    Jan Cumps over 5 years ago +1
    I advanced on this design. The SCPI Parser and the Instrument can now both run and interact. Both act as a TCP/IP server. When you send a SCPI command to the SCPI parser over the network, it interprets…
  • Jan Cumps
    Jan Cumps over 5 years ago in reply to Jan Cumps +1
    action photo: Both services running in debug mode and exchanging info. The write part works, setting the outputs of the PiFace Digital hat. I now have to write the read inputs part and the daemon wrappers…
Parents
  • Jan Cumps
    Jan Cumps over 5 years ago

    I advanced on this design. The SCPI Parser and the Instrument can now both run and interact. Both act as a TCP/IP server.

    When you send a SCPI command to the SCPI parser over the network, it interprets the command, and sends it over another port to the Instrument service. Then it sends any response back as a SCPI reply over the network.

     

    It doesn't sound spectacular, but there are a few things that I wanted to try out and that work:

     

    The SCPI parser is a C linux program that runs both as a TCP/IP socket server and client.

    It's a server for SCPI commands, and a client that connects to the Instrument server to let that one physically execute the desired commands.

     

    The Instrument is a C++ linux program that runs as a TCP/IP socket server and listens for actions. It acts on them and replies synchronously via the same socket.

     

    This setup allows me to mix C and C++, split the SCPI part from the instrument API, allows for security needs on the SCPI arser only and allows the 2 programs to be executed as daemons - in a different security space than any other user.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Jan Cumps
    Jan Cumps over 5 years ago in reply to Jan Cumps

    action photo:

    image

    Both services running in debug mode and exchanging info.

    The write part works, setting the outputs of the PiFace Digital hat.

    I now have to write the read inputs part and the daemon wrappers + instructions.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
Comment
  • Jan Cumps
    Jan Cumps over 5 years ago in reply to Jan Cumps

    action photo:

    image

    Both services running in debug mode and exchanging info.

    The write part works, setting the outputs of the PiFace Digital hat.

    I now have to write the read inputs part and the daemon wrappers + instructions.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
Children
No Data
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2023 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube