0

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++

3
  • Assuming you want to convert iResult to string, you can explore itoa or it's variants. Commented Aug 19, 2010 at 5:26
  • OP: The title of your question and your question completely contradict each other. Please clarify... Commented Aug 19, 2010 at 5:35
  • Also you can explore sprintf for this purpose Commented Aug 19, 2010 at 5:40

6 Answers 6

4
stringstream buf;
buf << 12345;
buf.str(); // string("12345")
buf.str().c_str(); // char* "12345"
Sign up to request clarification or add additional context in comments.

2 Comments

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: That does not make any sense.
2

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

printf("Bytes Sent: %s\n", sendbuf);

15 Comments

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, How is the "C way of doing things" "not the correct answer"???
@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::stringstreams As this question is tagged C++, then the C++ answer in the answer of thomasz.
@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?
@stranger I have found this article informit.com/guides/content.aspx?g=cplusplus&seqNum=72 but anyway I have been told several times to consider using only std::stringstream over printf and itoa and atoi etc... because it is safe, and may not crash, printf can crash if not given the exact number of argument it is waiting for.
|
1

Another opportunity is boost::lexical_cast<>

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

Comments

0

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.

Comments

0

if you use visual C++ 2008 or 2010 i think there is a function inbuilt to do your job.

something like itoa(int); will convert the given int and return a char *

pretty simple

its in stdlib.hlbtw

** make sure this is not the same in all compilers or distrubutions http://www.cplusplus.com/reference/clibrary/cstdlib/itoa/ here is a link for reference

Comments

0

Take a look at itoa which converts an integer to a string.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.