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