리눅스 함수 관련 질문있어요..

cksqhddl의 이미지

안녕하세요.!
질문이 2개 있는데요 클라이언트 소스를 보면

/* 수신 한 데이터를 저장 할 파일 오픈 */
fd = open("receive.dat", O_WRONLY|O_CREAT|O_TRUNC);

이 부분에서 receive.dat파일명으로 다운받지 않고
[질문1] 제가 입력한 파일명으로 다운받고 싶다면 "receive.dat"대신에 getName을 넣으면 되나요 ?

그리고 [질문2] 홈 디렉터리가 아닌 제가 원하는 디렉터리에 받으려면 어떻게 해야하나요 ?

이리저리 해보고 있는데 문법에 익숙하지 않아서 답답하네요..

도움부탁드려요 !
------------------ client ------------------------
#define BUFSIZE 30
void error_handling(char *message);
int main(int argc, char **argv)
{
int fd;
int sd;

char buf[BUFSIZE];
int len;
struct sockaddr_in serv_addr;

if(argc!=3){
printf("Usage : %s \n", argv[0]);
exit(1);
}

/* 수신 한 데이터를 저장 할 파일 오픈 */
fd = open("receive.dat", O_WRONLY|O_CREAT|O_TRUNC);
if(fd == -1)
error_handling("File open error");

/* 서버 접속을 위한 소켓 생성 */
sd=socket(PF_INET, SOCK_STREAM, 0);
if(sd == -1)
error_handling("socket() error");

memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family=AF_INET;
serv_addr.sin_addr.s_addr=inet_addr(argv[1]);
serv_addr.sin_port=htons(atoi(argv[2]));

if( connect(sd, (struct sockaddr*)&serv_addr, sizeof(serv_addr))==-1 )
error_handling("connect() error!");

{/* 서버로부터 파일명 리스트를 받아서 전송대상 파일명을 서버에 보낸다. */
char fileList[10000];
char getName[1024];
char *p = fileList;

len=read(sd, fileList, 10000 );
fileList[len]='\0'; /* 끝표시 */

printf("서버의 파일 리스트는 \n------\n%s-----\n", fileList);
printf("전송받을 파일명을 입력하시오 ->");
gets(getName);
write(sd, getName, strlen(getName)); /* 파일명 전송 */
}

/* 데이터를 전송 받아서 파일에 저장한다 */
while( (len=read(sd, buf, BUFSIZE )) != 0 )
{
write(fd, buf, len);
}

/* 전송해 준것에 대한 감사의 메시지 전달 */
write(sd, "Thank you\n", 10);
close(fd);
close(sd);
return 0;
}

void error_handling(char *message)
{
fputs(message, stderr);
fputc('\n', stderr);
exit(1);
}

cksqhddl의 이미지

인터넷소켓으로 코딩했는데 노트북 2대가 연결이 되지 않는관계로,
하나의 vmware안에서 프롬프트 2개 띄워서 하나는 서버 하나는 클라이언트로 테스트하고있는데요
getName으로 실행하면 에러없이 잘 동작하나 해당 파일 크기가 0으로 아무것도 쓰여지지않고있어요..

shint의 이미지

서버는 DevC++
클라이언트는 CYGWIN

컴파일 방법 2가지
gcc -o main.exe main.c
g++ -o main.exe main.cpp

소켓 프로그래밍에서는 send recv 를 사용하는데. 여기서는 read write 를 사용하네요.
NULL == '\0' == 0x00 값으로 초기화 하니. recv 가 리턴되는거 같습니다.

검증 방법은 이렇습니다.
- 각 함수를 구글에서 찾기
- 각 함수를 리눅스 소켓 프로그래밍 책에서 찾기
- 인자값. 리턴값. 오류값을 printf() 문으로 하나씩 확인

open() 으로 생성한 파일을 close() 하셔야 할겁니다.
fopen() 은 fclose()

자세한 사항은 책과 구글. 네이버. 다음에서. 함수 하나씩 검색해보시면서 확인이 가능합니다.

댓글 첨부 파일: 
첨부파일 크기
Package icon test read() write() 소켓 테스트.zip408.87 KB

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

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

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

댓글 달기

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