[windows c] 소켓프로그래밍-특정 폴더에 있는 모든파일 복사 질문요~~

han042의 이미지

c 초보라 너무 어려워요 도와주세요...
struct _finddata_t 를 이용해 한 폴더에 있는 파일명들을 순차적으로 클라이언트에 전송하여 같은 이름의 파일을 만든후 데이터를 전송하도록 만들었는데.

1. 클라이언트 쪽에 첫번째 파일이 잘 만들어져 재료가 전송되지만 쓰래기 값이 앞에 들어갑니다.
2. 하나의 파일이 생성된후 멈춰버립니다.

어떻게 해야할까요?? 고수님들 도와주세요....

첨부파일에 결과 스크리샷이있습니다.

서버측-----------------------------
#include stdio.h
#include stdlib.h
#include string.h
#include winsock2.h
#include io.h
#include time.h

#define BUFFER 30

typedef struct _chkFile
{ char name[260];
double size;
}chkFile;

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

void my_strcat(char* dst, const char* src)
{
while (*dst != '\0')

dst++;

do {
*dst++ = *src;
} while (*src++ != '\0');
}

int main(int argc, char **argv)
{
WSADATA wsa;
SOCKET SSock;
SOCKET CSock;
char buf[BUFFER];
char data[BUFFER];

FILE* fp;
SOCKADDR_IN sAdd;
SOCKADDR_IN cAdd;
int cAddSize;
int len;

struct _finddata_t c_file;
long hFile;

int count=0;

if(argc!=2){
printf("how to use : %s \n", argv[0]);
exit(1);
}

//initiates use of the Winsock DLL by a process
if(WSAStartup(MAKEWORD(2, 2), &wsa) !=0)
Error("WSAStartup() error!");

//creates a socket that is bound to a specific transport service provider
SSock=socket(PF_INET, SOCK_STREAM, 0);
if(SSock == INVALID_SOCKET)
Error("socket() error");

//Sets buffers to a specified character.
memset(&sAdd, 0, sizeof(sAdd));
sAdd.sin_family = AF_INET;
//converts a u_long from host to TCP/IP network byte order
sAdd.sin_addr.s_addr = htonl(INADDR_ANY);
//converts a u_short from host to TCP/IP network byte order
sAdd.sin_port = htons(atoi(argv[1]));

//associates a local address with a socket
if(bind(SSock, (SOCKADDR*)&sAdd, sizeof(sAdd)) < SOCKET_ERROR)
Error("bind() error");

//places a socket in a state in which it is listening for an incoming connection
if(listen(SSock,5)==SOCKET_ERROR)
Error("listen() error");

cAddSize=sizeof(cAdd);
//permits an incoming connection attempt on a socket
CSock=accept(SSock, (SOCKADDR*)&cAdd, &cAddSize);
if(CSock==-1)
Error("accept() error");

//----------------------------------------------------------------------------til Connection

//인사말 보내기
send(CSock,"From server : Hello!\n\0",21,0);
fputs("Client Connected\n",stdout);
printf("%d",sizeof(char));

if( (hFile = _findfirst( "*.*", &c_file )) == -1L ){
printf( "No files in Server directory!\n" );
}else{
printf( " %-12s %9ld\n", c_file.name, c_file.size );

while( _findnext( hFile, &c_file ) == 0 )
{
printf( " %-12s %9ld\n", c_file.name, c_file.size );

if(strcmp(c_file.name,"..") == 1){
send(CSock, c_file.name, sizeof(c_file.name),0);
fp = fopen(c_file.name,"r");
if(fp == NULL)
Error("File open error");

while(1){
len=fread(data,sizeof(char), BUFFER, fp);
send(CSock,data,len,0);

if(feof(fp)) break;
}

len = recv(CSock,buf,BUFFER-1,0);
buf[0]=0;
fputs(buf,stdout);

fclose(fp);
}
}
}
if(shutdown(CSock,SD_SEND) == SOCKET_ERROR)
Error("shutdown error");

//closes an existing socket
closesocket(CSock);
//terminates use of the Winsock 2 DLL (Ws2_32.dll)
WSACleanup();
return 0;
}

클라이언트----------------------------------------------
#include
#include
#include
#include
#include
#include
#include

#define BUFFER 30
#define NAMESIZE 20

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

int main(int argc, char **argv)
{
WSADATA wsa;
SOCKET hServSock;

char buf[BUFFER];
char data[BUFFER];

FILE* fp;
SOCKADDR_IN sAdd;
int len;

struct _finddata_t c_file;
long hFile;

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

//initiates use of the Winsock DLL by a process
if(WSAStartup(MAKEWORD(2, 2), &wsa) !=0)
Error("WSAStartup() error!");

//creates a socket that is bound to a specific transport service provider
hServSock=socket(PF_INET, SOCK_STREAM, 0);
if(hServSock == INVALID_SOCKET){
Error("socket() error");
printf("\a");
}

//Sets buffers to a specified character
memset(&sAdd, 0, sizeof(sAdd));
sAdd.sin_family = AF_INET;
//converts a string containing an IPv4 dotted-decimal address into a proper address for the IN_ADDR structure
sAdd.sin_addr.s_addr = inet_addr(argv[1]);
//converts a u_short from host to TCP/IP network byte order.
sAdd.sin_port = htons(atoi(argv[2]));

//establishes a connection to a specified socket
if(connect(hServSock, (SOCKADDR*)&sAdd, sizeof(sAdd))==SOCKET_ERROR)
Error("connect() error");

//---------------------------------------------------------------------------------------------------------------

//인사말 받기
len = recv(hServSock,buf,BUFFER-1,0);
buf[len]=0;
fputs(buf,stdout);

while((len = recv(hServSock,buf,BUFFER-1,0)) != 0)
{
buf[len]=0;
fputs(buf,stdout);

fp = fopen(buf,"wb");
if(fp == NULL)
Error("File open error");

while((len=recv(hServSock,data,BUFFER,0))!=0)
{
fwrite(data, sizeof(char), len, fp);
}
send(hServSock, "thanks\0", 10, 0);
fclose(fp);
}

//closes an existing socket
closesocket(hServSock);
//terminates use of the Winsock 2 DLL (Ws2_32.dll)
WSACleanup();
return 0;
}

File attachments: 
첨부파일 크기
Image icon result.JPG80.52 KB

댓글 달기

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