int direct_io_write(int fd, void* buff, ssize_t size) { int write_total = size; int write_size = 0; int writen_size = 0; int update_size = 0; while(size > 0){ write_size = write_to_buffer((char*)buff + writen_size, size); size -= write_size; writen_size += write_size; //DIO_TRACE("write_pos = %d", write_pos); if(size > 0 || DIRECT_IO_BUFFER_SZ == write_pos){ update_size = update_to_file(); if(0 == update_size){ break; } } } return write_total - size; }
int main(int argc, char *argv[]){ struct sockaddr_in si_me, si_other; int s, i, slen=sizeof(si_other); int port; char *ep; FILE *fp = fopen(argv[2], "w+"); //check if the arguements take are 3 or not if(argc!=3){ usage(); } //get the port number port = strtoul(argv[1], &ep, 10); if ( ( s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP) ) == -1 ) { printf("Error in creating socket\n"); exit(0); } memset((char *) &si_me, 0, sizeof(si_me)); si_me.sin_family = AF_INET; si_me.sin_port = htons(PORT); si_me.sin_addr.s_addr = htonl(IP); si_other.sin_family = AF_INET; si_other.sin_port = htons(port); si_other.sin_addr.s_addr = htonl(IP); if ( bind(s, (struct sockaddr*)&si_me, sizeof(si_me)) == -1 ) { printf("Error in binding the socket\n"); return 2; } srand(time(0)); (void) signal(SIGINT, sig_handler); int packet_id = 1; //start to listen while(flag){ generate_packet(packet_id); sendto(s, buffer, strlen(buffer) + 1, 0, (struct sockaddr*)&si_other, sizeof(si_other)); packet_id++; //check source ip and destination ip add to counter if (source==0||source==1||source==2){ if(destination==3 ||destination==4){ sendBtoA++; }else{ if(source<=8){ sendBtoC++; }else{ invalied++; } } }else if(source ==3||source==4){ if(destination<=2){ sendAtoB++; }else{ if(source<=8){ sendAtoC++; }else{ invalied++; } } }else{ if(source<=2){ sendCtoB++; }else{ if(source<=4){ sendCtoA++; }else{ invalied++; } } } if (packet_id%2 ==0){ sleep(2); } if(packet_id%20==0){ update_to_file(fp); } } update_to_file(fp); fclose(fp); close(s); return 0; }