gpt4 book ai didi

c++ - 尝试控制Philips Hue Lights时,未从桥接器获得任何响应

转载 作者:行者123 更新时间:2023-12-02 10:29:01 26 4
gpt4 key购买 nike

我试图编写一些C++代码来控制我的Philips Hue灯。在使用浏览器调试工具找出网桥的IP并添加了一个用户来控制灯光之后,我尝试用代码复制浏览器发送的消息以创建自己的例程。
这是我正在使用的代码:

#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>
#include <string>

#pragma comment(lib, "Ws2_32.lib")
#define PORT "80"

int main() {
const char* adress = "192.168.178.x";

// Initializing Winsock
WSADATA wsaData;

int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData); //MAKEWORD(2, 2) specifies the version
if (iResult != 0)
{
printf("WSAStartup failed: %d\n", iResult);
return 1;
}

// Data structure to hold info for the socket
struct addrinfo *result = NULL,
*ptr = NULL,
hints;

ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_INET; // Only using IPv4 adresses
hints.ai_socktype = SOCK_STREAM; // TCP socket
hints.ai_protocol = IPPROTO_TCP;

// Converting adress to readable format and stuffing it into result.
iResult = getaddrinfo(adress, PORT, &hints, &result);
if (iResult != 0)
{
printf("getaddrinfo failed: %d\n", iResult);
WSACleanup();
return 1;
}

// Actually creating a socket
SOCKET ConnectSocket = INVALID_SOCKET;

ptr = result;

// Creating socket with previously initialized data
ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);
if (ConnectSocket == INVALID_SOCKET)
{
printf("Error at socket(): %ld\n", WSAGetLastError());
freeaddrinfo(result);
WSACleanup();
return 1;
}

// Connecting to the server socket
iResult = connect(ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);

// Error checking
if (iResult == SOCKET_ERROR)
{
closesocket(ConnectSocket);
ConnectSocket = INVALID_SOCKET;
}

freeaddrinfo(result);

if (ConnectSocket == INVALID_SOCKET)
{
printf("Unable to connect to server!\n");
WSACleanup();
return 1;
}


/////////////////////////////////////////////////////////
// ACTUAL MESSAGE
std::string request = "GET /api/<username>/lights/2 HTTP/1.1\r\n";
request.append("Host: 192.168.178.x\r\n");
request.append("\r\n");

////////////////////////////////////////////////////////

printf("Request:\n%s", request.c_str());

// Send message
iResult = send(ConnectSocket, request.c_str(),(int) request.size(), 0);

// Error check
if (iResult == SOCKET_ERROR)
{
printf("send failed: %d\n", WSAGetLastError());
closesocket(ConnectSocket);
WSACleanup();
return 1;
}

printf("Bytes Sent: %ld\n", iResult);

// Shutting down socket since it won't be used anymore (can still receive data)
iResult = shutdown(ConnectSocket, SD_SEND);
if (iResult == SOCKET_ERROR)
{
printf("shutdown failed: %d\n", WSAGetLastError());
closesocket(ConnectSocket);
WSACleanup();
return 1;
}

const int buflen = 8196;
char recvbuf[buflen];
ZeroMemory(recvbuf, buflen);

// Keep socket open until connection is closed
do
{
iResult = recv(ConnectSocket, recvbuf, buflen, 0);
if (iResult > 0)
{
printf("Bytes received: %d\n", iResult);
printf("\nResponse:\n%s\n", recvbuf);
}
else if (iResult == 0)
{
printf("Connection closed\n");
}
else
{
printf("recv failed: %d\n", WSAGetLastError());
}
} while (iResult > 0);

closesocket(ConnectSocket);
WSACleanup();

return 0;
}


如果查看此问题的编辑历史记录,您会发现我以前认为我的HTTP消息有问题,但是发现了一些奇怪的行为:
每次重新启动系统后,我第一次发送消息时都会得到正确的响应。但随后的所有尝试都会导致立即关闭连接。
仍然不知道是什么原因造成的,但是我想某些东西没有正确关闭或终止?

最佳答案

经过一番尝试和错误后,我发现了一些使之起作用的方法。
如果我把这一部分:

iResult = shutdown(ConnectSocket, SD_SEND);
if (iResult == SOCKET_ERROR)
{
printf("shutdown failed: %d\n", WSAGetLastError());
closesocket(ConnectSocket);
WSACleanup();
return 1;
}
在接收消息的部分之后,它可以工作。
我的猜测是,尽管文档说套接字在关闭后仍然可以接收消息,但是它做出了一些更改,使服务器认为应该关闭连接。
不知道那是否正确。
如果有人知道实际原因,请随时添加评论。
我很高兴我不必再处理这个了。

关于c++ - 尝试控制Philips Hue Lights时,未从桥接器获得任何响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63084739/

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