소켓통신 파일입출력 관련해서 질문드립니다.

gklv6088의 이미지

소켓통신에서 파일전송 프로그램을 구현하고있는데
서버에서 클라이언트가 요구한 파일을 열어서 버퍼를 통해서 읽고, 그걸 클라이언트 소켓으로 write해주는 부분에서 프로그램이 멈추어 버립니다..

이게 아예전송이 안되는것도아니고 전송은 끝까지 다 되는데 전송을 마치고나면 EOF를 읽어 while문을 벗어나야하는데 전송을 마치고 그냥 그부분에서 멈추어 버리는것 같습니다.

서버에서 파일 write 부분 아래에 shutdown(cli_sock,SHUT_WR)으로 클라이언트소켓을 닫아주었을땐 잘동작하였는데, 연속으로 파일을 다운로드하기위해 shutdown파트를 빼버리니까 그부분에서 멈추어 버립니다. 아마 출력버퍼에 뭔가 막힌게 있어서 그런가 싶은데.. 어떻게 해결해야할지 도저히 모르겠네요..ㅠ
지금 이틀째 이부분을 고치지 못하고 있습니다
문제가 되는 코드는 아래와 같고 결과화면은 파일첨부하였습니다
부디 조언 부탁드립니다ㅠ

// 클라이언트에 전송할 파일 오픈
          <서버부분>
            printf("file %s 오픈 전 \n", file_name);
            filefd = open(file_name, O_RDONLY );   
            if(filefd  -1)  
             error_handling("File open error");  
            printf("file %s 오픈 완료 \n",file_name);
        // 클라이언트에 데이터 전송 
            printf("데이터 전송전 \n");
         while((datalen = read(filefd,buf,BUFSIZE)) > 0)
            {
 
                	writen(cli_sock, buf, datalen);
                    memset(buf, 0, BUFSIZE);
 
            }
             close(filefd);
 
////////////////////////////////////////////////////////////////////////////////
         <클라이언트>
          /* 수신 한 데이터를 저장 할 파일 오픈 */  
            filefd = open(file_name, O_WRONLY|O_CREAT|O_TRUNC, __S_IREAD|__S_IWRITE);  
         if(filefd  -1)  
              error_handling("File open error");  
 
 
         memset(buf, 0, sizeof(buf));
 
    /* 데이터를 전송 받아서 파일에 저장한다 */  
         while( (len=read(sockfd, buf, BUFSIZE )) > 0 )  
         {  
             writen(filefd, buf, len);   
         }  
 
             write(sockfd, "file trasfer completed\n", 24);     
             close(filefd);   
             break;
File attachments: 
첨부파일 크기
Image icon 디버깅.PNG31.81 KB
라스코니의 이미지

수신 클라이인트의 read() 에서 빠져나와야 하는 조건이 무엇일 지 생각해 보세요.
서버측에서 shutdown()을 해주는 것은 클라이언트가 강제로 연결이 끊어지게 하는 것으로, 그 결과 약간의 부작용(?)으로 파일이 만들어지게 하는 것처럼 보일 뿐입니다.

먼저 수신 클라이언트에서 서버로부터 어느 정도 크기의 파일을 받아야 하는지 알수 있다면 read while() 구조에서 빠져나올 수 있지 않을까요?

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.