gpt4 book ai didi

c++ - 正在更改的 SFML TCP 数据包

转载 作者:行者123 更新时间:2023-11-28 07:58:00 25 4
gpt4 key购买 nike

在未能在 SFML 论坛上获得任何帮助后,我决定看看这里是否有人可以提供任何帮助。我有一个应用程序,它通过与服务器应用程序的 TCP 连接进行 self 更新。这工作得很好,除了一个特定的数据包传输,其中数据包以某种方式改变了。服务器发送一个包含数字 1 的数据包:

pack << sf::Uint8(1);
ClientSocket.Send(pack);

(其中 pack 是 sf::Packet 类型,ClientSocket 是 sf::SocketTCP 类型)

单步执行我的调试器实际上确认这些行已执行,并且我的客户端中的下一个 Receive 调用是接下来的几行:

sock.Receive(pack);
sf::Uint8 conf;
pack >> conf;

(其中 pack 又是一个 sf::Packet,而 sock 是一个 SocketTCP)

但是,不知何故,在我的客户端中,conf 显示为零值(我的调试器确认了这一点)并且它也是如此(下一行根据值切换)。我在同一台机器上运行这两个应用程序,它们都将彼此读取为“127.0.0.1”,因此数据包(如果我理解正确的话)不会通过我的路由器,或者真的不会离开我的机器。关于为什么我的数据包被损坏有什么想法吗?

郑重声明,如果有任何帮助,我正在使用 SFML 1.6 和我的应用程序在 Debian Squeeze Linux 机器上运行。

编辑:根据请求,这里是我的应用程序中的更多代码。

首先,我的客户中与此更新程序具体相关的部分:

cout << "Connecting to update server." << endl;
sf::IpAddress server = sf::IpAddress::LocalHost;
sf::TcpSocket sock;
if (sock.connect(server, PORT, sf::seconds(20.0f)) != sf::Socket::Done)
return false;

cout << "Connected. Searching for updates for updater." << endl;

sf::Packet pack;
std::string lastUpdate = getLastUpdate();

#ifdef __WXGTK__
pack << "uupdate" << lastUpdate << sf::Uint8(1);
#elif defined(__WXMSW__)
pack << "uupdate" << lastUpdate << sf::Uint8(2);
#elif defined(__WXOSX__)
pack << "uupdate" << lastUpdate << sf::Uint8(3);
#endif

sock.send(pack);

pack.clear();

sock.receive(pack); //THIS IS THE PART WHERE IT BREAKS
sf::Int32 conf;
pack >> conf;

if (conf == 1) { //There is an update!
cout << "Update found!" << endl;

pack << "begin" << sf::Uint32(512);
sock.send(pack);

pack.clear();

sock.receive(pack);
pack >> conf;

if (conf == 0) { //No errors
wxFileOutputStream* out = new wxFileOutputStream(wxString(UPDATER, StrConv).append(".temp"));

char* data = new char[512];
while (data != chara("done") && data != chara("error")) {
sock.receive(pack);
sf::Int32 size;
pack >> size;
data = const_cast<char*>((const char*)pack.getData());

out->Write(data, size);
}
out->Close();
if (data == chara("done"))
wxCopyFile(wxString(UPDATER, StrConv).append(".temp"), wxString(UPDATER, StrConv));
wxRemoveFile(wxString(UPDATER, StrConv).append(".temp"));
}
}

cout << "Updater is up-to-date. Executing updater." << endl;

sock.disconnect();

bool success;
if (wxTheApp->argc == 1)
success = wxExecute(wxT(UPDATER));
else
success = wxExecute(wxString(UPDATER, StrConv).append(wxTheApp->argv[1]));

return success;

然后是与这些更新相关的服务器部分:

    //ClientSocket has already been initalised by way of a TcpListener
cout << "Checking for updater patches for client at " << ClientAddress.toString() << endl;

std::string lastUpdate;
pack >> lastUpdate;
sf::Uint8 os;
pack >> os;

//Check last time updater was changed
struct tm* clock;
struct stat attribs;
if (os == 1)
stat("Linux/Updater", &attribs);
else if (os == 2)
stat("Windows/Updater.exe", &attribs);
else if (os == 3)
stat("Mac/Updater", &attribs);
clock = gmtime(&(attribs.st_mtime));
time_t lastWrite = mktime(clock);

time_t clientUpdate = stringToNumber<time_t>(lastUpdate.c_str());

if (lastWrite > clientUpdate) {
cout << "Found updater patches for client at " << ClientAddress.toString() << endl;

pack << sf::Int32(1);
ClientSocket->send(pack); //THIS IS THE PART WHERE IT BREAKS

pack.clear();

ClientSocket->receive(pack);
std::string mes;
pack >> mes;

if (mes != "begin") {
cerr << "Client at " << ClientAddress.toString() << " sent unrecognised message: " << mes << ". Ending conversation." << endl;
ClientSocket->disconnect();
return false;
}

sf::Uint32 size;
pack >> size;

cout << "Beginning to send updater patch to " << ClientAddress.toString() << endl;

ifstream in;

if (os == 1)
in.open(chara("Linux/Updater"), ios_base::in | ios_base::binary);
else if (os == 2)
in.open(chara("Windows/Updater.exe"), ios_base::in | ios_base::binary);
else if (os == 3)
in.open(chara("Mac/Updater"), ios_base::in | ios_base::binary);

if (in.fail()) {
pack << sf::Uint8(1);
ClientSocket->send(pack);
cerr << "Failed to open updater at request of " << ClientAddress.toString() << ". Closing connection." << endl;
ClientSocket->disconnect();
return false;
}

pack << sf::Uint8(0);
ClientSocket->send(pack);

pack.clear();

//Get length of file
in.seekg(0, ios::end);
int length = in.tellg();
in.seekg(0, ios::end);

char* buf = new char[size];

for (unsigned int i = 0; i < length / size; i++) { //Read and send every `size`-sized block we can
in.read(buf, size);
pack << sf::Int32(size) << buf;
ClientSocket->send(pack);
}

in.read(buf, length % size);
pack << sf::Int32(length % size) << buf;
ClientSocket->send(pack);

pack.clear();

pack << "done";
ClientSocket->send(pack);

pack.clear();
} else {
pack << sf::Int8(0);
ClientSocket->send(pack);
}

编辑:我知道这里已经快一个月没有任何帖子了,但我仍然对此有疑问。我不知道如何解决这个问题。

最佳答案

这不是真正的答案,但我无法对问题添加评论,所以...

SFML 1.6 未得到维护,如果 SFML 本身存在问题,它可能已在 2.0 版中得到解决。可以下载RC here .

关于c++ - 正在更改的 SFML TCP 数据包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12252346/

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