소켓 프로그래밍 도중 궁금한 것이 생겨 질문드립니다.

ddt3wok의 이미지

안녕하세요 ? 평소 검색을 통해 본 커뮤니티 페이지에서 도움을 많이 얻고 있는 대학생입니다.
이번에 제가 학교 과제로 socket programming 에 대해 공부 중인데 관련 내용을 어떻게 검색해야할지 몰라 글을 남기게 되었습니다.

궁금한 것은
아래의 코드 실행결과가
-----------------------
filedata : ~~
bind()error!
end34411
-----------------------
인데

제 생각에는
-----------------------
filedata : ~~
end
bind()error!
34411
-----------------------
과 같이 되어야 하는데 말이죠 !
그런데 printf("end")가 printf("end\n")로 수정되면 제 생각과 같은 결과를 얻을 수 있습니다.
왜 그런지 도무지 모르겠습니다ㅠㅠ 도와주세요 ! (__)

void error_handling(char * message)
{
    fputs(message,stderr);
    fputc('\n',stderr);
    exit(1);
}
    void getFile(char * filename)
  {
      char buf[100];
      int fd = open(filename,O_RDONLY);
      if (fd == -1)
     {
      error_handling("open() error!");
        }
      printf("file descripyer: %d \n",fd);
      if(read(fd,buf,sizeof(buf))==-1)
          error_handling("read() error!");
 
      printf("file data : %s",buf);
      printf("end");
      close(fd);
  }
 
int main()
{
    int serv_sock;
    int clnt_sock;
    struct sockaddr_in serv_adr, clnt_adr;
    getFile("pw.txt");
    printf("34411");
 
    serv_sock = socket(PF_INET, SOCK_STREAM,0);
 
    if(bind(serv_sock, (struct sockaddr*)&serv_adr,
                  sizeof(serv_adr))==-1)
      {
          error_handling("bind() error!");
      }
}

Lipi의 이미지

error_handling("bind() error!");


printf("bind() error!\n");
exit(1);

로 바꾸면 어떻게 되나요?
ddt3wok의 이미지

그렇게 하니까 제가 원하는 결과가 나오네요 ! 이유는 아랫분께서 설명해주셨습니다. 답변감사합니다.(_ _)
좋은 하루 보내세요 !

yhsuk의 이미지

printf 는 stdout으로 출력을 내보내고, stdout은 기본적으로 버퍼를 사용합니다.
명시적으로 flush를 하거나 -> fflush(stdout);
stderr를 써서 출력하거나 -> fprintf(stderr, "bind() error!");
또는 "\n"을 사용하셨듯 printf에서 new line을 만나거나 하면 버퍼에 있는게 console로 찍힙니다.
아래 내용들을 참고하세요.

https://stackoverflow.com/questions/1716296/why-does-printf-not-flush-after-the-call-unless-a-newline-is-in-the-format-strin
http://yunzo.tistory.com/1

Signature :) - "여유를 갖고 행동하되 게을러지지 말자"

ddt3wok의 이미지

getchar() 를 이용하여 버퍼를 비우는 방법도 사용해봤는데 아마도 제 의도(버퍼 비우기)대로 동작하지 않았나봅니다.
답변 감사드립니다 (_ _) 덕분에 한걸음 더 성장했습니다. 좋은 하루 보내세요 !!

댓글 달기

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