简体   繁体   中英

Is C POSIX API compatible with C++ STL

I will be using the C POSIX API Library to learn about socket programming. I will be creating servers which will be listening for connections and clients which will connect to the server. On the server there is a text file which looks like this

Peter,Male,10
Mary,Female,20
Tim,Male,30
Shrek,Male,40

The server will read in the text file and store it in a data structure. I am wondering if there will be any compatibility problems with using a C++ STL like vector together with the C POSIX API to store all the text file information?

Can someone advise me if I should use C struct or C++ STL?

You can call C functions from C++ programs. To do this, you have to declare them as "extern C" so that the compiler knows how to call them. But the good news is that your C library include files almost certainly have something like this in them:

#ifdef __cplusplus
extern "C" {
#endif

/* C callable stuff goes here */

#ifdef __cplusplus
}
#endif

so that if you include them from a C++ program, the functions are already declared as C functions.

The arguments you pass to the C functions must be of the type which their declaration specifies - so you will need to use structs, pointers, etc as required. However internally your program can make use of whatever STL or other C++ libraries you want to.

There are networking libraries available for C++ which would make it easier to use sockets from C++. They are essentially wrappers around the POSIX functoins (or equivalent for other platforms). However, if you are trying to learn the POSIX APIs or want to do something not supported by these libraries then you are able to call the POSIX APIs directly.

C++ has its own libraries much more suited to do networking tasks (Boost.Asio?), so this questions can only serve educational purpose.

It is certainly possible to use C API from C++.

Unfortunately doing so requires some knowledge about name mangling, C++ containers and the API being used. As you asked this question you might have difficulties trying to learn both C++ and POSIX API.

You can use a std::vector of struct or class to store the data. Reading and writing from/to a file using C POSIX API shouldn't be a problem at all.

struct Record
{
   enum Gender {MALE, FEMALE};

   std::string name;
   Gender gender;
   int age;
}

std::vector<Record> records;

/* Read the data from the file using C POSIX API and store them in records */

/* Use the data from records and save them in file using C POSIX API */

POSIX is a C-API which often uses pointers to blocks of memory to be transferred or received. This works in C++ as long as you restrict your data to POD -types. As soon as you transfer a struct with something like a std::string you will have undefined behavior. That means that you can't send or receive something like

struct Person
{
    std::string name;
}

over the network without converting it to a POD before sending and then converting it back.

What you basically need is a way to serialize your data structure/class in a defined way, so that you can then easily transmit it over a transmission line.

This is not particular to the case of C vs C++ APIs you are referring to, but is also necessary for correct transmission of data between for instance two machines with different endianness .

There already exists a number of solutions for serializing structures, some easier to use than others. Currently I'm mainly working on a system where we use Thrift , which lets you define datatypes and structures in an easy-to-read IDL file, and can then generate files implementing these for a large number of languages, so that you can for instance very easily transfer a class from a C++ program over a network to a class in a Java program.

Other possibilities include JSON, XML, and probably a large number of others also. Since your case seems relatively simple, you can of course also just write your own - for instance by including serialize() and unserialize() functions, that respectively convert all member variables of your class to an array of char (serialization), and set all member variables of your class from an array of char (unserialization).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM