3

For example, if the client asks the server for the biggest and the smallest size of a particular object, the server would need to reply to the client's request with both variables. Is it possible to send two strings from the server for the client to output?

Prinks
  • 33
  • 3
  • 1
    You can write any number of byte stream to the socket (client or server). You need to write your own code to interpret the new line characters – Charlie Jul 21 '19 at 07:23
  • 1
    Yes it's perfectly possible. All you are sending is bytes, you can send as many or as few as you wish. Of course you have to write code that interprets those bytes correctly, but how many bytes you send and what they represent is up to you. – john Jul 21 '19 at 07:26
  • do you have specific server library? – apple apple Jul 21 '19 at 08:05
  • @appleapple What the `write()` function does is extremely well known, and it is not part of any 'specific server library'. Your point? – user207421 Jul 21 '19 at 09:47
  • @appleapple The `write()` function is part of Posix, and it can be used to write to sockets on a large number of operating systems. – user207421 Jul 21 '19 at 09:49
  • @user207421 it just you guess anyway, there is not even code or tag relevant. I just ask OP to include which `write` it is. – apple apple Jul 21 '19 at 09:54
  • @appleapple No guesswork on my part. These are facts, and very well-documented. It is you that is guessing: (1) about a 'specific server library', not mentioned in the question, and (2) about him being constrained to what's in the C++ standard, ditto. And even if. Was a custom `write()` function the same answer would apply, due to what TCP does under the hood: the OP can issue as many writes as he likes. – user207421 Jul 21 '19 at 09:58
  • @user207421 where is the evidence? otherwise why you say you're not guessing? And this is why mcve is sometimes needed and the propose of tag system. – apple apple Jul 21 '19 at 10:00
  • @appleapple The evidence that he is using a `write()` function is right there in the title; the evidence that Posix contains a `write()` function is right there in the Posix standard, where it has been for about 30 years; and the evidence that it can be used on sockets ditto apart from the timeline, which is somewhat shorter. It is evidence that he is using a 'specific server library' that is missing, and in any case it is all irrelevant for the reasons I stated. It does not 'depend' in any way 'on what the `write()` function do'. TCP is a streaming protocol. – user207421 Jul 21 '19 at 10:02
  • @user207421 OP doesn't include the `posix` tag. so all you talk is guess. . – apple apple Jul 21 '19 at 10:13
  • @user207421 all I do is ask for what `write` OP means. I don't really get what I do wrong. (OP can easily update the Q saying posix or add the posix tag as response) (if it's the case, of course) – apple apple Jul 21 '19 at 10:50
  • I provided a reasonable answer. Nothing wrong with that. @Prinks, If you want it, then I will send it to you. Additionaly: user207421, a very experienced C++ expert (according to his rep) commented on my answer. Then somebody downvoted the answer. And then I simply deleted that answer. I think that this is better for the community. And after the next comment under this question, I will delete this comment as well. I have 0 blood pressure and pulse in this regard. And, at the end: Prinks is a new contributor. Be nice, and check out our Code of Conduct. – A M Jul 21 '19 at 12:23
  • @ArminMontigny Thank you for kind response and helpfulness, I would love to hear your response to the question posted. – Prinks Jul 21 '19 at 15:28
  • @Prinks: See answer below – A M Jul 21 '19 at 15:38

1 Answers1

-1

Yes, this is possible. You can use write() to write a number of bytes. You just need to collect the data in a contiguous area and then handle a pointer to that area to write, of course with the number of data to write.

You can also call write in a loop to write different areas.

A contiguous area can also have the size 1. Meaning, you can write byte by byte. In a loop. Or as single statements.

To build your data area, you can use different STL containers, like std::string or std::vector. To access the data, you can use member functions (like c_str() or data();

If you want to have complete freedom, you may want to use an std::ostringstream. Here you can insert data like in std::cout and then send the result with write to anywhere.

I prepared an example for you.

Please note. As file descriptor I am using 1. This is equivalent to std::cout. So you will see the result of the program in the console.

#include <io.h>
#include <iostream>
#include <string>
#include <vector>
#include <sstream>


// File descriptor. For test purposes we will write to the console
constexpr int testFdforStdOut = 1;

int main()
{
    // String parts
    std::string part1("Part1\n");
    std::string part2("Part2\n");
    std::string part3("Part3\n");
    // Combine all strings
    std::string allData = part1 + part2 + part3;
    // Write all data
    _write(testFdforStdOut, allData.c_str(), allData.size());

    // Vector of strings
    std::vector<std::string> vectorOfStrings{ "\nPart4\n", "Part5\n", "Part6\n", "Part7\n" };
    // Write data in a loop
    for (const std::string&s : vectorOfStrings)
        _write(testFdforStdOut, s.c_str(), s.size());

    std::ostringstream oss;
    oss << "\nTest for anything\n" << 2 << '\n' << 3 * 3 << "\nPart8\n";
    _write(testFdforStdOut, oss.str().c_str(), oss.str().size());

    return 0;
}
A M
  • 14,694
  • 5
  • 19
  • 44