Hi all:
Got a formatting question here:
Trying to send data string over to a server via send();
The problem is that I want to enclose the data string with double-quotes, "data string" should be displayed on the server side. The method I used is using two double_quotes on each end of the data string, writing the string with the double_quotes to another string and lastly, send the second string using send(). However, the result is the data string without the double quotes I added? Why?

Codes:

_snprintf(user_message,MESSAGE_LINE_SIZE, "MESSAGE ""%s""\r",s);
send(sock,user_message,(int)strlen(user_message),0)==SOCKET_ERROR)

Thanks

Dani AI

Generated

Short summary and a few robustness tips you won't find in the replies: the disappearance of the literal double-quotes was caused by how the C compiler treats adjacent string literals (they get concatenated at compile time), so the two empty string literals you wrote didn’t produce visible quote characters. pointed out the concatenation behavior and gave the right direction (escape the quote). For production code it’s worth going one step further: construct the message in a way that both preserves the quotes and handles buffer sizes and partial sends safely.

A compact, safer pattern (wrap the data in quotes, check for truncation, and use the formatted-length for send) looks like this:

char out[MESSAGE_LINE_SIZE];
int n = snprintf(out, sizeof out, "%c%s%c\r\n", '"', s, '"');  /* wrap s in quotes */
if (n < 0) { n = 0; }                    /* format error */
else if ((size_t)n >= sizeof out) {      /* truncated: snprintf returns would-be length */
  n = sizeof out - 1;                    /* actual bytes present (excluding terminating NUL) */
}
send(sock, out, n, 0);  /* use n, not strlen(out), and loop until all bytes are sent */

Notes and cautions:

  • Prefer the standard C99 snprintf (or the platform’s secure alternative) and always inspect its return value; that’s the reliable way to detect truncation. If you must use nonstandard _snprintf, check your platform docs and explicitly ensure NUL-termination when truncated.
  • Avoid calling strlen on a buffer that might not be NUL-terminated — that’s undefined behavior. Use the formatted-length (or strnlen) when deciding how many bytes to send.
  • send can write fewer bytes than requested; loop until all bytes are transmitted.
  • If quotes still don’t appear on the server, confirm the server’s parsing/logging (some protocols or servers strip or normalize quotes); examine the raw bytes (hex dump) arriving at the server to be sure.

This keeps the original fix (escape or explicitly insert the quote character) but adds safe length-handling and sending practices for real-world code.

Recommended Answers

All 4 Replies

C is not VB.

_snprintf(user_message,MESSAGE_LINE_SIZE, "MESSAGE \"%s\"\r",s);

>>"MESSAGE ""%s""\r"
The reason this doesn't work the way you want it is because that is a standard way of concantinating string literals and the compiler will just make one string out of it.

char str[] = "Hello " "World"
is the same as this
char str[] = "Hello World"

Thanks man. It worked.

Hi dragon, good to see you again.
Thanks for you info, I always learn something new from you.
Thank you again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.