C 语言实现文件传输 (socket实现)

本来想实现一个简单的可以从服务器下载文件,但是需要访问IP地址,比较麻烦,然后就先实现了文件的传输,接下来慢慢实现从服务器下载文件……

在这里需要提醒一下,复制的文件要放在服务器目录下,文件将复制到客户端目录下……


服务器源代码:

#include
#include
#include
#include

#define LISTENPORT 12345

#pragma comment(lib,"Wsock32")
#pragma comment(lib,"ws2_32")

sendFile(SOCKET conSock)
{
printf("Prapare to send file\n");
char *sendBuf = new char[100];

FILE *in;
char infile[50] = "VLC.txt"; 

if((in=fopen(infile,"rb"))==NULL)
{
printf("Can't open the source file \n");
exit(0);
}

printf("File name is %s\n", infile);

// send file name to the client
send(conSock, infile, sizeof(infile), 0);

int handle = open(infile, 0x0001);
long file_len = filelength(handle);
long file_len_bak = file_len;

printf("Size of the file is %d\n", file_len);

// store the length of the file in sendBuffer
int i;
for (i = 0; file_len > 9; i++)
{
sendBuf[i] = (file_len % 10);
file_len = file_len / 10;
}
sendBuf[i] = file_len % 10;  

send(conSock, sendBuf, i + 1, 0);  

printf("Transmission started\n");

Sleep(1);

char ch;
char chack;

while (file_len_bak != 0)
{
ch = fgetc(in);
send(conSock, &ch, 1, 0);
recv(conSock, &chack, 1, 0);
file_len_bak--;
printf(".");
}

ch = EOF;
send(conSock, &ch, 1, 0);
printf("\nTransmission finished");
}

int main()
{
WSADATA   words; 
if(WSAStartup(MAKEWORD(2,2),&words)!=0)   
{
printf("Winsock   init   failed!\n");
}  

SOCKET listenSock, conSock;

sockaddr_in remoteAddr;
int remoteAddrLen, int ServerAddrLen;

listenSock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (listenSock == INVALID_SOCKET)
{
printf("ListenSocket create failed!\n");
return 0;
}

sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(LISTENPORT);
sin.sin_addr.S_un.S_addr = INADDR_ANY;

ServerAddrLen = sizeof(sin);

if (bind(listenSock, (sockaddr *)&sin, ServerAddrLen) == SOCKET_ERROR)
{
printf("Bind error!\n");
return 0;
}

if (listen(listenSock, 2) == SOCKET_ERROR)
{
printf("Can't listen!\n");
return 0;
}

remoteAddrLen = sizeof(remoteAddr);

while (TRUE)
{
conSock = accept(listenSock, (sockaddr *)& remoteAddr, &remoteAddrLen);

if (conSock == INVALID_SOCKET)
{
printf("Accept failed!\n");
continue;
}
else
{
printf("Accept a new connect : %s \r\n", inet_ntoa(remoteAddr.sin_addr));
sendFile(conSock);
}
}

closesocket(conSock);
closesocket(listenSock);

WSACleanup();
return 1;
}


客户端代码:

#include
#include
#include
#include


using namespace std;


#define SERVERPORT 12345


#pragma comment(lib, "Wsock32")
#pragma comment(lib, "ws2_32")


receiveFile(SOCKET consock)
{
printf("Prepare to receive file\n");

FILE *dest;
char destfile[50];
char recvBuff[100];

// Receive name of the file
int namelen = recv(consock, recvBuff, 100, 0);
memcpy(destfile, recvBuff, namelen);

printf("Name of the file is %s \n", destfile);

if((dest=fopen(destfile,"wb"))==NULL)
{
printf("Can't open the dest file");
exit(0);
}  

// Receive size of the file
int flag_file_len = recv(consock, recvBuff, 100, 0);

long file_len = 0;
for (int i = 0; flag_file_len != 0; i++)
{
long temp = recvBuff[i];
for (int j = 0; j != i; j++)
{
temp = temp * 10;
}
file_len = file_len + temp; 
flag_file_len--;
}

printf("Size of the file is %ld\n", file_len);
printf("Ready to receive file\n");

char ch;
char chack = 1;
int n;
while ( recv(consock, &ch, 1, 0))
{
fputc(ch, dest);
send(consock, &chack, 1, 0);
file_len--;
if (file_len == 0)
{
break;
}
printf(".");
}
printf("\nTransmission finished\n");
}


int main()
{
WSADATA   words;
if(WSAStartup(MAKEWORD(2,2),&words)!=0)   
{
printf("Winsock init failed\n");
}
SOCKET conSock;

conSock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (conSock == INVALID_SOCKET)
{
printf("Socket create failed\n");
return 0;
}

sockaddr_in servAddr;

servAddr.sin_family = AF_INET;
servAddr.sin_port = htons(SERVERPORT);
servAddr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");

if (connect(conSock, (sockaddr *)& servAddr, sizeof(servAddr)) == -1)
{
printf("Connect failed\n");
return 0;
}
else
{
printf("Connect to server succeed\n");
receiveFile(conSock);
}

closesocket(conSock);
WSACleanup();

return 1;
}


你可能感兴趣的:(C,文件传输,socket)