【发布时间】:2016-11-13 09:20:02
【问题描述】:
您好,我对使用sockets 很陌生,对它们还不是很熟悉,基本上我要做的就是将string variable 传递给web address(例如www.example.com/index.php?Example=StringExample)然后得到一个响应,例如,如果index.php 看起来像这样,它将返回"Test Example":
<?php
if($_GET['Example'] == "StringExample")
{
echo "Test Example";
}
?>
这是我在 C++ 中尝试过的:
struct sockaddr_in SocketAddress;
hostent* addr = gethostbyname("www.example.com/index.php?Example=StringExample");
int sizeofaddr = sizeof(addr);
SocketAddress.sin_addr.s_addr = inet_addr(addr->h_name);
SocketAddress.sin_port = htons(80);
SocketAddress.sin_family = AF_INET;
SOCKET Connection = socket(AF_INET, SOCK_STREAM, NULL);
if (connect(Connection, (SOCKADDR*)&addr, sizeofaddr) != 0)
{
return 0; //Failed to Connect
}
char buffff[256];
recv(Connection, buffff, sizeof(buffff), NULL);
//"Test Example" now stored in buffff
我做错了什么?
顺便说一句,在我的情况下,我不想使用任何 libraries 像 boost 或类似的东西。感谢您的帮助:)
【问题讨论】:
标签: php c++ sockets web connection