gpt4 book ai didi

c++ - 发送文件时套接字写入失败

转载 作者:行者123 更新时间:2023-11-28 03:02:33 24 4
gpt4 key购买 nike

我已经在这个问题上停留了一段时间,因为我无法通过套接字发送文件。我已经使用这种方法发送了其他信息,但当我尝试将 PNG 文件作为字符串发送时,问题似乎出现了。

这些是我用来发送和接收信息的方法:

// Sends a Message to the specified Socket
void Server::sendMessage(int socket, string message)
{
// Write the Message Size to the Socket
send(socket, itoa((message.length() + 1)), sizeof(size_t));

// Wait for Write Confirmation
bool response;
receive(socket, &response, 2);

// Write the Message to the Socket
send(socket, (char*) message.c_str(), message.length() + 1);

// Wait for Write Confirmation
receive(socket, &response, 2);
}

// Receives Message from the specified Socket
string Server::receiveMessage(int socket)
{
// Read the Message Size from the Socket
int size;
receive(socket, &size, sizeof(size_t));

// Send Write Confirmation
send(socket, itoa(true), 2);

// Receive the Message from the Socket
char message[size];
receive(socket, message, size);

// Send Write Confirmation
send(socket, itoa(true), 2);

// Return the Message as a String
string msg(message);
return msg;
}

sendreceive 方法分别只是writeread 的中继。我只是在这些方法中进行错误检查,是 send 方法告诉我 write 不工作。以防万一,这是我的发送方法:

// Sends a Data Packet to the specified Socket
int Server::send(int socket, void* data, int size)
{
// Write the Data to the Socket
int count = write(socket, data, size);

// Make sure the Write Succeeded
if(count == -1)
{
print("$f1Error: $f0Unable to Write to Socket $t1%i$t0\n", socket);
exit(1);
}

return count;
}

需要注意的是,Server是作为Thread运行的,因此以上三个函数都是静态的。客户端还包含相同的四个网络功能。

打破这种情况的命令行发生在一个单独的静态函数中,我用它来处理客户端。以下是所述方法的相关部分:

// Handles each Client with a Thread 
void* Server::server_handleClient(void* arg)
{
// Determine the Socket Descriptor
int socket = *((int*) arg);
free(arg);

// Create the Rover
Rover* rover = new Rover();

// Loop Indefinitely
while(true)
{
...
// Take a Picture and Send it
sendMessage(socket, rover -> takePicture());
...
}

// Delete the Rover
delete rover;

// Close the Socket
close(socket);

// Return a Successful Status
return (void*) new int(0);
}

在这里您可以看到我使用了我创建的另一个类中的方法。这是 Rover 类中的 takePicture 方法,这是我实际抓取图片的地方:

// Takes a Picture and Returns the Photo as a String
inline string Rover::takePicture()
{
// Open the Picture File
ifstream picture;
string filepath = "./Server/Pictures/" + getDirection() + ".png";
picture.open(filepath.c_str());

// Make sure the File Opened
if(!picture.is_open())
return "";

// Read the File into a String Buffer
stringstream buffer;
buffer << picture.rdbuf();

return buffer.str();
}

简而言之,服务器流动站获取图片,然后将其发送给客户端。当我检查照片字符串的内容时,它就在那里。所有可能的照片大小都是合理的(用于测试的照片是 674,962 字节,发送的缓冲区大小是预期的 674,963)

我已经使用这些方法来发送各种消息,并且一切正常。我可以发送字符串(如“Hello World!”)和整数。

我做错了什么吗?我要发送的文件是不是太大了?我缺少一些信息吗?我需要帮助...


编辑:我做了一些改变,并取得了一些进展。我对 sendMessage 命令做了一个小改动。目前的问题是图片发送不正常。

新的sendMessage函数:

// Sends a Message to the specified Socket
void Server::sendMessage(int socket, string message, bool data = false)
{
// Write the Message Size to the Socket
send(socket, itoa((message.length() + 1)), sizeof(size_t));

// Wait for Write Confirmation
bool response;
receive(socket, &response, 2);

// Determine the Type of Data to Send
if(data)
{
// Write the Message Data to the Socket
send(socket, (char*) message.data(), message.length() + 1);
}
else
{
// Write the Message to the Socket
send(socket, (char*) message.c_str(), message.length() + 1);
}

// Wait for Write Confirmation
receive(socket, &response, 2);
}

此功能的客户端拷贝也已更新以匹配。

现在我们正在努力保存 PNG 文件,下面是处理该文件的函数:

// Handles each Client with a Thread 
void* Client::client_handleServer(void* arg)
{
// Define Socket Variables
int socket = *((int*) arg);
free(arg);

...
// Export the Picture to the Client's Directory
message = receiveMessage(socket);
ofstream picture;
picture.open("./Client/Pictures/Picture.png", std::ifstream::binary);
picture << message;
picture.close();
...
}

最佳答案

当前您正在以文本模式打开文件。这意味着文件中包含换行符“\n”的任何字符都将转换为换行符 + 回车符“\r\n”。

像这样以二进制模式打开你的文件

picture.open(filepath.c_str(), std::ifstream::binary);

那么它可能会起作用。

关于c++ - 发送文件时套接字写入失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20385104/

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