UDP로 파일복사 서버&클라이언트 만들고 있는데 질문있습니다..
UDP로 파일복사 서버&클라이언트 만들고 있는데 질문있습니다..
a.txt 같은 텍스트 파일은 용량이 커도 다 읽히는데
mp3 같은 파일은 5메가라도 초반부분만 읽고 끝나버리는데 그 이유를 모르겠어요
한두번 읽으면 바로 파일 끝으로 포인터가 가버리네요 이유가 뭘까요?ㅠ
----------client------------
#include
#include
#define FILE_SVC_PORT 5491
#define BUF_SIZE 512
void sockSetAddress(struct sockaddr_in * pSockAddr, char *pstrIP, unsigned short nPort);
void sockError(int bQuit);
int main(int argc, char* argv[])
{
FILE* out; // file pointer
WSADATA wsa;
struct sockaddr_in local_addr;
SOCKET s;
// Argument processing
if(argc==1) // no optiong
{
fopen_s(&out, "ufile.dat","wb");
}
else if(argc != 3) // wrong argument
{
printf("Usage : %s -f file\n", argv[0]);
exit(1);
}
else if(strcmp(argv[1],"-f") != 0) // wrong option
{
printf("Usage : %s -f file\n", argv[0]);
exit(1);
}
else // option
{
fopen_s(&out, argv[2],"wb");
}
// Winsock Initialization
if (WSAStartup(MAKEWORD(2,2), &wsa) < 0)
sockError(1);
// Open a Socket
if ( (s = socket (AF_INET, SOCK_DGRAM, 0)) < 0 )
sockError(1);
// Set local address & BIND
sockSetAddress(&local_addr, NULL, FILE_SVC_PORT);
if ( bind(s, (SOCKADDR *)&local_addr, sizeof(local_addr)) == SOCKET_ERROR)
sockError(1);
puts("====UDP File Server====");
// Receive & echo back
{
struct sockaddr client_addr;
int len_addr = sizeof(client_addr);
int retval;
char buf[BUF_SIZE];
while (1)
{
memset(buf, 0x00, sizeof(buf));
retval = recvfrom(s, buf, BUF_SIZE, 0, &client_addr, &len_addr);
if (retval > 0)
{
//retval = sendto(s, buf, retval, 0, (SOCKADDR *) &client_addr, len_addr);
fwrite(buf, sizeof(char), retval, out);
}
else
break;
if (retval == SOCKET_ERROR)
{
sockError(0);
break;
}
}
}
// Close the opened socket
closesocket(s);
// Winsock Finalization
WSACleanup();
return 0;
}
void sockSetAddress(struct sockaddr_in * pSockAddr, char *pstrIP, unsigned short nPort)
{
if (!pSockAddr) return;
pSockAddr->sin_family = AF_INET;
pSockAddr->sin_port = htons(nPort);
if (pstrIP)
{
if (isdigit(pstrIP[0]))
{
pSockAddr->sin_addr.s_addr = inet_addr(pstrIP);
}
else
{
HOSTENT * pHostent;
if (pHostent = gethostbyname(pstrIP))
memcpy(&pSockAddr->sin_addr.s_addr, pHostent->h_addr, pHostent->h_length);
else
pSockAddr = NULL;
}
}
else
{
pSockAddr->sin_addr.s_addr = htonl(INADDR_ANY);
}
}
void sockError(int bQuit)
{
LPVOID lpMsgBuf;
FormatMessage (
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, WSAGetLastError( ),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR)&lpMsgBuf, 0, NULL ) ;
MessageBox ( NULL, (LPCTSTR)lpMsgBuf, "Error",
MB_OK | MB_ICONINFORMATION ) ;
LocalFree ( lpMsgBuf );
if (bQuit) exit(1);
}
----------server------------
#include
#include
#define FILE_SVC_PORT 5491
#define BUF_SIZE 512
void sockSetAddress(struct sockaddr_in * pSockAddr, char *pstrIP, unsigned short nPort);
void sockError(int bQuit);
int main(int argc, char* argv[])
{
FILE* out; // file pointer
WSADATA wsa;
struct sockaddr_in local_addr;
SOCKET s;
// Argument processing
if(argc==1) // no optiong
{
fopen_s(&out, "ufile.dat","wb");
}
else if(argc != 3) // wrong argument
{
printf("Usage : %s -f file\n", argv[0]);
exit(1);
}
else if(strcmp(argv[1],"-f") != 0) // wrong option
{
printf("Usage : %s -f file\n", argv[0]);
exit(1);
}
else // option
{
fopen_s(&out, argv[2],"wb");
}
// Winsock Initialization
if (WSAStartup(MAKEWORD(2,2), &wsa) < 0)
sockError(1);
// Open a Socket
if ( (s = socket (AF_INET, SOCK_DGRAM, 0)) < 0 )
sockError(1);
// Set local address & BIND
sockSetAddress(&local_addr, NULL, FILE_SVC_PORT);
if ( bind(s, (SOCKADDR *)&local_addr, sizeof(local_addr)) == SOCKET_ERROR)
sockError(1);
puts("====UDP File Server====");
// Receive & echo back
{
struct sockaddr client_addr;
int len_addr = sizeof(client_addr);
int retval;
char buf[BUF_SIZE];
while (1)
{
memset(buf, 0x00, sizeof(buf));
retval = recvfrom(s, buf, BUF_SIZE, 0, &client_addr, &len_addr);
if (retval > 0)
{
//retval = sendto(s, buf, retval, 0, (SOCKADDR *) &client_addr, len_addr);
fwrite(buf, sizeof(char), retval, out);
}
else
break;
if (retval == SOCKET_ERROR)
{
sockError(0);
break;
}
}
}
// Close the opened socket
closesocket(s);
// Winsock Finalization
WSACleanup();
return 0;
}
void sockSetAddress(struct sockaddr_in * pSockAddr, char *pstrIP, unsigned short nPort)
{
if (!pSockAddr) return;
pSockAddr->sin_family = AF_INET;
pSockAddr->sin_port = htons(nPort);
if (pstrIP)
{
if (isdigit(pstrIP[0]))
{
pSockAddr->sin_addr.s_addr = inet_addr(pstrIP);
}
else
{
HOSTENT * pHostent;
if (pHostent = gethostbyname(pstrIP))
memcpy(&pSockAddr->sin_addr.s_addr, pHostent->h_addr, pHostent->h_length);
else
pSockAddr = NULL;
}
}
else
{
pSockAddr->sin_addr.s_addr = htonl(INADDR_ANY);
}
}
void sockError(int bQuit)
{
LPVOID lpMsgBuf;
FormatMessage (
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, WSAGetLastError( ),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR)&lpMsgBuf, 0, NULL ) ;
MessageBox ( NULL, (LPCTSTR)lpMsgBuf, "Error",
MB_OK | MB_ICONINFORMATION ) ;
LocalFree ( lpMsgBuf );
if (bQuit) exit(1);
}
댓글 달기