在SFML论坛中没有得到任何帮助之后,我决定看看这里是否有人可以提供任何帮助。我有一个应用程序通过TCP连接到服务器应用程序更新自己。除了一个特定的数据包传输之外,这种方法完全正常,其中数据包以某种方式被更改。服务器发送包含数字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);
}
编辑:我知道已经差不多一个月了,因为这里有任何帖子,但我仍然遇到这个问题。我知道如何解决这个问题。
答案 0 :(得分:1)
这不是一个真正的答案,但我无法在这个问题上添加评论......
SFML 1.6未得到维护,如果SFML本身存在问题,则可能已在2.0版中解决。您可以下载RC here。
答案 1 :(得分:0)
所以,事实证明问题相当简单。你知道我是如何添加大量pack.clear()
行的吗?结果我忘了在接收原始数据和在服务器上发送原始数据之间添加一个。所以我的数据包仍然有第一次调用sock.receive(pack)
时遗留下来的数据(距离上面发布的服务器代码的开头几行)。我花了很长时间才看到这一点。拨打pack.clear()
电话,问题就消失了。