Verständnis Problem

This is a discussion on Verständnis Problem within the Linux Programming forums, part of the Platform Specific Boards category; Hallo, ich verstehe diese Zeile nicht, durch das %16 denkt man : Code: if(((i%16)==15) || (i == length-1)) { for ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    3

    Verständnis Problem

    Hallo, ich verstehe diese Zeile nicht, durch das %16 denkt man
    :
    Code:
      if(((i%16)==15) || (i == length-1)) {
    
            for (  j = 0; j < length; j++)
    wieso i%16 rest 15 oder length-1 und dann das mit der j for schleife kann das wer villt versträndlicher erklären



    Hier mal der ganze Code:

    simple_server.h

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include "hacking.h"
    
    #define PORT 7790
    
    int
    main(void)
    {
    
    int sockfd, new_sockfd;
    struct sockaddr_in host_addr, client_addr;
    int recv_length=1, yes=1;
    socklen_t sin_size;
    char buffer[1024];
    
    if((sockfd=(PF_INET, SOCK_STREAM, 0))==-1)
    perror("in socket");
    
    if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int))==-1)
    perror("in setting options");
    
    host_addr.sin_family = AF_INET;
    host_addr.sin_port = htons(PORT);
    host_addr.sin_addr.s_addr = 0;
    memset(&(host_addr.sin_zero), '0', 8);
     
    
    
    if(bind(sockfd, (struct sockaddr *)&host_addr, sizeof(struct sockaddr))==-1)
    perror("in binding");
    
    if(listen(sockfd, 5)==-1)
    perror("in listening on socket");
    
    while(1){
    
    sin_size = sizeof(struct sockaddr_in);
    new_sockfd = accept(sockfd, (struct sockaddr *) &client_addr, &sin_size);
    if(new_sockfd == -1)
    perror("accepting connection");
    
    printf("server got connection from %s prot %d\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
    
    send(new_sockfd, "Hello, world!\n", 13,0);
    recv_length = recv(new_sockfd,&buffer,1024,0);
    
    while (recv_length > 0) {
        printf( "RECV: %d bytes\n", recv_length );
        dump(buffer, recv_length);
        recv_length = recv(new_sockfd, &buffer, 1024, 0);
    
    }
    close(new_sockfd);
    }
    return 0;
    }
    hacking.h :

    Code:
    void dump(const unsigned char *data_buffer, const unsigned int length) {
        unsigned char byte;
        unsigned int i,j;
        for ( i = 0; i < length; i++)
            {
            byte = data_buffer[i];
            printf( "%02x ", data_buffer[i]);
            if(((i%16)==15) || (i == length-1)) {
    
            for (  j = 0; j < length; j++)
                    printf ("    ");
            printf( "|  " );
            for ( j =(i-(i%16)); j <= i; j++)
                    {
                        byte = data_buffer[j];
                if((byte > 31) && (byte < 127))
                printf( "%c", byte );
                else
                    printf( "." );
       
                    }
            printf( "\n" );
    }
    }
    }

  2. #2
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Last time I spoke German was like 25+ years ago but I think he/she is asking about why the modulus 15 || length-1 logic is what it is....my guess is that it is simply watching for the end of a line of data to format it assuming they want to break on every 16th (0-15) data item or if it is the last element in the buffer...just me guessing and my German is so old and unused that he might be asking for a biscuit recipe or something..
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    3
    hey thanks for helping me even with picking wrong language...

    i have a roblem with getting this
    my guess is that it is simply watching for the end of a line of data to format it assuming they want to break on every 16th (0-15) data item or if it is the last element in the buffer
    for example i is 15 then 15/16 = 0,9375

    (rest = 9,375)

    and thats not == 15

    okay i understand that this is watching for end of i (i == length-1)


    but what us that for ???
    ((i%16)==15) why rest 15

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    1,209
    With integer math 15/16 = 0, rest = 15. The modulus calculates the rest based on integer division and thus 15%16=15, just like 16%16=0, 17%16=1 (17/16=1, rest 1) and so on.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 05:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 09:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 07:54 PM

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