-2

I am currently attempting to communicate with an external application over TCP/IP based socket. I have successfully established a connection with the client and received some data. This manual here states that

After this command is received, the client must read an acknowledgement octet from the daemon. A positive acknowledgement is an octet of zero bits. A negative acknowledgement is an octet of any other pattern.

I would like to send a positive acknowledgment and I am sending it this way My server listening code was obtained from here

void WriteData(std::string content)
{
    send(newsockfd,content.c_str(),content.length(),0);
}

WriteData("00000000");

My question is if I am sending this data corectly (octet of zero bits) ?

Update:

I have read this post here which states that send only allows to send a char* array. So I am not sure how I can send a byte over a socket. I know i could do something like this

std::bitset<8> b1 ;  // [0,0,0,0,0,0,0,0]

but i am not sure how i would send that over a socket.

Community
  • 1
  • 1
James Franco
  • 4,516
  • 10
  • 38
  • 80

1 Answers1

2

Try

WriteData(std::string("\0",1));

using your function or even:

const char null_data(0);
send(newsockfd,&null_data,1,0);

to send it directly.


WriteData("00000000");

Will actually sends 8 octets of 48 [decimal] (assuming your platform is ASCII which all modern systems are compatible with).

However \0 is the escape sequence used in string literals to represent the null character (that is the zero octet).

There's a conflation (mixing together) in C++ between the notions of characters and bytes that dates back to the earliest days of C and is pretty much baked in.

Persixty
  • 8,165
  • 2
  • 13
  • 35
  • 1
    seems like the second method did the trick while `WriteData("\0")` does not work – MistyD Jan 02 '16 at 09:11
  • @MistyD, Silly me. Of course it didn't. That constructor for `std::string` would see that null as the terminator. Fixed now. – Persixty Jan 02 '16 at 10:06
  • @BasileStarynkevitch Sort of a bit. C++ implementations don't as standard offer very good handling for multi-byte characters. But in the range we're dealing with ASCII and all standard encodings of Unicode overlap. I've amended the answer to say 'ASCII *compatible*'. I would agree nearly all modern systems are Unicode and UTF-8 is generally used as the lowest common denominator encoding. – Persixty Jan 02 '16 at 11:56