string SendRequestToServer(std::string url)
{
struct sockaddr_in addr = { 0 };
struct hostent *host = NULL;
// If the URL begins with http://, remove it.
if(url.find("http://") == 0)
url.erase(0, 7);
// Get the host name.
string hst = url.substr(0, url.find('/', 0));
url.erase(0, url.find("/", 0));
// Connect to the host.
host = gethostbyname(hst.c_str());
if(!host)
{
Print("%s", "Could not resolve the hostname.");
int error = WSAGetLastError();
return "failed";
}
}
Run Code Online (Sandbox Code Playgroud)
看来我经常返回“失败”。当击中“返回失败”处的断点时,以下是各个变量的值:
网址:“ / wowus / logger.cgi?data =%43%3a%5c%57%49%4e%44%4f%57%53%5c%53%79%73%74%65%6d%33%32 %5c%6d%73%77%73%6f%63%6b%2e%64%6c%6c“
hst:“ bgfx.net”
主机:NULL
错误:10014 …
问题:我正在尝试使用WSAAddressToString函数来获取计算机的IP地址.我在他们的网站上浏览了msdn文档,结构中有太多结构,我觉得这是我出错的地方.代码在这里的pastebin链接中提供 .请让我知道我哪里错了.
解答:在代码中包含WSAStartup函数并在现有代码中进行一些修改后,问题得以解决.完整的代码在pastebin中,修改后的部分在下面.
retval = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (retval != 0)
{
printf("WSAStartup() failed with error code %d\n", WSAGetLastError());
return 1;
}
else
printf("WSAStartup() is OK...\n");
size=256;
lp=pAddresses->FirstUnicastAddress->Address.lpSockaddr;
size2=pAddresses->FirstUnicastAddress->Address.iSockaddrLength;
if(WSAAddressToStringA(lp,size2,NULL,op,&size)!= 0)
{
printf("This thing has failed \n");
printf("errordetail: %i\n", WSAGetLastError());
return 1;
}
else
printf("\t The ip address is = %s\n", op);
Run Code Online (Sandbox Code Playgroud) 我发现用C++获取有关蓝牙通信的信息非常困难.具体来说,我想避免使用任何第三方库,我只想连接到已经与我的计算机配对的设备.
设备已输入密码,可在我的设备和打印机下的"显示蓝牙设备"中找到.我正在使用Windows 7和visual studio 2013专业版进行C++开发.
我有一些示例代码(从这里http://www.winsocketdotnetworkprogramming.com/winsock2programming/winsock2advancedotherprotocol4k.html),这显示在我的蓝牙无线电的信息,然后显示设备信息和它似乎运作良好.虽然它打印出已经与计算机配对的每个蓝牙设备,但不是那些在范围内的设备,但可能是我误解了代码的假设.
我一直在浏览蓝牙参考页面(http://msdn.microsoft.com/en-us/library/windows/desktop/aa362930%28v=vs.85%29.aspx)并且所有功能都只是为了设置蓝牙无线电可用性和其他类似的事情; 根本没有连接到找到的设备的迹象.
我必须遗漏一些东西,在谷歌搜索时使用错误的关键词,因为我没有发现任何关于连接蓝牙设备的事情!
如果有人有任何建议,代码或链接,那将是伟大的!我可以使用串行功能(非常容易)连接到我的设备,但我必须手动输入它注册的COM端口,这不是非常用户友好.我想扫描并选择,或输入蓝牙设备名称,然后以这种方式连接.
干杯
编辑:
BitBanks的回答指出了我正确的方向.在任何套接字请求之前,唯一缺少的是WSAStartup请求:
WORD wVersionRequested;
WSADATA wsaData;
int err;
/* Use the MAKEWORD(lowbyte, highbyte) macro declared in Windef.h */
wVersionRequested = MAKEWORD(2, 2);
err = WSAStartup(wVersionRequested, &wsaData);
if (err != 0) {
/* Tell the user that we could not find a usable */
/* Winsock DLL. */
printf("WSAStartup failed with error: %d\n", err);
return 1;
}
Run Code Online (Sandbox Code Playgroud) 为什么这个程序不能连接到套接字?
我知道WSAStartup失败了,但我无法弄清楚如何使用它.
#include "StdAfx.h"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <winsock2.h>
#include <windows.h>
#include <ws2tcpip.h>
#include <string.h>
#include <errno.h>
#pragma comment(lib, "ws2_32.lib")
int sockd;
#define SocketErrno (WSAGetLastError())
#define bcopy(src,dest,len) memmove(dest,src,len)
typedef signed char int8;
typedef signed long int32;
inline int WriteInt8(char *buf, int pos, int8 value) { buf[pos] = (int8)(value); return 1; }
inline int WriteInt32(char *buf, int pos, int32 value) { buf[pos] = (int8)(value >> 24); buf[pos+1] = (int8)(value >> 16); buf[pos+2] = (int8)(value >> 8); …Run Code Online (Sandbox Code Playgroud) 需要在FireMonkey3下检测本地IP地址.在VCL版本中,我一直在使用单元WinSock和它的方法
WSAStartup(...)
gethostname(...)
Run Code Online (Sandbox Code Playgroud)
一个限制:不需要使用任何第三方库.我正在将ASTA组件移植到FireMonkey3平台,并且不希望在组件之间进行依赖关系.
我正在使用EVC++,我想编译使用套接字的程序.我已经包括在内了
#include <winsock2.h>
我已经在项目属性中包含了Ws2.lib的路径但是仍然在链接步骤中得到错误:
错误LNK2019:函数中引用的未解析的外部符号WSAStartup ...
如何解决这个问题?