Winsock 将文件上传到远程服务器
Winsock upload file to remote server
我正在尝试将文件上传到服务器,但它们总是损坏,该程序非常简单,仅用于在客户端和服务器之间传输文件。我需要此代码中的帮助来解决问题。并改进它。
服务器
int Socket_Manip::FILE_UPLOAD() {
//get file size.
iResult = recv(ClientSocket, GotFileSize, LENGTH, 0);
if (iResult == 0)
{
closesocket(ClientSocket);
WSACleanup();
return 1;
}
else if (iResult < 0) {
closesocket(ClientSocket);
WSACleanup();
return 1;
}
//start manip download
long FileSize = atoi(GotFileSize);
long SizeCheck = 0;
char* mfcc;
FILE *fp = fopen("C::\Users\Server\Downloads\transfer.zip", "wb");
if (fp == NULL)
{
closesocket(ClientSocket);
WSACleanup();
return 1;
}
if (FileSize > 1499) {
mfcc = (char*)malloc(1500);
while (FileSize > SizeCheck){
int Received = recv(ClientSocket, mfcc, 1500, 0);
if (Received == 0)
{
break;
}
else if (Received < 0) {
closesocket(ClientSocket);
WSACleanup();
return 1;
}
SizeCheck += Received;
fwrite(mfcc, 1, Received, fp);
fflush(fp);
}
} else
{
mfcc = (char*)malloc(FileSize + 1);
int Received = recv(ClientSocket, mfcc, FileSize, 0);
fwrite(mfcc, 1, Received, fp);
fflush(fp);
}
fclose(fp);
free(mfcc);
}
客户端发送方
int File_Transfer_Manip() {
FILE *File;
char *Buffer;
unsigned long Size;
File = fopen("C:\Users\r4minux\Downloads\upload.zip", "rb");
if (!File)
{
printf("Error filen");
return 1;
}
fseek(File, 0, SEEK_END);
Size = ftell(File);
fseek(File, 0, SEEK_SET);
Buffer = new char[Size];
fread(Buffer, Size, 1, File);
char cSize[MAX_PATH];
sprintf(cSize, "%i", Size);
fclose(File);
iResult = send(ConnectSocket, cSize, MAX_PATH, 0); // File size
if (iResult == SOCKET_ERROR) {
printf("send erroR: %dn", WSAGetLastError());
closesocket(ConnectSocket);
WSACleanup();
getchar();
return 1;
}
int Offset = 0;
while (Offset < Size)
{
int Amount = send(ConnectSocket, Buffer + Offset, Size - Offset, 0);
if (Amount <= 0)
{
std::cout << "Error: " << WSAGetLastError() << std::endl;
break;
}
else
{
Offset += Amount;
}
}
// cleanup
free(Buffer);
}
TCP 是一个字节流。 它没有消息边界的概念。 你没有确定send()
实际上正在发送你给它的所有内容,或者recv()
正在阅读你要求它的所有内容。 它们可以返回更少的字节。 你必须考虑到这一点。
试试这个:
服务器:
int readBytes(SOCKET s, void *buffer, int buflen)
{
int total = 0;
char *pbuf = (char*) buffer;
while (buflen > 0)
{
int iResult = recv(s, pbuf, buflen, 0);
if (iResult < 0)
{
if (WSAGetLastError() == WSAEWOULDBLOCK)
{
// optionally use select() to wait for the
// socket to have more bytes to read before
// calling recv() again...
continue;
}
printf("recv error: %dn", WSAGetLastError());
return SOCKET_ERROR;
}
else if (iResult == 0)
{
printf("disconnectedn");
return 0;
}
else
{
pbuf += iResult;
buflen -= iResult;
total += iResult;
}
}
return total;
}
int Socket_Manip::FILE_UPLOAD()
{
//start download
FILE *fp = fopen("C::\Users\Server\Downloads\transfer.zip", "wb");
if (fp == NULL)
{
printf("Error creating filen");
closesocket(ClientSocket);
WSACleanup();
return 1;
}
//get file size.
unsigned long FileSize;
int iResult = readBytes(ClientSocket, &FileSize, sizeof(FileSize));
if (iResult <= 0)
{
fclose(fp);
closesocket(ClientSocket);
WSACleanup();
return 1;
}
FileSize = ntohl(FileSize);
char mfcc[1024];
while (FileSize > 0)
{
int Received = readBytes(ClientSocket, mfcc, min(sizeof(mfcc), FileSize));
if (Received <= 0)
{
fclose(fp);
closesocket(ClientSocket);
WSACleanup();
return 1;
}
if (fwrite(mfcc, 1, Received, fp) != Received)
{
printf("Error writing filen");
fclose(fp);
closesocket(ClientSocket);
WSACleanup();
return 1;
}
FileSize -= Received;
}
fflush(fp);
fclose(fp);
return 0;
}
客户
int sendBytes(SOCKET s, void *buffer, int buflen)
{
int total = 0;
char *pbuf = (char*) buffer;
while (buflen > 0)
{
int iResult = send(s, pbuf, buflen, 0);
if (iResult < 0)
{
if (WSAGetLastError() == WSAEWOULDBLOCK)
{
// optionally use select() to wait for the
// socket to have more space to write before
// calling send() again...
continue;
}
printf("send error: %dn", WSAGetLastError());
return SOCKET_ERROR;
}
else if (iResult == 0)
{
printf("disconnectedn");
return 0;
}
else
{
pbuf += iResult;
buflen -= iResult;
total += iResult;
}
}
return total;
}
int File_Transfer_Manip()
{
char Buffer[1024];
FILE *fp = fopen("C:\Users\r4minux\Downloads\upload.zip", "rb");
if (!fp)
{
printf("Error opening filen");
return 1;
}
fseek(fp, 0, SEEK_END);
unsigned long FileSize = ftell(fp);
fseek(fp, 0, SEEK_SET);
unsigned long tmpFileSize = htonl(FileSize);
int iResult = sendBytes(ConnectSocket, &tmpFileSize, sizeof(tmpFileSize));
if (iResult <= 0)
{
fclose(fp);
closesocket(ConnectSocket);
WSACleanup();
getchar();
return 1;
}
while (FileSize > 0)
{
long Size = fread(Buffer, 1, min(sizeof(Buffer), FileSize), fp);
if (Size <= 0)
{
printf("Error reading filen");
fclose(fp);
closesocket(ConnectSocket);
WSACleanup();
getchar();
return 1;
}
if (sendBytes(ConnectSocket, Buffer, Size) != Size)
{
fclose(fp);
closesocket(ConnectSocket);
WSACleanup();
getchar();
return 1;
}
FileSize -= Size;
}
fclose(fp);
return 0;
}
相关文章:
- 如何通过套接字将文本文件的内容从服务器发送到客户端
- 如何从远程 SFTP 服务器获取 HH-MM-SS 时间戳格式的文件列表
- 使用 libssh 从 SFTP 服务器下载文件并使用 C++ 将其写入 ofstream?
- 使用 Libssh 将文件从服务器复制到客户端:分配文件复制目标路径时出现问题
- ofstream将额外的零字节写入Unix服务器上的文件
- 将文件发送到 HTTP 服务器 (C++ Wininet)
- COM 客户端连接 COM 服务器所需的文件是什么(进程外方案)
- 可视化 将 TCP 服务器数据包保存到 C++ 中的文件
- C++应用程序连接到网站服务器并根据密码检索文件
- 为什么我的 Redhat 服务器上的 QuickFIX 进程没有将其核心文件写入应有的位置?
- 通过TCP在客户端/ C#服务器之间传输文件C++
- 从远程服务器请求PDB文件
- 将文件从计算机上传到在线服务器上
- 连接到 FTP 服务器并访问文件
- 在通过套接字向服务器发送文件数据时,我得到了一些垃圾值?为什么
- 为什么文件没有通过 tcp 在 C# 中的客户端和 C 中的服务器之间正确传输(保存?)
- 为什么 tinyxml2 无法解析流量服务器 xml 文件
- 如何在不保存文件的情况下制作打印屏幕并将其发送到FTP服务器?我的工作代码将文件保存到HDD
- 使用SFTP和Pageant认证更新服务器文件夹
- FTP服务器文件传输