views:

149

answers:

5

hi,

SOCKET lhSocket;
int iResult;
lhSocket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
char *sendbuf = "this is a test";
iResult = send(lhSocket, sendbuf, (int)strlen(sendbuf), 0 );

printf("Bytes Sent: %ld\n", iResult);

I have client and Server program using sockets in C++ now i send a buffer it is received by server now when server acknowledge me back saying i got your packet i should get that in string format not bytes received : something. how to achieve that ?

My iresult returns me an integer value, I am sending a message over socket to server , i dont want to print it as Bytes sent : 14. I want to print the message sent as string to server. I am dealing with Sockets. How i can achieve this in C++

+2  A: 

sendbuf is the string which you are sending. Print sendbuf instead:

printf("Bytes Sent: %s\n", sendbuf);
strager
No, it is not the correct answer, this is only the C way of doing things. The standard C++ way of converting stuffs is in the answer of Tomasz. I quote him: stringstream buf;buf << 12345;buf.str(); // string("12345")buf.str().c_str(); // char* "12345"
Stephane Rolland
@Stephane Rolland, How is the "C way of doing things" "not the correct answer"???
strager
@stranger because the use of printf(...) is considered dangerous, as much as the use of any variadic functions ( taking ... arguments ). C++ offers means to achieve this without danger, using the standard std::stringstreamsAs this question is tagged C++, then the C++ answer in the answer of thomasz.
Stephane Rolland
@Stephane Rolland, "considered dangerous" by whom? I don't see how this answer is *wrong* because someone *could* make a mistake. I'm sure every answer involving plain pointers is *wrong* because it doesn't use smart pointers, then?
strager
Stephane Rolland
@stranger, I don't say your answer is wrong. I say the C++ way of doing things is using std::stringstream, and if you don't want to have a look at this technique it is up to you not to be convinced without knowing, your being blindfolded is really not my concern.
Stephane Rolland
@Stephane Rolland, You said my answer "is not the correct answer". Also, I'm not saying using `std::stringstream` is wrong. Given the existing code, `printf` seemed like a more reasonable solution, as `stdlib` is already being used. IMO, mixing C++-like I/O (with buffers and streams) with C-like I/O (with printf and char arrays) is more confusing and harmful than being consistent with a possibly more error-prone method. Re printf programming errors: GCC at least tells you if you're not matching the parameters with the specifiers in the string, so that's less of a problem.
strager
@Stephane Rolland, I can't seem to access your informit link. Also, my name is strager, not stranger. And another point: there's a difference between UB and "crashing"; mismatching `printf` parameters is UB, not "causing a crash". I'm sure there are pitfalls to using `std::stringstream` as well (with the ugly syntax being one of them!).
strager
@strager, ok you are free. As a matter of personnal tastes I had soon become hallergic to printf. My personnal experience being, that I often had to do debug on released products, so I had to use trace systems that I wrote, often in a hurry... and if I do the division Nb of Bugs using printf / Nb of Bugs using std::stringstream it returns NaN ( Not An Integer ) because I simply have had NO crash with stringstream. Maybe just a coincidence.
Stephane Rolland
@strager and this syntax looks ugly only because you have not used it. It has also looked ugly for me. But after using it ten times, it is natural in deed, and printf then looks ugly with its %d %s %ul etc... I have become fan of operator << . (that may I recall you has never crashed whatever I fed it with... It doesn't compile when there's a trouble)
Stephane Rolland
@strager sorry I hadn't read your arguments about mixing C++-like I/O and C-like I/O... I completely agree with you on this point, but I would be more of a C++ integrist going totally to C++-like I/O ;-)
Stephane Rolland
And I would use std::string instead of char* and strlen and... I'd better shut up now ;-)))
Stephane Rolland
@Stephane Rolland, No matter what you say, and no matter how much I use it, `<<` is still a bit-shift operator, not a data-flow operator. Using it in another sense is ugly. Ugly, ugly.
strager
:-) well... well.. well... std::cout << "obviously we will never agree on that point" << std::endl (but if you could tell me the way you highlight C and C++ keywords in comments I would be very glad)
Stephane Rolland
@Stephane Rolland, Use \`backticks\`.
strager
+3  A: 
stringstream buf;
buf << 12345;
buf.str(); // string("12345")
buf.str().c_str(); // char* "12345"
Tomasz Wysocki
I have client and Server program using sockets in C++ now i send a buffer it is received by server now when server acknowledge me back saying i got your packet i should get that in string format not bytes received : something. how to achieve that ?
Swapnil Gupta
@Swapnil: That does not make any sense.
Billy ONeal
A: 

Take a look at this: itoa

Markos
A: 

You're asking different things in the title and your post.

Converting int to string in C++ is done with

#include <sstream>
std::ostringstream oss;
oss << some_int;
// do whatever with oss.str()...

as Tomasz illustrated.

To receive data from a socket, you need to make a further call to either recv() or read(). Your "send" call does not itself wait for the reply. recv() or read() accept character-array buffers to read the response into, but you will need to loop reading however much the calls return until you have enough of a response to process, as TCP is what's called a "byte stream" protocol, which means you are not guaranteed to get a complete packet, line, message or anything other than a byte per call.

Given the level of understanding your question implies, I strongly suggest you have a look at the GNU libC examples of sockets programming - there are some server and client examples - easily found via Google.

Tony
A: 

Another opportunity is boost::lexical_cast<>

const int myIntValue = 12345;
const std::string myStringValue = boost::lexical_cast(myIntValue);

fbasile