/*----------------------------------------------------------------------------- ÆÄÀϸí : test_bind.c ±â ´É : ½Ã½ºÅÛÀÌ ÀÚµ¿À¸·Î ¹èÁ¤ÇÑ Æ÷Æ®¹øÈ£¸¦ Ãâ·ÂÇÏ´Â ÇÁ·Î±×·¥ ÄÄÆÄÀÏ : cc -o test_bind test_bind.c -lsocket -lnsl ½ÇÇ࿹ : test_bind ---------------------------------------------------------------------------- */ #include #include #include #include #include #include #include #define ADDRESS "192.203.144.11" #define PORT 7 #define MSG "Test Message" int main() { int sd1, sd2 ; /* ¼ÒÄϹøÈ£ */ struct sockaddr_in sin1, sin2 ; /* ¼ÒÄÏÁÖ¼Ò ±¸Á¶Ã¼ */ int addr_len ; /* ¼ÒÄÏÁÖ¼Ò ±¸Á¶Ã¼ÀÇ Å©±â */ u_short rtn1, rtn2; /* Æ÷Æ®¹øÈ£ */ /* TCP¿Í UDP µÎ°¡Áö ¼ÒÄÏ »ý¼º */ sd1 = socket(AF_INET, SOCK_STREAM, 0) ; sd2 = socket(AF_INET, SOCK_DGRAM, 0) ; sin1.sin_family = PF_INET ; sin1.sin_addr.s_addr = inet_addr(ADDRESS) ; sin1.sin_port = htons(PORT) ; if(connect(sd1, (struct sockaddr*)&sin1, sizeof(sin1))<0) { printf("Error : Connect failed!!!\n") ; exit(1) ; } addr_len = sizeof(sin2); if (getsockname(sd1, (struct sockaddr*)&sin2, &addr_len) < 0){ printf("getsockname error\n"); } rtn1 = sin2.sin_port ; if(sendto(sd2, MSG, strlen(MSG), 0, (struct sockaddr*)&sin1, sizeof(sin1))<0) { printf("Error : sendto failed!!\n") ; exit(1) ; } addr_len = sizeof(sin2); if(getsockname(sd2, (struct sockaddr*)&sin2, &addr_len)<0) printf("Error : getsockname error\n") ; rtn2 = sin2.sin_port ; /* ¹èÁ¤µÈ Æ÷Æ®¹øÈ£ Ãâ·Â */ printf("stream socket's bind return = %d\n", rtn1 ) ; printf("datagram socket's bind return = %d\n", rtn2) ; close(sd1) ; close(sd2) ;