while(1){
printf("FTP MODE\n");
if (send(sock, echoString, echoStringLen, 0) != echoStringLen)
DieWithError("send() sent a different number of bytes than expected");
if ((bytesRcvd = recv(sock, echoBuffer, RCVBUFSIZE - 1, 0)) <= 0)
DieWithError("recv() failed or connection closed prematurely");
echoBuffer[bytesRcvd] = '\0'; /* Terminate the string! */
printf(echoBuffer); /* Print the echo buffer */
printf("\n");
printf("name.ttt->:");
scanf("%s",echoString);
if(!strcmp(echoString,"/quit")){
printf("terminate\n");
close(sock);
exit(1);
}
echoStringLen = strlen(echoString); /* Determine input length */
FTP = fopen(echoString,"rb");
//file name send
if (send(sock, echoString, echoStringLen, 0) != echoStringLen)
DieWithError("send() sent a different number of bytes than expected");
//size req recv
if ((bytesRcvd = recv(sock, echoBuffer, RCVBUFSIZE - 1, 0)) <= 0)
DieWithError("recv() failed or connection closed prematurely");
fseek(FTP, 0, SEEK_END);
filesize = ftell(FTP);
fseek(FTP, 0, SEEK_SET);
sprintf(echoString,"%d",filesize);
//size send
if (send(sock, echoString, echoStringLen, 0) != echoStringLen)
DieWithError("send( size ) error ");
//file transport
printf("sending =>");
while(!feof(FTP)){
if((bytesRcvd = recv(sock,echoBuffer,RCVBUFSIZE - 1,0)) <=0)
DieWithError("recv() failed");
echoBuffer[bytesRcvd] = '\0';
FTPBUF[i++] = fgetc(FTP);
if(ftell(FTP) >= filesize/100*percent){
printf("#");
percent +=10;
}
if(i==256){
if(send(sock, FTPBUF,256,0) != 256)
DieWithError("send ( size ) errof ");
i=0;
}
}
for(j=0;j<i;j++){
FTPBUF[j] = fgetc(FTP);
}
FTPBUF[j] = '\0';
if(send(sock,FTPBUF,i,0)!=i)
DieWithError("recv() failed");
}
이게 클라에서 서버로 보내는 내용임
while(1){
//file name.ttt req
msgBox = "filename to put to server->";
if(send(clntSocket, msgBox,strlen(msgBox),0) != strlen(msgBox))
DieWithError("send() failed");
printf("send request filename\n");
//file name recv
if((recvMsgSize = recv(clntSocket,FTPBuffer,FTPBUFSIZE, 0))<0)
DieWithError("recv() failed");
FTPBuffer[recvMsgSize] ='\0';
//file make
strcat(filename,FTPBuffer);
FTP = fopen (filename,"ab");
printf("fmake success\n");
//size req send
if(send(clntSocket, "size",5,0) != 5)
DieWithError("send() failed");
printf("send request size\n");
//size recv
if((recvMsgSize = recv(clntSocket,FTPBuffer,FTPBUFSIZE, 0))<0)
DieWithError("recv() failed");
FTPBuffer[recvMsgSize] ='\0';
filesize = atoi(FTPBuffer);
printf("size = %d\n",filesize);
//file transport
printf("sending =>");
while(1){
msgBox = "ACK";
if(send(clntSocket, msgBox,strlen(msgBox),0) != strlen(msgBox))
DieWithError("send() failed");
FTPStatus = 1;
if ((recvMsgSize = recv(clntSocket, FTPBuffer, FTPBUFSIZE, 0)) < 0)
DieWithError("recv() failed");
FTPBuffer[recvMsgSize] = '\0';
for(i=0;i<recvMsgSize;i++){
fputc(FTPBuffer[i++],FTP);
if(ftell(FTP) >= filesize/100*percent){
printf("#");
percent += 10;
}
}
if(ftell(FTP) == filesize)
break;
}
//msgBox = "ACK";
//if(send(clntSocket, msgBox,strlen(msgBox),0) != strlen(msgBox))
//DieWithError("send() failed");
printf("\n");
sprintf(msgBox,"%s(%d bytes) uploading success to ",filename,filesize);
if(send(clntSocket, msgBox,strlen(msgBox),0) != strlen(msgBox))
DieWithError("send() failed");
}
FTPStatus = 0;// ftp quit.
}
// FTP end
}
이건 받는쪽
둘다 send 메세지 => 에서 딱 멈춤
잘 봤습니다
아니 알려달라니까 뭘 잘봤대 형
recvMsgSize 를 서로 이미 약속하거나
보내드렸습니다
최초에 교환할때 정해야지
recvMsgSize = recv 로 몇개가 들어올지 혼돈의 카오스라
부족하면 반복문 돌면서 더 대기하고
그런 부분필요
근데 가령 파일이 997 바이트라면
TCP가 데이터의 끝을 따로 안찍어줌
256으로 4번째엔 글자가 256개가 아니게 되는데
메시지에 구조를 만들어서 얼마읽으라고 힌트를 줘야함
글로써줌
recv 3번째 인자에 size 만큼 읽는거아님?
일단 글로써주신다니 한번 읽어봐야지
size 만큼 읽는걸 보장하지않음