UDP 로 리눅스에서 client->server 파일 전송 프로그램 만드는 중인데 클라이언트는 파일 다 보내고 끝나고 서버는 80 퍼센트 정도에서 멈춥니다...

익명 사용자의 이미지

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

#define MAXLINE 2000

int main(int argc, char *argv[])
{
struct sockaddr_in servaddr, cliaddr;
int listen_sock;
char buf[MAXLINE+1];
char cli_ip[20];
char filename[20];
int filesize=0;
int total=0, sread, fp;

int clnt_addr_size;

if(argc != 2) {
printf("usage: %s port \n", argv[0]);
exit(0);
}

// create a socket
if((listen_sock = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
perror("socket fail");
exit(0);
}
// fill servaddr with 0
bzero((char *)&servaddr, sizeof(servaddr));

// servaddr setting
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(atoi(argv[1]));

// bind()
if(bind(listen_sock, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)
{
perror("bind fail");
exit(0);
}

bzero( filename, 20 );
recvfrom(listen_sock, filename, sizeof(filename), 0 ,(struct sockaddr*)&cliaddr, &clnt_addr_size);
printf( "\n%s ", filename ); //receive the filename from the client. save it to filename.

// recv( accp_sock, &filesize, sizeof(filesize), 0 );
recvfrom(listen_sock, &filesize, sizeof(filename), 0 ,(struct sockaddr*)&cliaddr, &clnt_addr_size); //receive the file size
printf( "%d \n", filesize );

//strcat( filename, "_backup" );
fp = open( filename, O_WRONLY | O_CREAT | O_TRUNC,0400);

while( total != filesize )
{
sread = recvfrom(listen_sock, buf, 100, 0,(struct sockaddr*)&cliaddr, &clnt_addr_size ); //sread 는 받은 데이터 크기겠지, buf에다가 받은 데이터 집어넣음
printf( "\nfile is receiving now.. \n" );
total += sread;
buf[sread] = 0;
write( fp, buf, sread ); //위에 생성해놓은 file 에다가 buf에 들어있는 내용(클라에게 받은) 저장함.
bzero( buf, sizeof(buf) ); //메모리를 0으로 채운다
printf( "processing : %4.2f%% \n", total*100 / (float)filesize );

}

printf( "file traslating is completed \n" );
printf( "filesize : %d, received : %d \n", filesize, total );
close(fp);
close( listen_sock );
return 0;
}

위에가 서버

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

#define MAXLINE 2000

int main(int argc, char **argv )
{
struct sockaddr_in servaddr;
int s;
char buf[MAXLINE+1];
char filename[20];
int filesize, fp, filenamesize ;
int sread, total=0;

if(argc != 4)
{
printf("usage: %s ip_address, port, filename\n ", argv[0]);
exit(0);
}

if((s = socket(PF_INET, SOCK_DGRAM, 0)) < 0)
{
perror("socket fail");
exit(0);
}

bzero((char *)&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
inet_pton(AF_INET, argv[1], &servaddr.sin_addr);
servaddr.sin_port = htons(atoi(argv[2]));

strcpy(filename,argv[3]);
filenamesize = strlen(filename);

if( (fp = open( filename, O_RDONLY )) < 0 ) // allocate fp to filename and open. if it's less than 0, then open failed
{
printf( "open failed\n" );
exit(0);
}

sendto( s, filename, sizeof(filename), 0,(struct sockaddr*)&servaddr, sizeof(servaddr)); // send the server the filename.

filesize = lseek( fp, 0, SEEK_END ); // somehow find out the filesize
sendto( s, &filesize, sizeof(filename), 0,(struct sockaddr*)&servaddr, sizeof(servaddr)); // send the server the filesize
lseek(fp, 0, SEEK_SET );


while( total != filesize )
{
sread = read( fp, buf, 100 ); //read the file , 읽어들인 바이트 수 반환해서 sread에 저장. buf 에 파일 내용 저장.
printf( "file is sending now.. \n" );
total += sread;
buf[sread] = 0; // 널 문자 넣어서 끝에 boundary만들어줌
sendto( s, buf, sread, 0,(struct sockaddr*)&servaddr, sizeof(servaddr) ); // send the server the file. buf 에 현재 파일 내용 들어가있음. sread 사이즈만큼 보내줌
printf( "processing :%4.2f%% \n", total*100 / (float)filesize );
}
printf( "file translating is completed \n" );
printf( "filesize : %d, sending : %d \n", filesize, total );


close(fp); // close the file pointer
close(s); // the socket for accessing to the server. 클라이언트 소켓
return 0;
}

이게 클라이언트인데요

실행 시켜보면 80퍼센트 정도에서 서버쪽에서 반복문을 더 돌지 못하고 멈춥니다.
어떻게 해야될까요 ㅜㅜㅜㅜ

File attachments: 
첨부파일 크기
Image icon help.PNG37.24 KB
라스코니의 이미지

1) recvfrom()에서 굳이 100개 씩 제한할 필요가 있을까요? 한 1024개 정도 버퍼를 두고 받도록 하는게 보통입니다.

2) UDP 방식으로 client에서 서버로 보내도록 시키면 client에서 그냥 되는데로 최고 속도(best effort?)로 보냅니다. 서버에서 만약 놓치는 게 있으면 그냥 못 받는거죠. 그래서 보통 sendto() 뒤에 1ms 정도 delay를 둡니다.

3) wireshark로 데이터가 전부다 보내졌는지 확인보세요. 뭔가 빨간 색으로 뜨면 제대로 송수신이 안됬다는 겁니다.

댓글 달기

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