메시지큐를 이용해서 파일 복사하기.

ratmhun의 이미지

데이터 전송 프로젝트 세번째, 메시지 큐를 이용해서 파일을 복사하는 프로그램입니다. 사실 메시지큐는 구조체를 이용해서 데이터를 넘길 수 있기 때문에 아무 생각 없이 파일명과 데이터를 한꺼번에 넘길 수 있어서 좋았습니다만...
역시 데이터의 길이, 파일명의 길이에 대해 완벽한 해법을 찾지 못하겠습니다. 이번에는 복사된 object 파일의 끝에 쓰레기 값이 붙는 것을 확인할 수 있었습니다. (제 생각으로는 1024 같이 Magic number를 써서 buf의 길이를 지정했기 때문에 이런 현상이 발생하는 것 같습니다.)

쓰레기 값이 붙지 않고 파일을 제대로 복사할 수 있는 방법이 없을까요?

거두절미하고 소스코드를 보시죠.
server.c

Quote:

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <fcntl.h>
#include <stdio.h>

#define MSQKEY 34855
#define FILENAMELEN 10
#define MSQSIZE 32

struct mymsgbuf {
char filename[FILENAMELEN];
char mtext[1024];
};

int main(int argc, char **argv)
{
void fatal(char *);
key_t key;
int n, object, msqid;
struct mymsgbuf mb = {0, 0};

key = MSQKEY;
if((msqid = msgget(key, IPC_CREAT|IPC_EXCL|0666)) < 0)
fatal("msgget");

while((n = msgrcv(msqid, &mb, sizeof(struct mymsgbuf), 0, 0)) > 0)
{
if((object = open(mb.filename, O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0)
fatal("write open");
write(object, mb.mtext, 1024);
}

if(msgctl(msqid, IPC_RMID, (struct msqid_ds *)0) < 0)
fatal("msgctl");
close(object);
return 0;
}

void fatal(char *error_name)
{
perror(error_name);
exit(1);
}

client.c

Quote:

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <fcntl.h>
#include <stdio.h>

#define MSQKEY 34855
#define FILENAMELEN 10
#define MSQSIZE 32

struct mymsgbuf {
char filename[FILENAMELEN];
char mtext[1024];
};

int main(int argc, char **argv)
{
void fatal(char *);
key_t key;
int n, msqid, source;
struct mymsgbuf mb = {0, 0};
char buf[1024];

if((source = open(argv[1], O_RDONLY)) < 0)
fatal("open readfile");

key = MSQKEY;
if((msqid = msgget(key, 0666)) < 0)
fatal("msgget");

strcpy(mb.filename, argv[2]);
while((n = read(source, buf, 1024)) > 0)
{
strncpy(mb.mtext, buf, n);
printf("%s", mb.mtext);
if(msgsnd(msqid, &mb, sizeof(struct mymsgbuf), 0) == -1)
fatal("msgsnd");
}
return 0;
}

void fatal(char *error_name)
{
perror(error_name);
exit(1);
}

익명 사용자의 이미지

n = msgrcv(msqid, &mb, sizeof(struct mymsgbuf), 0, 0))
에서 n은 메시지 큐로 부터의 받아진 text의 길이 입니다..
구조체에서 n만큼만 읽어 들리면 되는 겁니다..
보낼 때로 n만큼 보내면 길이가 맞게 되는 것이죠...

댓글 달기

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