/*---------------------------------------------------------------------------------------------------------------- ÆÄÀϸí : talk_client.c ±â ´É : ÅäÅ© ¼­¹ö¿Í 1:1 Åë½ÅÀ» Çϴ Ŭ¶óÀ̾ðÆ® ÇÁ·Î±×·¥ ÄÄÆÄÀÏ : cc -o talk_client talk_client.c readline.c -lsocket -lnsl »ç¿ë¿¹ : talk_client 192.203.144.11 3000 ----------------------------------------------------------------------------------------------------------------*/ #include #include #include #include #include #include #include #include #define MAXLINE 1024 int readline(int, char *, int ); /* Å°º¸µå¿¡¼­ ÇÑ ÁÙ¾¿ Àд ÇÔ¼ö */ char *escapechar = "exit\n"; /* Á¾·á¹®ÀÚ Á¤ÀÇ */ int main(int argc, char *argv[]) { char line[MAXLINE], sendline[MAXLINE], recvline[MAXLINE+1]; char *haddr; int n, size, comp, port; pid_t pid; static int s; static struct sockaddr_in server_addr; /* ¸í·É¹® ÀÔ·Â ÀÎÀÚ Ã³¸® */ if (argc < 3) { printf("Usage: %s SERVER_ADDRESS TCP_PORT\n", argv[0]); return -1; } else { haddr = argv[1]; /* ÅäÅ© ¼­¹ö ÁÖ¼Ò */ port = atoi(argv[2]); /* ÅäÅ© ¼­¹öÀÇ Æ÷Æ®¹øÈ£ */ } /* ¼ÒÄÏ »ý¼º */ if((s = socket(PF_INET, SOCK_STREAM, 0)) < 0) { printf("Client: Can't open stream 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 = htons(port); /* ¼­¹ö¿¡ ¿¬°á ¿äû */ if(connect(s,(struct sockaddr *)&server_addr,sizeof(server_addr)) < 0) { printf("Client: can't connect to server.\n"); return -1; } if(( pid = fork()) > 0 { /* ºÎ¸ð ÇÁ·Î¼¼½º´Â Å°º¸µå ÀÔ·ÂÀ» ¼­¹ö·Î Àü¼Û */ while(readline(0, sendline,MAXLINE) != 0) { size = strlen(sendline); if(write(s, sendline, strlen(sendline)) != size) { printf("Client: can't write to server.\n"); return -1; } /* Á¾·á¹®ÀÚ¿­ ÀԷ ó¸® */ if(strncmp(sendline, escapechar, 4) == 0) { kill(pid, SIGQUIT); break; } } } else if (pid == 0) { /* ÀÚ½Ä ÇÁ·Î¼¼½º´Â ¼ÒÄÏÀ¸·ÎºÎÅÍ µé¾î¿À´Â ¸Þ½ÃÁö¸¦ È­¸é¿¡ Ãâ·Â */ while(1) { if((size = read(s, recvline, MAXLINE)) > 0) { recvline[size] = '\0'; /* Á¾·á¹®ÀÚ¿­ ¼ö½Å½Ã Á¾·á */ if(strncmp(recvline, escapechar,4) == 0) break; printf("%s", recvline); } } } close(s);