Like Tree1Likes
  • 1 Post By brewbuck

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

  1. #1
    Registered User Annonymous's Avatar
    Join Date
    Apr 2011
    Location
    Jackson, New Jersey, United States
    Posts
    302

    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!

    Reference:
    Code:
    bcopy((char *)server->h_addr,
             (char *)&serv_addr.sin_addr.s_addr,
             server->h_length);
    Last edited by Annonymous; 06-24-2011 at 11:04 AM.

  2. #2
    Registered User Annonymous's Avatar
    Join Date
    Apr 2011
    Location
    Jackson, New Jersey, United States
    Posts
    302
    And I do realize I post some crazy stuff ^ lol

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,467
    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:
    Quote 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).

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    A simple way to do this if you know the ip address and port number:

    Code:
    	int sock = socket(PF_INET, SOCK_STREAM, 0);
    	struct sockaddr_in addr;
    
    	addr.sin_family = AF_INET;
    	inet_aton("127.0.0.1", (struct in_addr*)&(addr.sin_addr));
    	addr.sin_port = htons(1332);
    
    	if (connect(sock, (struct sockaddr*)&addr, sizeof(struct sockaddr_in)) == -1) {
    		fprintf(stderr,"%s\n", strerror(errno));
    	};
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    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.
    rags_to_riches likes this.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    Registered User Annonymous's Avatar
    Join Date
    Apr 2011
    Location
    Jackson, New Jersey, United States
    Posts
    302
    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

    Code:
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
        if (sockfd < 0) 
            fprintf("ERROR opening socket");                             
        bzero((char *) &serv_addr, sizeof(serv_addr));
        server = gethostbyname(argv[1]);
        if (server == NULL)
            fprintf(stderr,"ERROR, no such host\n");
        portno = atoi(argv[2]);
        serv_addr.sin_family = AF_INET;
        /*bcopy(src, dest, len)*/
        bcopy((char *)server->h_addr,
             (char *)&serv_addr.sin_addr.s_addr,
             server->h_length);
        serv_addr.sin_port = htons(portno);
        if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0) 
            fprintf(stderr, "ERROR connecting");
    @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?

    Sent from my iPhone
    Last edited by Annonymous; 06-25-2011 at 08:00 PM.

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Annonymous View Post
    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:

    Quote 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.

    And beware tutorials that use depreciated and non-standard methods
    Last edited by MK27; 06-26-2011 at 08:16 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 05-12-2011, 01:02 AM
  2. struct holding data inside a linked list struct
    By icestorm in forum C Programming
    Replies: 2
    Last Post: 10-06-2009, 12:49 PM
  3. pointers, hostent and gethostbyname
    By whackaxe in forum Networking/Device Communication
    Replies: 1
    Last Post: 07-06-2004, 01:57 PM
  4. bcopy() question
    By failure_to in forum C Programming
    Replies: 6
    Last Post: 06-04-2004, 04:23 PM
  5. Replies: 1
    Last Post: 05-05-2004, 06:58 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21