리눅스 소켓 프로그래밍에서 .jpg 파일을 전송하려고 하는 과정에서 질문있습니다.

rkkk의 이미지

리눅스에서 vim으로 소스를 작성하고 있습니다.
제가 서버에서 클라이언트로 사진을 전송하려고 합니다. 폴더 /var/www 여기에 .jpg 파일로 사진이 있으면 이것을 접속한 클라이언트에게 사진을 전송하려고 합니다.
구글링을 해보니 바이너리파일로 전송을 해야한다고 하시고, fseek 으로 어떻게 하는거같은데 오늘 하루종일 붙잡고했는데 잘 안되네요 ㅠㅠㅠ
고수분들 도와주십시오 ㅠㅠ

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include

#define PORT 1234
#define MAX 61440

void error_handling(char *message);

int main()
{
int s_socket,c_socket;
struct sockaddr_in s_addr, c_addr;
int len,length;
int n;
char *temp;
char *name;
DIR *dp;
struct dirent *dir;
char *dir_path = "/var/www";
FILE *file = NULL;
char buf[MAX];
int count=0;
size_t fsize =0, nsize=0, fpsize=0;
size_t fsize2 =0;

printf("start\n");
s_socket=socket(PF_INET, SOCK_STREAM, 0);

if(s_socket ==-1)
error_handling("socket() error ");
memset(&s_addr,0,sizeof(s_addr));
s_addr.sin_addr.s_addr=htonl(INADDR_ANY);
s_addr.sin_family = AF_INET;
s_addr.sin_port = htons(PORT);

if(bind(s_socket, (struct sockaddr *)&s_addr, sizeof(s_addr)) ==-1)
error_handling("bind () error");
printf("bind\n");

if(listen(s_socket,5)==-1)
error_handling("listen() error");
printf("listen\n");

len = sizeof(c_addr);
c_socket = accept(s_socket, (struct sockaddr*)&c_addr, &len);

if(c_socket ==-1)
error_handling("accept() error");
printf("client accept\n");

while(1)
{
if((dp=opendir(dir_path))==NULL)
{
printf("opendir()error!\n");
exit(EXIT_FAILURE);
}

printf("open dir\n");
while((dir=readdir(dp))!=NULL)
{
if(dir->d_ino==0)
continue;

else{
printf("name\n");
name = dir->d_name;
printf("%s\n",name);
count++;
if(count==3)
break;
}
}
closedir(dp);
break;
}
printf("loop end\n");

file = fopen(name,"rb");
printf("file open\n");

fseek(file,1,SEEK_SET);
//fseek(file,0,SEEK_END);
fsize = ftell(file);
//fseek(file,0,SEEK_SET);

printf("file size send before\n");
send(c_socket,&fsize, sizeof(fsize),0);
printf("file size send\n");

while(nsize!=fsize)
{
printf("while loop\n");
fpsize = fread(buf,1,MAX,file);
nsize = nsize+fpsize;
send(c_socket,buf,fpsize,0);
printf("file send\n");
}

printf("while end\n");
fclose(file);
printf("end\n");
return;
}

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

댓글 달기

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