简体   繁体   English

C ++ WinSock是否从PHP页面获取数据?

[英]C++ WinSock Get Data From PHP Page?

Hello I'm quite new to using sockets and am not that familiar with them yet, Basically all i am trying to do is pass a string variable to a web address (eg www.example.com/index.php?Example=StringExample ) and then get a response, so for example it would return "Test Example" if index.php looked like this: 您好,我对使用sockets还很陌生,并且对它们还不那么熟悉,基本上我想要做的就是将string variable传递给web address (例如, www.example.com/index.php?Example=StringExample )然后得到响应,因此例如,如果index.php看起来像这样,它将返回"Test Example"

<?php

if($_GET['Example'] == "StringExample")
{
    echo "Test Example";
}

?>

Here is what I've tried in c++: 这是我在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

What am i doing wrong? 我究竟做错了什么?

Btw in my case i would not like to use any libraries like boost or anything like that. 顺便说一句,在我的情况下,我不想使用诸如boost类的任何libraries Thanks for the help :) 谢谢您的帮助 :)

gethostbyname("www.example.com/index.php?Example=StringExample");

"www.example.com/index.php?Example=StringExample" is not a valid server name. “ www.example.com/index.php?Example=StringExample”不是有效的服务器名称。 This is an entire URL; 这是一个完整的URL; a server name would be "www.example.com". 服务器名称为“ www.example.com”。 gethostbyname() takes the name of a server, and not a URL, and returns its IP address. gethostbyname()采用服务器名称(而不是URL),并返回其IP地址。 Additionally, gethostbyname() has been obsoleted. 此外, gethostbyname()已被淘汰。 New code should use the getaddrinfo(3) function, instead. 新代码应改为使用getaddrinfo(3)函数。

This is obviously an HTTP URL. 显然,这是一个HTTP URL。 To download a document via HTTP it is a lot more work than just connecting a socket. 要通过HTTP下载文档,不仅要连接套接字,还要做很多工作。 Establishing a socket connection is just the first step in the process of downloading a document from an HTTP server. 建立套接字连接只是从HTTP服务器下载文档的第一步。 This must be followed by sending a valid HTTP request, and then receiving an HTTP response from the server. 这之后必须发送有效的HTTP请求,然后从服务器接收HTTP响应。

There are many libraries, such as curl , that implement the entire client-side process needed to download an HTTP document, that will handle the socket connection themselves. 有很多库,例如curl ,可以实现下载HTTP文档所需的整个客户端过程,这些过程将自己处理套接字连接。

But there's nothing wrong with trying to implement this yourself, either. 但是,尝试自己实现也没有错。 It's a good programming excersize. 这是一个很好的编程专家。

So, after resolving www.example.com 's IP address, you will need to 因此,在解析www.example.com的IP地址之后,您需要

1) Connect to the server's port 80, the default HTTP port. 1)连接到服务器的端口80(默认的HTTP端口)。

2) Send an HTTP request for "/index.php?Example=StringExample". 2)发送HTTP请求“ /index.php?Example=StringExample”。

3) Parse the HTTP response. 3)解析HTTP响应。

The specification for HTTP requests and responses is defined by RFC 2616 , which you can consult for complete documentation of how HTTP requests and responses are structured. HTTP请求和响应的规范由RFC 2616定义,您可以查阅该规范以获取有关HTTP请求和响应的结构的完整文档。

If you want to access a web server with sockets, you have to keep in mind: 如果要使用套接字访问Web服务器,则必须记住:

  • You can open a tcp/ip connection to your web server 您可以打开到Web服务器的TCP / IP连接
  • BUT afterwards you have to do the http protocol by yourself 但是之后,您必须自己执行http协议

In case of your example: 以您的示例为例:

hostent* addr = gethostbyname("www.example.com");
//...
const char* request = "GET index.html"
send(Connection, request, strlen(request), NULL)
//fetch index.html with a recv and parse it

To be more precise, if you want to access your server, you have to take a look how GET, PUT, POST, etc. are implemented in the http protocol, send the proper commands to your web server and recv() the replies 更准确地说,如果要访问服务器,则必须看看如何在http协议中实现GET,PUT,POST等,将适当的命令发送到Web服务器并recv()答复

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM