[windows c] 소켓프로그래밍-특정 폴더에 있는 모든파일 복사 질문요~~
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;
}
| 첨부 | 파일 크기 |
|---|---|
| 80.52 KB |


댓글 달기