Implementing a Simple FTP‑Like File Transfer Using TCP Sockets in C
This article demonstrates how to create a basic FTP‑like file transfer application in C using Windows Winsock, covering both server and client implementations, handling file size negotiation, data transmission loops, and compilation steps, with complete source code examples.
The author encountered a problem transferring a large file via WeChat and decided to write a custom file‑transfer program using TCP socket programming. The article explains the overall approach, uses the loopback address for testing, and provides full source code for both the server and client.
Server implementation : The server creates a TCP socket, binds to 127.0.0.1:8888, listens for a connection, receives the file size from the client, then reads the incoming data in a loop and writes it to server.rar . After the transfer completes it reports success or error, closes the socket and cleans up Winsock.
#include
#include
#pragma comment(lib, "ws2_32.lib")
int main()
{
// tcp连接部分
WORD wVersionRequested;
WSADATA wsaData;
wVersionRequested = MAKEWORD(1, 1);
WSAStartup( wVersionRequested, &wsaData );
unsigned int sockSrv = socket(AF_INET, SOCK_STREAM, 0);
SOCKADDR_IN addrSrv;
addrSrv.sin_family = AF_INET;
addrSrv.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
addrSrv.sin_port = htons(8888);
bind(sockSrv,(SOCKADDR*)&addrSrv, sizeof(SOCKADDR));
listen(sockSrv, 5);
SOCKADDR_IN addrClient;
int len = sizeof(SOCKADDR);
unsigned int sockConn = accept(sockSrv,(SOCKADDR*)&addrClient, &len);
// 接收ftp client发送过来的文件长度
char sizeFileStr[20] = {0};
recv(sockConn, sizeFileStr, sizeof(sizeFileStr) - 1, 0);
int fileSize = atoi(sizeFileStr);
// 接收ftp client发送的文件并保存
char recvBuf[1024] = {0};
int recvTotalSize = 0;
FILE *fp = fopen("server.rar", "wb");
while(recvTotalSize < fileSize)
{
int recvSize = recv(sockConn, recvBuf, sizeof(recvBuf), 0);
recvTotalSize += recvSize;
printf("received %f MB\n", recvTotalSize/(1024.0 * 1024.0));
fwrite(recvBuf, 1, recvSize, fp);
}
fclose(fp);
if(recvTotalSize == fileSize)
{
printf("Done!");
}
else
{
printf("Error!");
}
closesocket(sockSrv);
WSACleanup();
getchar();
return 0;
}Client implementation : The client also creates a TCP socket, connects to the same address, determines the size of the local file ( MVC++6.0.rar ), sends the size to the server, then reads the file in 1 KB chunks and transmits each chunk, reporting progress.
#include
#include
#pragma comment(lib, "ws2_32.lib")
int main()
{
// tcp连接部分
WORD wVersionRequested;
WSADATA wsaData;
wVersionRequested = MAKEWORD(1, 1);
WSAStartup( wVersionRequested, &wsaData );
SOCKET sockClient = socket(AF_INET, SOCK_STREAM, 0);
SOCKADDR_IN addrSrv;
addrSrv.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
addrSrv.sin_family = AF_INET;
addrSrv.sin_port = htons(8888);
connect(sockClient, (SOCKADDR*)&addrSrv, sizeof(SOCKADDR));
// ftp client读取文件长度
FILE *fp = fopen("MVC++6.0.rar", "rb");
fseek(fp, 0, SEEK_END);
int totalSize = ftell(fp);
fclose(fp);
// ftp client发送文件长度
char fileSizeStr[20] = {0};
itoa(totalSize, fileSizeStr, 10);
send(sockClient, fileSizeStr, strlen(fileSizeStr) + 1, 0);
// ftp client发送文件
fp = fopen("MVC++6.0.rar", "rb");
int readSize = 0;
int sendTotalSize = 0;
char sendBuf[1024] = {0};
while((readSize = fread(sendBuf, 1, sizeof(sendBuf), fp)) > 0)
{
send(sockClient, sendBuf, readSize, 0);
sendTotalSize += readSize;
printf("sent %f MB\n", sendTotalSize/(1024.0 * 1024.0));
}
fclose(fp);
if(sendTotalSize == totalSize)
{
printf("Done!");
}
else
{
printf("Error!");
}
closesocket(sockClient);
WSACleanup();
getchar();
return 0;
}To run the program, compile both source files with a Windows C compiler, start the server executable first, then launch the client executable with the file to be transferred placed in the appropriate directory. The transfer progress is printed on both sides, and a final screenshot shows successful file receipt.
The author encourages readers to experiment with the code to deepen their understanding of network programming and problem‑solving.
IT Services Circle
Delivering cutting-edge internet insights and practical learning resources. We're a passionate and principled IT media platform.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.