gpt4 book ai didi

客户端不创建文件

转载 作者:可可西里 更新时间:2023-11-01 10:43:09 24 4
gpt4 key购买 nike

所以,我有一个工作套接字。我基本上是在制作一个套接字,以允许我将文件从一个程序传输到另一个程序。除客户端外一切正常,它不会创建文件吗?

服务器:

int main() {
WSADATA Winsock;
SOCKET Socket, Sub;
Addr addr;
IncomingAddress incomingAddress;
int AddressLen = sizeof(IncomingAddress);

// Start up Winsock
WSAStartup(MAKEWORD(2, 2), &Winsock);

if (LOBYTE(Winsock.wVersion) != 2 || HIBYTE(Winsock.wVersion) != 2) {
WSACleanup();
return 0;
}

Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

// Memset alternative
ZeroMemory(&addr, sizeof(Addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(portNumber);
bind(Socket, (sockaddr*) &addr, sizeof(Addr));

if (listen(Socket, 1) == SOCKET_ERROR) {
printf("Listening error!\n");
} else {
printf("Listening...\n");
}

if (Sub = accept(Socket, (sockaddr*) &incomingAddress, &AddressLen)) {
char *ClientIP = inet_ntoa(incomingAddress.sin_addr);
int ClientPort = ntohs(incomingAddress.sin_port);
printf("Client connected!\n");
printf("IP: %s:%d\n", ClientIP, ClientPort);

printf("Sending file... \n");

FILE *File;
char *Buffer;
unsigned long Size;

File = fopen("test.txt", "rb+");
if (!File) {
printf("Error while reading the file!\n");
getchar();
return 0;
}

fseek(File, 0, SEEK_END);
Size = ftell(File);
fseek(File, 0, SEEK_SET);

Buffer = malloc(Size);

fread(Buffer, Size, 1, File);
char cSize[MAX_PATH];
sprintf(cSize, "%i", Size);

fclose(File);
send(Sub, cSize, MAX_PATH, 0); // File size
}
return 0;
}

客户:

int main() {
WSADATA Winsock;
SOCKET Socket;
Addr addr;
IncomingAddress incomingAddress;
int AddressLen = sizeof(IncomingAddress);

WSAStartup(MAKEWORD(2, 2), &Winsock);

if (LOBYTE(Winsock.wVersion) != 2 || HIBYTE(Winsock.wVersion) != 2) {
WSACleanup();
return 0;
}

Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

ZeroMemory(&addr, sizeof(Addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr("127.0.0.1");
addr.sin_port = htons(6000);

if (connect(Socket, (sockaddr*) &addr, sizeof(Addr)) < 0) {
printf("Connection Failed!\n");
getchar();
return 0;
}

printf("Connection successful! \n");
printf("Receiving file... \n");

int Size;
char *Filesize = malloc(1024);

if (recv(Socket, Filesize, 1024, 0)) { // File Size
Size = atoi((const char*) Filesize);
printf("File size: %d\n", Size);
}

char *Buffer = malloc(Size);

int Offset = 0;
while (Size > Offset) {
int Amount = recv(Socket, Buffer + Offset, Size - Offset, 0);

if (Amount <= 0) {
printf("Error: " + WSAGetLastError());
break;
} else {
Offset += Amount;
printf("2\n");
}
}

FILE *File;
File = fopen("test.txt", "wb+");
fwrite(Buffer, 1, Size, File);
fclose(File);

getchar();
closesocket(Socket);
WSACleanup();
return 0;
}

最佳答案

您只是将文件的长度发送给客户端,而不是文件本身:

    send(Sub, cSize, MAX_PATH, 0); // File size

这永远不会收到任何东西:

    int Amount = recv(Socket, Buffer + Offset, Size - Offset, 0);

是的,只需发送实际文件即可。

关于客户端不创建文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40170912/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com