/* udpclient.c 0118 */ #include #include #include #include #include #include #include #include #include #define BUFSIZE 1024 void error_handling(char *message); int main(int argc, char **argv) { int sock; char message[BUFSIZE]; int str_len, addr_size; struct sockaddr_in serv_addr; struct sockaddr_in from_addr; addr_size = sizeof(&serv_addr ); struct stat st; char buf[BUFSIZE], d_name[BUFSIZE]; char mes[BUFSIZE]; char file_name[BUFSIZE]; int len , file_len; char size[BUFSIZE]; long file_size; char file_name12[BUFSIZE]; int i; int fd; int count = 1; //Å×½ºÆ® ¸ñÀû FILE *fp; if(argc!=3){ printf("Usage : %s \n", argv[0]); exit(1); } sock=socket(PF_INET, SOCK_DGRAM, 0); if(sock == -1) error_handling("socket() error"); memset(&serv_addr, 0, sizeof(serv_addr)); serv_addr.sin_family=AF_INET; serv_addr.sin_addr.s_addr=inet_addr(argv[1]); serv_addr.sin_port=htons(atoi(argv[2])); /* connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr));*/ /* ¼­¹ö ¿¬°á Å×½ºÆ® */ fputs( "Test : ", stdout); fgets( message , sizeof(message) , stdin ); printf("Message from client : %s" , message); sendto(sock , message , strlen(message) , 0 , (struct sockaddr*)&serv_addr , sizeof(serv_addr)); //write( sock , message , strlen(message)); str_len = recvfrom( sock , message , sizeof(message) , 0 , (struct sockaddr*)&serv_addr , &addr_size); message[str_len] = 0; printf(" Message from server : %s" , message); /*¼­¹ö ¿¬°á Å×½ºÆ® ¿Ï·á*/ sleep(1); system("clear"); while(1) { //server·Î ºÎÅÍ ¹ÞÀº ÆÄÀÏ À̸§µé Ãâ·Â len=recvfrom(sock, d_name, BUFSIZE , 0 , (struct sockaddr*)&serv_addr , &addr_size); if(!strcmp(d_name, "end")) { printf("\n\n"); break; } //file_nameÀü¼Û ¿Ï·á¸¦ ³ªÅ¸³»´Â 'end'Àü¼Û½Ã ¹«Çѹݺ¹ Á¾·á if(!strcmp(d_name, ".")||!strcmp(d_name, "..")) continue; //'.'À̳ª '..' Àü¼Û½Ã ¹«½Ã else { printf("%s\n", d_name); i++; if (i==8) { printf("\n"); i=0; } } //file_name Ãâ·Â } while (1){ while (1){ fputs("Input File name (Q to Quit):" , stdout); //¹ÞÀ» ÆÄÀÏÀÇ À̸§ ÀÔ·Â ¹Þ¾Æ¼­ fgets(file_name , sizeof(file_name)-1 , stdin); if(!strcmp(file_name, "q\n")) exit (1); //q¸¦ ÀԷ¹ÞÀ¸¸é ¹Ù·Î Á¾·á sendto( sock , file_name, strlen(file_name)-1, 0 , (struct sockaddr*)&serv_addr , sizeof(serv_addr) ); // write(sock, file_name, strlen(file_name)-1); //server·Î Àü¼Û sleep(1); len=recvfrom(sock, mes, sizeof(mes)-1 , 0 , (struct sockaddr*)&serv_addr , &addr_size); //server¿¡¼­ È®ÀÎµÈ ¹Ì½ÃÁö Àü¼Û¹ÞÀ½ mes[len]='\0'; if(!strcmp(mes,"Yes")) { printf("file to server sucess"); //ÆÄÀÏÀÌ Àִ°æ¿ì ´ÙÀ½À¸·Î.. break; } else printf("FILE open EORROR"); //ÆÄÀϾøÀ¸¸é ´Ù½Ã } //ÆÄÀÏ ¿­±â while(1) { if((fp=fopen(file_name, "wb")) ==NULL) { printf("fd %d\n", fp); // Å×½ºÆ® ¸ñÀû //°°Àº ÆÄÀÏ À̸§ÀÌ ÀÖÀ» °æ¿ì ÆÄÀÏ À̸§ ´Ù½Ã ¹Þ¾Æ¼­ ¿­±â fputs("°°Àº ÆÄÀϸíÀÌ Æú´õ³»¿¡ Á¸ÀçÇÕ´Ï´Ù »õ·Î¿î ÆÄÀϸíÀ» ÀÔ·ÂÇØ ÁÖ¼¼¿ä\n" , stdout); fgets(file_name12 , sizeof(file_name12) , stdin); strncpy(file_name,file_name12 , sizeof(file_name12)-2); printf("filename12 input test %d\n%s", count++ , file_name); //Å×½ºÆ® ¸ñÀû } else break; //¿­±â ¼º°ø½Ã ´ÙÀ½À¸·Î.. printf("file open"); //Å×½ºÆ® ¸ñÀû } while((file_len=recvfrom(sock, buf, BUFSIZE , 0 , (struct sockaddr*)&serv_addr , &addr_size )) > 0) { //server·Î ºÎÅÍ ÆÄÀÏÀÇ ³»¿ë Àü¼Û¹Þ¾Æ printf(" %d" , count++); //Å×½ºÆ® ¸ñÀû fwrite(buf, sizeof(char) ,file_len,fp); //ÆÄÀÏ ¾²±â } stat(file_name, &st); printf("file %d\n", st.st_size); len=recvfrom(sock, size, BUFSIZE-1 , 0 , (struct sockaddr*)&serv_addr , &addr_size); size[len]='\0'; printf("%s\n", size); file_size=atol(size); printf("file_size %ld\n", file_size); if ( st.st_size != file_size) { close(fd); printf("ÆÄÀÏ Àü¼Û ½ÇÆÐ!!! ´Ù½Ã Çѹø ½ÃµµÇØ ÁֽʽÿÀ\n"); fd=open(file_name, O_TRUNC); close(fd); } close(fd); } sendto(sock, "Thank you\n", 10 , 0 , (struct sockaddr*)&serv_addr ,sizeof(serv_addr)); //server¿¡ thank you¸Þ½ÃÁö Àü¼Û close(sock); return 0; } void error_handling(char *message) { fputs(message, stderr); fputc('\n', stderr); exit(1); }