我使用代码连接到Hercules HW终端上的localhost作为服务器,它可以工作。
然后我尝试在其他计算机上的本地网络上的另一台计算机上设置服务器。
WSAGetLastError()11004
有什么问题?我只是想不通。
使用Winsock的客户端的代码是:
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <string>
#include <iostream>
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <cstdlib>
#include <cstdio>
#include <time.h>
// Need to link with Ws2_32.lib, Mswsock.lib, and Advapi32.lib
#pragma comment (lib, "Ws2_32.lib")
#pragma comment (lib, "Mswsock.lib")
#pragma comment (lib, "AdvApi32.lib")
#define DEFAULT_BUFLEN 512
#define DEFAULT_PORT "2112"
#define DEFAULT_PROTO SOCK_STREAM
int main()
{
char Buffer[128];
// default to localhost
char *server_name = "localhost";
unsigned short port = atoi(DEFAULT_PORT);
int retval, loopflag = 0;
int i, loopcount, maxloop = -1;
unsigned int addr;
int socket_type = DEFAULT_PROTO;
struct sockaddr_in server;
struct hostent *hp;
int first = 1;
WSADATA wsaData;
SOCKET conn_socket;
// srand(time(NULL));
if ((retval = WSAStartup(0x202, &wsaData)) != 0)
{
fprintf(stderr, "Client: WSAStartup() failed: Error %d\n", retval);
WSACleanup();
return -1;
}
else
printf("Client: WSAStartup() is OK.\n");
printf("Defina o nome ou IP do servidor[default = localhost]:\n");
// scanf( "%s" , Buffer );
gets_s(Buffer);
if (Buffer[0] != 0)
server_name = Buffer;
// server_name = &Buffer;
if (isalpha(server_name[0]))
{ // server address is a name
hp = gethostbyname(server_name);
}
else
{ // Convert nnn.nnn address to a usable one
addr = inet_addr(server_name);
hp = gethostbyaddr((char *)&addr, 4, AF_INET);
}
if (hp == NULL)
{
fprintf(stderr, "Client: Endereco IP nao identificado \"%s\": Error %d\n", server_name, WSAGetLastError());
WSACleanup();
exit(1);
}
else
printf("Client: gethostbyaddr() esta OK.\n");
memset(Buffer, 0, 128);
printf("Defina a porta do server[default = 2112]:\n");
gets_s(Buffer);
if (Buffer[0] != 0)
port = atoi(Buffer);
memset(&server, 0, sizeof(server));
memcpy(&(server.sin_addr), hp->h_addr, hp->h_length);
server.sin_family = hp->h_addrtype;
server.sin_port = htons(port);
conn_socket = socket(AF_INET, socket_type, 0); /* Open a socket */
if (conn_socket <0)
{
fprintf(stderr, "Client: Erro ao abrir o socket: Error %d\n", WSAGetLastError());
WSACleanup();
return -1;
}
else
printf("Client: socket() esta OK.\n");
printf("Client: Cliente conectando com: %s.\n", hp->h_name);
conn_socket = socket(AF_INET, socket_type, 0); /* Open a socket */
if (connect(conn_socket, (struct sockaddr*)&server, sizeof(server)) == SOCKET_ERROR)
{
fprintf(stderr, "Client: connect() falhou: Error %d\n", WSAGetLastError());
WSACleanup();
return -1;
}
else
printf("Client: connect() esta OK.\n");
loopcount = 0;
return 0;
}
使用Visual Basic 2015进行编译,使用:Project > Properties > Configuration Properties > C/C++ > Preprocessor > Preprocessor Definitions
_WINSOCK_DEPRECATED_NO_WARNINGS
_CRT_SECURE_NO_WARNINGS
telnet
和ping
在使用Hercules HW终端创建的其他服务器上工作。nmap -p 2112 192.168.1.134
结果是:
Nmap scan report for 192.168.1.134
Host is up (0.00s latency).
PORT STATE SERVICE
2112/tcp open kip
MAC Address: 00:19:BB:F7:48:29 (Hewlett Packard)
Nmap done: 1 IP address (1 host up) scanned in 3.31 seconds
答案 0 :(得分:2)
来自https://msdn.microsoft.com/en-us/library/windows/desktop/ms740668(v=vs.85).aspx
WSANO_DATA 11004
有效名称,没有请求类型的数据记录。 请求的名称有效且在数据库中找到,但没有正确的关联数据被解析。通常的示例是使用DNS(域名服务器)的主机名到地址转换尝试(使用gethostbyname或WSAAsyncGetHostByName)。返回MX记录,但没有A记录 - 表示主机本身存在,但不能直接访问。
您的代码在gethostbyaddr(...)
失败了。这是因为路由器上的DNS没有适用于您计算机的DNS记录。
拥有地址,端口和协议,您应该直接尝试使用它连接到服务器。
此外,不推荐使用gethostbyaddr:请参阅https://msdn.microsoft.com/en-us/library/windows/desktop/ms738521(v=vs.85).aspx
有关如何设置套接字以进行连接的参考,请参阅http://beej.us/guide/bgnet/output/html/singlepage/bgnet.html#connect
在beej的代码中你会看到一个getaddrinfo调用。它支持如图所示的主机名,或点缀ip字符串,因此您应该能够从示例中获取以下代码:if (isalpha(server_name[0]))
{ // server address is a name
hp = gethostbyname(server_name);
}
else
{ // Convert nnn.nnn address to a usable one
addr = inet_addr(server_name);
hp = gethostbyaddr((char *)&addr, 4, AF_INET);
}