bcopy & hostent struct

This is a discussion on bcopy & hostent struct within the Networking/Device Communication forums, part of the General Programming Boards category; So, I'm trying to memorize and learn the make up of the client model. I ran across the function bcopy. ...

bcopy & hostent struct

So, I'm trying to memorize and learn the make up of the client model. I ran across the function bcopy. The tutorial I am using is awesome it really broke it down for me. http://www.linuxhowtos.org/C_C++/socket.htm is what i have so far right? I am using the variable, server, to point to the struct type hostent. I am getting the ip address with gethostbyname, netdb header. Storing it in h_addr in the hostent struct. Now bcopy, I copy a specified length from the source to the destination. I copy the ip length(in h_length), dereference the struct variable server and access h_addr, copying the ip to serv_addr.sin_addr.s_addr. Where as in the server I would set serv_addr.sin_addr.s_addr to INADDR_ANY; The tutorial can only teach so much, as much as i can "understand"! Help from experienced programmers, can tell me if what i learned was correct or not. Any further insight, or more info would be appreciated!

Don't bother "memorizing" the client model, beyond the basic "it connects to a server that is waiting to talk to somebody, then they talk back and forth, then they close the connection". That's all you really need for basic network programming. Memorizing this stuff will come from practice and using it. It's most important to get the big picture and know where to find the details (Google, man pages, etc). As for that tutorial, I scanned it very (I mean very) briefly, and it seems okay. Here's one I really like if you want another reference: Beej's Guide to Network Programming.

I can't say much about the code, your summary seems fine, but it's hard to tell because you only posted one line. The only problem with that line is that bcopy is deprecated:

Originally Posted by man bcopy

CONFORMING TO
4.3BSD. This function is deprecated (marked as LEGACY in POSIX.1-2001): use memcpy(3) or memmove(3) in new programs. Note that the first two arguments are interchanged
for memcpy(3) and memmove(3). POSIX.1-2008 removes the specification of bcopy().

You use memcpy if the src and dest don't overlap, or memmove if they do. Many network programming guides seem to stick to deprecated functions like bcopy (or they're just all old). Also, memcpy/memmove take void * as params, so no cast is necessary. Just memcpy(&serv_addr.sin_addr.s_addr, server->h_addr, server->h_length). Notice that the src and dst parameters are switched (dst comes first here).

bcopy() is an ancient, deprecated function which only seems to occur in stupid script kiddie networking tutorial code. Don't use it. Seriously, when I see it I assume I'm looking at stupid cracker code.

anduril426, I know about beej's guide. I'm not that found of it! A lot of people swear by it but, this is the tutorial I have been using from the beginning so I'm going to stick with it thanks! And as for bcopy, yeah a lot of tutorials use it As well as books. I actually went to Barns&Nobels to order a *Nix TCP Socket book and the newest they had was from 1999-2001. But is using memcpy a necessary switch? And the way I learn is to memorize something. I start off coding it, over and over. As I go along, I study what the functions are and what they do, how they work. Im not just memorizing. Im practicing, using compiling. Dreaming of it lol That is what I meant!

MK27 & anduril426,....

That's what I have and how I'm coding it. It compiles and runs here's the core of the code

@Brewbuck, I am only a year and a half into programming in c all together. Still wet behind the ears! And I am completely new to socket programming. Only a few months at most. I do not hack! It's bad news, although seems very interesting! But these are the tutorials I am using. They are using bcopy, I can't help that! But I will learn the alternatives! If you don't mind me asking, why is bcopy so bad to use?

First, portability. It is not just depreciated -- it's been completely removed from the POSIX specification. In fact, it is not part of the ANSI C standard and I think never was. I'm guessing it's a BSD function than isn't even used on BSD systems anymore, but might be, so compilers like gcc still support it.

Since you can do exactly the same thing with memmove, you might as well use memmove. However -- and I'd guess this is why bcopy was depreciated -- unless where you are copying to overlaps where you are copying from, you should just use memcpy(). Because:

Originally Posted by man memmove

The memory areas may overlap: copying takes place as though the bytes in src are first copied into a temporary array that does not overlap src or dest, and the bytes are then copied from the temporary array to dest.

"As though" here I think means "as though you had done it this way", and whether it was "you" that did it or the library that provides memmove(), that is how it will really have to be done. Remember, the specification does not dictate how an implementation should be done "under the hood", it just says what it should do for the user.

But to deal with the overlap, an implementation of memmove will almost certainly have to copy the entire from block into a temp buffer, or else it will have to do some other tricks requiring time and resources. The standard spec for memcpy(), on the other hand, says that if the segments are overlapping, the behavior is undefined. So you can't use memcpy for overlapping segments BUT the memcpy() implementation does not have to waste time and resources on that possibility.

Since copying areas of memory is a pretty standard activity, we might as well do it in the most efficient way possible, or C programming won't be worth the paper it's printed on. That way is memcpy(), or, if you have a potential overlap -- which is unusual -- memmove.

That's the second reason, it's probably why bcopy never made it into the standard, which is why its not portable. Short story: bcopy is depreciated, and has been for a long time. Learn to use memcpy instead. Just beware the order of the arguments to memcpy is opposite that of bcopy.