LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   buggy c codrPlP (https://www.linuxquestions.org/questions/programming-9/buggy-c-codrplp-366881/)

alaios 09-25-2005 10:20 AM

buggy c codrPlP
 
Plz take a look at the following code

Code:

void send_diff(struct configuration *diff,union qdisc *qdisc){

        int                    sock,n,temp,sock2,n2,counter;
        socklen_t              server_len,server_len2;
        struct sockaddr_in      server,client,server2,client2;
        struct hostent          *host,*host2;

        if (!(host=gethostbyname(DIFFSERV))){ //?? Huge problem how to read value from file
                perror("Client gethostname");
        }
        memset(&server,0,sizeof(server));
        server.sin_family= AF_INET;
        memcpy(&server.sin_addr,host->h_addr,host->h_length);
        server.sin_port=htons((9997));
        if ((sock=socket(PF_INET, SOCK_DGRAM, 0)) < 0){
                perror("Client socket ");
        }
        memset(&client,0,sizeof(client));
        client.sin_family      = AF_INET;
        client.sin_addr.s_addr  = htonl(INADDR_ANY);
        client.sin_port        = htons(9997);

        if ( bind(sock, (struct sockaddr *) &client, sizeof(client)) <0){
                perror("Client bind ");
        }
        server_len=sizeof(server);
        printf("To i exei timi %d \n",i);

        for (counter=0;counter<5;counter++){
                if (sendto( sock, diff , sizeof(struct configuration), 0,(struct sockaddr *) &server, server_len) <0){
                        perror("Client send to");
                        close(sock);
                }
                sleep(5);
        }

        close(sock);
        sleep(10);
        server2.sin_family= AF_INET;
        memcpy(&server2.sin_addr,host->h_addr,host->h_length);
        server2.sin_port=htons((9990));
        if ((sock2=socket(PF_INET, SOCK_DGRAM, 0)) < 0){
                perror("Client socket ");
        }
                       
 memset(&client2,0,sizeof(client));
        client2.sin_family      = AF_INET;
        client2.sin_addr.s_addr  = htonl(INADDR_ANY);
        client2.sin_port        = htons(9990);

        if ( bind(sock2, (struct sockaddr *) &client2, sizeof(client2)) <0){
                perror("Client bind ");
        }
        server_len2=sizeof(server2);
        for(counter=0;counter<5;counter++){
                printf("Mesa sti loop \n");
                if (sendto( sock2, qdisc_stats, sizeof(qdisc_stats), 0, (struct sockaddr *) &server2, server_len2) <0){
                        perror("Client send to");
                        close(sock2);
                }
                sleep(5);
        }
        for(temp=0;temp<3;temp++){
                printf("Stelno rate %s \n",qdisc_stats[temp].htbstats.rate);
                printf("Stelno ceil %s \n",qdisc_stats[temp].htbstats.ceil);

        }
        printf("Finished sending structs \n");
        close(sock2);

}

As u can see this code has two parts... Each part sends five time a message to a given port... When i restart the pc this code works FINE!!!! which means that i send and receive packets as well... This is only for the first time... When i retry to execute this programme only the first part works fine.... the second one DOESNOT SEND ANYTHING (checked with tcpdump) and not print error with the perror function.... I dont know why..... I have checked this many times (i am bored restarting my pc) Plz check the code... perhaps i reserve a resource that i dont release so i cannot reuse it....(just suggestions)
Do u have anything else i mind?

alaios 09-25-2005 06:11 PM

Hmmm i have spend a few hours with this prob!!! Stil not working :(
I am still asking why this thing works only for one time and not for a second one :(( shit!!!
I have thought to use the gdb to check it out... but i dont think that debugger can execute step by step the sendto(... ) system call .. I think that the problem relies for sth that my programmes use and dont release after it is killed... I dont know if u agree or nor.. but suggest everything u can think and i am here to test it


All times are GMT -5. The time now is 11:31 AM.