Reputation: 55
I’m trying to send a stringstream through a TCP socket connection. The problem is that windows sockets only takes a const char data array. What would be the best and most efficient way to send a stringstream data type using sockets?
char *szMessage="Hello World";
send(Socket,szMessage,strlen(szMessage),0);
I’m willing to learn a different sockets library if windows native sockets library is not sufficient for this job. Also I’ve also just started using Boost C++ libraries for threading and their sockets library looks a little more advanced but I’m unsure if it supports sending stringstream’s.
Thanks in advance
Upvotes: 1
Views: 2179
Reputation: 100032
The purpose of a socket API is to send bytes. The byte datatype in C++ is char
.
To send a more complex datatype across a socket, you need a protocol. Windows provides (DCE) RPC. Many people use SOAP. There are lots of others.
A stringsrteam
contains characters. If you control both ends of the connection, the difference between characters and bytes may not be important to you. If you do not, it can be very important, since the two ends can disagree on the interpretation unless you are careful to make sure that both use the same character encoding.
Upvotes: 0
Reputation: 35059
stringstream
provides functions to retrieve a const char*
, see the documetation:
std::stringstream ss( ... );
ss.str().c_str(); // now the const char *
send(Socket,ss.str().c_str(),ss.str().length(),0);
Upvotes: 3