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

}


이건 받는쪽