#include tcharh#include stdioh#include winsock2h#include stdlibh #include stringh #include Ws2tcpiph#include iostream#include fstream#pragma commentlibws2_32lib#define BUFFER_SIZE 2048#define FILE_N
#include <tchar.h>
#include <stdio.h>
#include <winsock2.h>
#include <stdlib.h>
#include <string.h>
#include <Ws2tcpip.h>
#include
#define BUFFER_SIZE 2048 #define FILE_NAME_MAX_SIZE 512
using namespace std; string home = "client_home/";
bool sendAll(SOCKET socket, const char* buffer, int length) { int totalSent = 0; int bytesLeft = length; int bytesSent = 0;
while (totalSent < length)
{
bytesSent = send(socket, buffer + totalSent, bytesLeft, 0);
if (bytesSent == SOCKET_ERROR)
{
return false;
}
totalSent += bytesSent;
bytesLeft -= bytesSent;
}
return true;
}
bool receiveAll(SOCKET socket, char* buffer, int length) { int totalReceived = 0; int bytesLeft = length; int bytesRead = 0;
while (totalReceived < length)
{
bytesRead = recv(socket, buffer + totalReceived, bytesLeft, 0);
if (bytesRead == SOCKET_ERROR || bytesRead == 0)
{
return false;
}
totalReceived += bytesRead;
bytesLeft -= bytesRead;
}
return true;
}
int main(int argc, char* argv[]) { // Initialize WSA, allowing the program to make Windows socket calls WORD sockVersion = MAKEWORD(2, 2); WSADATA wsaData; if (WSAStartup(sockVersion, &wsaData) != 0) { return 0; }
// Create client_socket and check if it is created successfully
SOCKET client_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); ;
if (client_socket == INVALID_SOCKET) {
// If the created socket is invalid, exit the program
perror("socket error !");
return 0;
}
// Create the address structure server_addr and set the port and IP
sockaddr_in server_addr;
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
// Port number of the server to connect to: 8888
server_addr.sin_port = htons(8888);
// Specify the address of the server: 127.0.0.1
PCWSTR src = TEXT("127.0.0.1");
InetPton(AF_INET, src, &server_addr.sin_addr.s_addr);
// Connect the client_socket to the address server_addr
if (connect(client_socket, (SOCKADDR*)&server_addr, sizeof(SOCKADDR)))
{
perror("connect error !\n");
return 0;
}
cout << "Please enter the command to execute (1 for upload file, 2 for download file, 3 for view file list):" << endl;
string choice;
cin >> choice;
// Send the data in buffer to the server
if (sendAll(client_socket, choice.c_str(), BUFFER_SIZE) == false)
{
perror("Error message: Failed to send command!");
exit(1);
}
// Upload file
if (choice == "1")
{
// Enter the file to upload
memset(buffer, 0, BUFFER_SIZE);
cout << "Please enter the name of the file to upload, e.g. test.txt:" << endl;
string filename;
cin >> filename;
sendAll(client_socket, filename.c_str(), filename.length());
// Open the file and read the file data
string f = home + filename;
FILE* fp;
errno_t F_ERR = fopen_s(&fp, f.c_str(), "rb");
string stateNum;
if (F_ERR != 0)
{
cout << "Error message: File not found!" << endl;
stateNum = "1";
sendAll(client_socket, stateNum.c_str(), stateNum.length());
exit(1);
}
stateNum = "2";
sendAll(client_socket, stateNum.c_str(), stateNum.length());
// Confirm the server's status and determine if the file transfer can begin
char flag[2];
if (receiveAll(client_socket, flag, 2) == false)
{
cout << "Error message: Failed to receive client status from server!" << endl;
exit(2);
}
cout << "Client starts uploading file!" << endl;
memset(buffer, 0, BUFFER_SIZE);
int length = 0;
// Read a segment of data and send it to the server, loop until the file is read completely
while ((length = fread(buffer, sizeof(char), BUFFER_SIZE, fp)) > 0)
{
if (sendAll(client_socket, buffer, length) == false)
{
cout << "Error message: File upload failed!" << endl;
break;
}
memset(buffer, 0, BUFFER_SIZE);
}
// Close the file
fclose(fp);
cout << "File uploaded successfully!" << endl;
}
// Download file
else if (choice == "2")
{
// Enter the file to download
memset(buffer, 0, BUFFER_SIZE);
cout << "Please enter the name of the file to download, e.g. test.txt:" << endl;
cin >> buffer;
char file_name[FILE_NAME_MAX_SIZE + 1];
strncpy_s(file_name, buffer, strlen(buffer) > FILE_NAME_MAX_SIZE ? FILE_NAME_MAX_SIZE : strlen(buffer));
if (sendAll(client_socket, buffer, BUFFER_SIZE) == false)
{
perror("Error message: Failed to send file name!");
exit(1);
}
// Receive the server's status: whether the file to download is found or not
char stateNum[2];
memset(stateNum, 0, 2);
if (receiveAll(client_socket, stateNum, 2) == false)
{
cout << "Error message: Failed to receive server status!" << endl;
}
if (!strcmp(stateNum, "1"))
{
cout << "Error message: File name not found on server!" << endl;
exit(2);
}
// Prepare to write to file
string f = file_name;
f = home + f;
FILE* fp;
errno_t F_ERR = fopen_s(&fp, f.c_str(), "wb");
if (F_ERR != 0)
{
cout << "Failed to open file!" << endl;
exit(1);
}
// Receive data from the server into buffer
// Write each segment of data received to the file, loop until the file is received and written completely
memset(buffer, 0, BUFFER_SIZE);
cout << "Downloading file..." << endl;
int length = 0;
while ((length = recv(client_socket, buffer, BUFFER_SIZE, 0)) > 0)
{
if (fwrite(buffer, sizeof(char), length, fp) < length)
{
cout << "Failed to write file!" << endl;
break;
}
memset(buffer, 0, BUFFER_SIZE);
}
// Close the file after successful reception
cout << file_name << " downloaded successfully!" << endl;
fclose(fp);
}
else if (choice == "3")
{
cout << "Server file list:" << endl;
char buffer[BUFFER_SIZE];
memset(buffer, 0, BUFFER_SIZE);
while (recv(client_socket, buffer, BUFFER_SIZE, 0) > 0)
{
cout << buffer << " ";
memset(buffer, 0, BUFFER_SIZE);
}
}
// Close the socket and WSA
closesocket(client_socket);
WSACleanup();
system("pause");
return 0;
原文地址: https://www.cveoy.top/t/topic/hWXL 著作权归作者所有。请勿转载和采集!