1

我正在使用 Winsocks 2.2 和 Visual Studio 2010 编写 C++ 网络游戏,并决定使用我的网络服务器存储游戏的活动服务器列表是一个好主意。当服务器启动时,它会在我的网络服务器上注册自己,退出注销;当有人访问服务器列表时,服务器本身将尝试清理列表(这种行为我仍在努力设计为不涉及服务器上的太多工作;但我认为当游戏服务器尝试添加自身时,我的php 文件将使用 fsockopen 来检测它是否可以从外部网络实际访问服务器,如果不能,则不会添加服务器,直到它可以正确设置端口转发或以某种方式解决问题)。

好的,所以经过一些研究,我想出了如何使用 TCP 连接从服务器获取一些东西,从格式化 HTTP 服务器的专用消息。这是我所拥有的:

if(FAIL == Connection::Get_Connection(&m_Connection, networkSettings.ServerListAddress, 80))
{
    return FAIL;
}

m_Connection.SendMsg("GET /servers.php HTTP/1.1\r\nHost: cyclotron.leetnightshade.com\r\nUser-Agent: CycloTron\r\n\r\n");

我期待返回格式正确的数据,但我并没有完全得到。这是我得到的:

2f
Server Count:1
129.21.138.1,40000,Depth of Hell
0

这是带有所有标头信息的一些垃圾的另一个输出:

HTTP/1.1 200 OK
Date: Tue, 12 Apr 2011 23:23:11 GMT
Server: Apache
X-Powered-By: PHP/5.2.17
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Set-Cookie: PHPSESSID=8254688ee345202bd177d57e4ba339b2; path=/
Set-Cookie: PHPSESSID=73eae89f61e7268f433af9bdfe299173; path=/
Set-Cookie: PHPSESSID=8fb5d6fd9f1023bb00290b4daa3c7952; path=/
Connection: close
Transfer-Encoding: chunked
Content-Type: text; charset=us-ascii

e
Server Count:1
21

129.21.138.1,40000,Depth of Hell
0

这就是我的输出应该看起来的样子,我偶尔会得到这个,但不是所有时候:

HTTP/1.1 200 OK
Date: Tue, 12 Apr 2011 23:32:13 GMT
Server: Apache
X-Powered-By: PHP/5.2.17
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Set-Cookie: PHPSESSID=a3c88c2d96d45c6f6d3b029e095c429a; path=/
Set-Cookie: PHPSESSID=bf19734ff60813d6d0a5ba944410356a; path=/
Set-Cookie: PHPSESSID=c36a2d9e12c81d4a19a7f41dc5522b4e; path=/
Content-Length: 47
Connection: close
Content-Type: text; charset=us-ascii

Server Count:1
129.21.138.1,40000,Depth of Hell

我认为这并不重要,但这是我在 Web 服务器上的 PHP 代码:

$num = mysql_num_rows($result);
echo 'Server Count:'.$num;

while ($row = mysql_fetch_assoc($result))
{
    // TODO: check date of entry, if it's really old, remove it.
    echo PHP_EOL.$row['address'].','.$row['port'].','.$row['displayName'];
}

这是一些涉及接收字符串的代码(是的,目前它有点简单,我意识到我可以使用 cstring 函数来查找两个新行,所以我不必进行字符串复制,我'我只是想坚持使用字符串来让事情变得更容易):

memset(m_MsgBuffer, 0, sizeof (char) * M_BufferSize);

m_Received = recv(m_Connection.M_Socket, m_MsgBuffer, M_BufferSize, 0);

m_MsgBuffer[m_Received] = '\0';

string str = string(m_MsgBuffer);

size_t index = str.find("\r\n\r\n");
str.erase(0,index);

std::cout << "Received message: " << str << std::endl;

那么,你们中有人知道这些垃圾数据来自哪里吗?

编辑:查看正确的标头信息后,带有垃圾的那个具有“Transfer-Encoding:chunked”并且没有“content-length”。...到底是怎么回事?

4

1 回答 1

6

所谓“垃圾”,其实就是来自服务器的分块数据。HTTP/1.1 服务器可以自由地以分块格式发回数据,如果它愿意的话,并且 HTTP/1.1 规范非常明确:“所有 HTTP/1.1 应用程序必须能够接收和解码“分块”传输编码“ .

分块编码的细节在 HTTP/1.1 规范中有描述:

http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.6.1

如果您正在编写 HTTP 客户端,则需要阅读 HTTP 规范...

于 2011-04-12T23:49:29.673 回答