리눅스 소켓 프로그래밍에서 .jpg 파일을 전송하려고 하는 과정에서 질문있습니다.
리눅스에서 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);
}
댓글 달기