/*------------------------------------------------------------------------------------------------------------
ÆÄÀϸí : mydaytime.c
±â ´É : daytime ¼ºñ½º¸¦ ¿ä±¸ÇÏ´Â TCP(¿¬°áÇü) Ŭ¶óÀ̾ðÆ®
ÄÄÆÄÀÏ : cc -o mydaytime mydaytime.c -lsocket -lnsl
½ÇÇ࿹ : mydaytime 192.203.144.11
-------------------------------------------------------------------------------------------------------------- */
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#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);
}
|