/*--------------------------------------------------------------------------------------------- ÆÄÀϸí : socket_delay.c ±â ´É : ¼ÒÄÏ ÇÔ¼ö ó¸® ¼Ò¿ä ½Ã°£À» [nsec] ´ÜÀ§·Î ÃøÁ¤ ÄÄÆÄÀÏ : cc -o socket_delay socket_delay.c -lsocket -lnsl -lposix4 ½ÇÇ࿹ : socket_delay ----------------------------------------------------------------------------------------------*/ #include #include #include #include #include #include #define IP_addr "203.252.65.3" /* ÀÓÀÇÀÇ IP ÁÖ¼Ò */ #define port_number 7 /* echo ¼­ºñ½º Æ÷Æ®¹øÈ£ */ long calclock (struct timespec *myclock); /* ½Ã°£ Â÷À̸¦ °è»êÇÏ´Â ÇÔ¼ö */ void insertnull (char *ps, int len); /* ÃʱâÈ­ ÇÔ¼ö*/ void main(void) { int s; /* ¼ÒÄϹøÈ£ */ int cd, wd, rd; struct sockaddr_in serv_addr; struct timespec myclock[2]; long timedelay; char wmsg[] = " To be or not to be, that is the problem..."; char rbuf[100]; bzero((char *)&serv_addr, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = inet_addr(IP_addr); serv_addr.sin_port = htons(port_number); /* ----------------------------------- socket() ó¸® ½Ã°£ ÃøÁ¤ -------------------------------- */ clock_gettime(CLOCK_REALTIME, &myclock[0]); s = socket(PF_INET, SOCK_STREAM, 0); clock_gettime(CLOCK_REALTIME, &myclock[1]); timedelay = calclock( myclock ); printf("For 'socket' call : %10ld nsec \n", timedelay); /* --------------------------------- connect() ó¸® ½Ã°£ ÃøÁ¤ --------------------------------- */ clock_gettime(CLOCK_REALTIME, &myclock[0]); cd = connect(s, (struct sockaddr *)&serv_addr, sizeof(serv_addr)); clock_gettime(CLOCK_REALTIME, &myclock[1]); timedelay = calclock(myclock); printf("For 'connect' call : %10ld nsec \n", timedelay); /* ------------------------------------ write() ó¸® ½Ã°£ ÃøÁ¤ ---------------------------------- */ clock_gettime(CLOCK_REALTIME, &myclock[0]); wd = write(s, wmsg, sizeof(wmsg)); clock_gettime(CLOCK_REALTIME, &myclock[1]); timedelay = calclock( myclock ); printf("For 'write' call : %10ld nsec \n", timedelay); /* ------------------------------------ read() ó¸® ½Ã°£ ÃøÁ¤ ---------------------------------- */ clock_gettime(CLOCK_REALTIME, &myclock[0]); rd = read(s, rbuf, sizeof(rbuf)); clock_gettime(CLOCK_REALTIME, &myclock[1]); timedelay = calclock( myclock ); printf("For 'read' call : %10ld nsec \n", timedelay); /* ------------------------------------ close() ó¸® ½Ã°£ ÃøÁ¤ --------------------------------- */ clock_gettime(CLOCK_REALTIME, &myclock[0]); close(s); clock_gettime(CLOCK_REALTIME, &myclock[1]); timedelay = calclock( myclock ); printf("For 'close' call : %10ld nsec \n", timedelay); } /*----------------------------------------------------------------------------------------------------------------- ¹è¿­ myclock[] ³»ÀÇ ½Ã°£Â÷¸¦ ¸®ÅÏÇÏ´Â ÇÔ¼ö -----------------------------------------------------------------------------------------------------------------*/ long calclock( struct timespec *myclock ) { long timedelay, temp, temp_n; if (myclock[1].tv_nsec >= myclock[0].tv_nsec) { temp = myclock[1].tv_sec - myclock[0].tv_sec; temp_n = myclock[1].tv_nsec - myclock[0].tv_nsec; timedelay = 1000000000 * temp + temp_n; } else { temp = myclock[1].tv_sec - myclock[0].tv_sec - 1; temp_n = 1000000000 + myclock[1].tv_nsec - myclock[0].tv_nsec; timedelay = 1000000000 * temp + temp_n; } return timedelay; }