read의 리턴 값으로 1만 나와요.

news0426의 이미지

TCP 서버 통신해서
txt 파일을 받으려고 소스를 짜고 있는대요

while (len = read(sd, buf, BUFSIZE) != 0) {
printf("%d %s %d\n", fd, buf, len);
write(fd, buf, len);
printf("%d %s %d\n", fd, buf, len);
}

3 123123" 1
3 123123" 1

이렇게 나와요ㅠ

분명히 buf에는 123123이 읽혔는데 왜
read의 리턴 값은 1일까요.. 그거 때문에 write 해도 1 밖에 안 들어가요ㅠㅠ 고수님들 답좀 알려주세요ㅠㅠㅠ

qiiiiiiiip의 이미지


read가 데이터를 읽고 맨끝에 NULL을 써넣는것이 아니므로

buf를 문자열로 출력한 값이 이번에 새로 읽은 값이라고 볼 수는 없겠습니다.

read의 return이 1이라면 1byte만 읽혔다고 봐야할텐데,

BUFSIZE는 얼마인가요??

qiiiiiiiip의 이미지

아 아랫분 글 읽고 나니 다시 생각이..

while ( ( len = fread() ) != 0 )

으로 하셔야할듯..

지금은 while ( len = ( fread() != 0 ) ) 이니까 항상 1로 나오는 상황인듯..

news0426의 이미지

우와 완벽하게 해결 되었어요...
아 그렇게 돌아가는 군요ㅠ 퀴아이이이이이이이프 님의 멋진 내공에 놀랍니다ㅠ
해결!! 감사합니다^^

qiiiiiiiip의 이미지


컴파일러의 warning을 잘 보는게 중요합니다..
위의 코드의 경우 컴파일옵션에 -Wall을 넣고,
컴파일해보면 해당 부분에서

warning: suggest parentheses around assignment used as truth value

와 같은 warning이 뜹니다..

되도록이면 모든 warning을 없애는 방향으로 코딩을 하시면
디버깅 시간이 많이 줄어듭니다.. ^^

shint의 이미지

sd 를 읽어서. 그 길이를 BUFSIZE 만큼 얻고.
len 은 참일 동안만 돌고.
fd 를 찍고.
fd 에 쓰고.
fd 를 또 찍는군요.

저라면. BUFSIZE 랑 sd fd 랑 len 조건문. 초기화.랑 이전에 버퍼보다 큰 값이 들어가서 일어나는 문제. 같은걸 확인해보겠습니다.

while (1)
{
    len = read(sd, buf, BUFSIZE);
    if(len <= 0)
    {
        break;
    }
    printf("%d %s %d\n", fd, buf, len);
 
    write(fd, buf, len);
    printf("%d %s %d\n", fd, buf, len);
}

----------------------------------------------------------------------------
젊음'은 모든것을 가능하게 만든다.

매일 1억명이 사용하는 프로그램을 함께 만들어보고 싶습니다.
정규 근로 시간을 지키는. 야근 없는 회사와 거래합니다.

각 분야별. 좋은 책'이나 사이트' 블로그' 링크 소개 받습니다. shintx@naver.com

news0426의 이미지

감사합니다^^ 다음에 한번 활용해 보겠습니다!

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.