/*------------------------------------------------------------------------------------------- ÆÄÀϸí : mydaytime.c ±â ´É : daytime ¼­ºñ½º¸¦ ¿ä±¸ÇÏ´Â TCP(¿¬°áÇü) Ŭ¶óÀ̾ðÆ® ÄÄÆÄÀÏ : cc -o mydaytime mydaytime.c -lsocket -lnsl ½ÇÇ࿹ : mydaytime 203.252.65.3 --------------------------------------------------------------------------------------------*/ #include #include #include #include #define BUF_LEN 128 int main(int argc, char *argv[]) { int s, n; struct sockaddr_in server_addr; char *haddr; char buf[BUF_LEN+1]; if(argc != 2) { printf("usage: %s ip_address\n", argv[0]); return -1; } haddr = argv[1]; /* ¼ÒÄÏ »ý¼º */ if((s = socket(PF_INET, SOCK_STREAM, 0)) < 0) { printf("can't create socket\n"); return -1; } /* ¼­¹öÀÇ ¼ÒÄÏÁÖ¼Ò ±¸Á¶Ã¼ server_addrÀ» '\0'À¸·Î ÃʱâÈ­ */ bzero((char *)&server_addr, sizeof(server_addr)); /* server_addrÀÇ ÁÖ¼Ò ÁöÁ¤ */ server_addr.sin_family = AF_INET; server_addr.sin_addr.s_addr = inet_addr(haddr); server_addr.sin_port = htonl(13); /* ¿¬°á¿äû */ if(connect(s, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) { printf("can't connect.\n"); return -1; } /* ¼­¹ö°¡ º¸³»¿À´Â daytime ¸Þ½ÃÁöÀÇ ¼ö½Å ¹× È­¸éÃâ·Â */ while((n = read(s, buf, BUF_LEN)) > 0) { buf[n] = '\0'; printf("%s", buf); } close(s); }