ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
This is not the only string of this type I have to send though. Chances are I'll have to send a lot of strings of similar form. Question is: which method do I use? The first won't allocate a temporary storage location but expands over 3 lines [loss of clarity?]. The 2nd is clearer but allocates a temporary storage location for the string AND asprintf() is not POSIX compliant [GNU extension, will work on Linux/BSD .. and I can live with that ]
Yeah, it's a bit messy, but it would reduce the number of lines of code each time you do something like this. Altogether, your first way might be the best as long as you don't have to convert numbers to strings or anything that you'd need printf for. It isn't really any harder to read, and it saves memory.
You probably don't need to send the third line \r\n since it doesn't really provide anything extra. In which case your first approach would probably be better.
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195
Rep:
If I am not mistaken, the function sockwrite hangs when the connection is interrupted or times out. Should bytessent not checked for being zero to indicate that there is nothing sent at all? Or did you omit this for simplicity? (and be nice to us, readers)
Using fprintf on a socket is fine. In fact, *nix sockets are designed to be used that way. You should not be using threads anyway, so what is the big deal?
If I am not mistaken, the function sockwrite hangs when the connection is interrupted or times out. Should bytessent not checked for being zero to indicate that there is nothing sent at all? Or did you omit this for simplicity? (and be nice to us, readers)
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195
Rep:
Introuble,
Ok, at first I failed to see that Write was a different function from write.
Now, I know that someone else's code is always worse that someone's own, but still a comment which is meant as good advice.
First, I doubt whether leaving the program because of a write failure is the best thing to do. But since you know your program requirements best, it might be according to your needs.
However, if for any reason you ever would want to take any action as a result of a write failure you would not be able to do so as your program already exited. No clean-up, no error messages, no action in case of a simple time-out.
Also, your write function only checks for a return code of -1. There might be more return codes, including zero, which are not detected in this way, and leave your program in a loop anyway.
I know this sounds pedantic, but from your fisrt post I got the idea you are trying to code as cleanly as possible, hence my two cents...
write() [ Write() ] will only be used on a socket. And yes, if the call fails, the program must exit.
Code:
No clean-up, no error messages, no action in case of a simple time-out.
#1. If I *really* want to, I can use atexit(). I see no reason to design my program so that if an error which must cause an exit occurs, the function this error occured in would return to it's caller, which would in turn return to it's caller etc. untill main would be reached and the return/exit call would be made there. However, except for "good practise" [which, I have to say, this time, I don't quite comprehend as I don't use memory-leak detecting software ..], I don't see a point in freeing buffers before an exit and not leaving it to the operating system. The data contained in those buffers isn't "sensitive" anyway.
#2. What do you mean no error messages?
Code:
perror("write");
And about the reference to that other thread .. look who's the thread starter
I'm always opened to suggestions. I've decided to look at code from other programs that use the same protocol as my app to see how things are done. My opinion is I have familiarity with C but what I deeply miss is the "practical" part. No book taught how things are done "in real life", this is the reason why this thread exists. [and the one you made reference to]
[p.s.: expect a thread asking about a "how-to" study the source of a large program ]
1. Yes, I'd definitely encourage you to de-allocate all buffers before a *normal* exit, and *not* rely on the OS. It's just good discipline, if nothing else.
As far as an *abnormal* termination: that's different. No, I don't see any compelling reason to be anal as long as you're aborting the whole program.
2. As far as your original question, about concatenating a string:
a) I agree, global buffers probably aren't a great choice.
b) I also agree that "fprintf()" probably isn't a great choice, either (although tuxdev is right - there is absolutely *no* problem using fprintf with sockets under Linux).
c) So what's wrong with using three lines? That's fine; it's not at all unclear.
d) And what's wrong with doing an "sprintf()" (or the equivalent, for example "snprintf()") into a *stack* variable?
I honestly believe options c) and d) are equally clean, and perform equally well.
3. Finally (and what prompted me to write this) - a book recommendation:
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195
Rep:
Quote:
Originally Posted by introuble
#2. What do you mean no error messages?
Code:
perror("write");
Ok, I confused perror with that other command which writes to the system log. I don't call that (the other one) an error message to the user, but perror does the job.
Quote:
Originally Posted by introuble
And about the reference to that other thread .. look who's the thread starter
Cheers.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.