[질문] 간단한 소켓플밍입니다.

gostop의 이미지

#define BSIZE 1024
int main(int argc, char **argv)
{

int fd, ssock, csock, caddr_len, len;
char buf[BSIZE];
struct sockaddr_in saddr, caddr;

ssock=socket(PF_INET,SOCK_STREAM,0);
if(ssock==-1) error("socket() error!");

memset(buf,0,BSIZE);
memset(&saddr,0,sizeof(saddr));
saddr.sin_family=AF_INET;
saddr.sin_addr.s_addr=htonl(INADDR_ANY);
saddr.sin_port=htons(atoi(argv[2]));

if(bind(ssock, (struct sockaddr *)&saddr, sizeof(saddr))==-1) error("bind() error!");

if(listen(ssock,5)==-1) error("listen() error!");

while(1) {
    if((fd=open(argv[1],O_RDONLY))==-1) error("open() error!"); // <- 여기랑

    caddr_len=sizeof(caddr);
    if((csock=accept(ssock, (struct sockaddr *)&caddr, &caddr_len))==-1) {
        error("accept() error!");
        break;
    }

    while((len=read(fd,buf,BSIZE))!=0) {
        write(csock,buf,len);
    }

    if(shutdown(csock,1)==-1) error("shutdown() error!");

    read(csock,buf,sizeof(buf));
    printf("message: %s\n", buf);
    close(csock);
    close(fd);                 // <- 여기
}
}

질문 올리기 편하게 자잘한 것들은 뺏구요..
보시면 잘 아시겠지만 클라이언트에게 파일을 전송해주는 간단한 프로그램입니다.
여기서 궁금한게 위에 소스에서 주석으로 달아논 부분을 while(1){} 앞뒤로 빼면 디스크 I/O 가 좀더 줄어들거란 생각에 밖으로 빼주면..
처음엔 클라이언트가 파일을 잘 받아옵니다.
그런데 두번째부턴 파일을 못 받아오네요..
이 프로세스가 종료되기 전까진 파일이 계속 열려있는 상태가 될것 같은데 그게 아닌가요?
운형의 이미지

파일 포인터 위치때문이군요.

한번 파일을 끝까지 읽고 나면, 파일 포인터는 파일 마지막에 위치해있을 겁니다.

다음번 클라이언트가 접속을 하면, 현재 파일포인터 위치(파일 마지막)에서 부터 읽으려고 할 것이고, 더이상 읽을 것이 없어서 파일을 보내지 않을 겁니다.

open함수를 밖으로 루프 밖으로 뺀다면 lseek를 사용해서 파일 포인터의 위치를 파일 맨 앞으로 옮겨야 할겁니다.

이렇게 하면... file i/o에 의한 오버해드는 얼추 비슷할 것 같네요.

Do you think that's the air you are breathing now?

김충길의 이미지

위에분에 지적한 문제외에도 그냥 파일 내용을 던지는 것보다

처음 4바이트는 보낼 파일의 길이를 보내고 받는 쪽에서는 그만큼만

받게 하는게 좋을거 같습니다.

프로토콜 치고는 간단하지만 이렇게 하지 않고는 받는 측에서는
얼마까지 기다려야 하는지 모르지요.

screen + vim + ctags 좋아요~

댓글 달기

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