与 localhost:30000 的连接被拒绝,即使我知道它正在监听
Connection to localhost:30000 refused even though I know it's listening
我正在使用 C 和 Winsock2 设置一个简单的服务器和客户端应用程序。有一天我设法让它工作,但第二天,它一直给我错误 10061(连接被拒绝)。我没有更改我的安全设置或我认为相关的任何内容。
我可以通过在地址栏中键入 localhost:30000 使用 Web 浏览器连接到服务器。我查看了我的代码,将其与官方 Winsock 教程进行了比较,但似乎没有任何效果。
由于服务器似乎完美无缺,这里是客户端代码,直到它失败的部分:
#if defined __WIN32__ || defined _WIN32 || defined WIN32
// define WINVER for MinGW
#ifndef WINVER
#define WINVER 0x0501
#endif
#include <winsock2.h>
#include <ws2tcpip.h>
#else
//(this will be for Linux code that I haven't implemented yet)
#endif
#include <stdio.h>
#include <sys/types.h>
#include <process.h>
#define PORTNUM "30000"
#define BUFLEN 512
int main(int argc, char *argv[]) {
WSADATA wsaData;
// Create new SOCKET object
SOCKET ConnectSocket = INVALID_SOCKET;
// Create socket for client
struct addrinfo *result = NULL,
*ptr = NULL,
hints;
// Initialize Winsock
int iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != 0) {
fprintf(stderr, "WSAStartup failed: %d\n", iResult);
exit(1);
}
ZeroMemory( &hints, sizeof(hints) );
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
// Resolve the server address and port
iResult = getaddrinfo(argv[1], PORTNUM, &hints, &result);
if (iResult != 0) {
printf("getaddrinfo failed: %d\n", iResult);
WSACleanup();
return 1;
}
// Attempt to connect to the first address returned by
// the call to getaddrinfo
ptr=result;
// Create a SOCKET for connecting to server
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;
}
// Connect to server.
iResult = connect(ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
if (iResult == SOCKET_ERROR) {
printf("Error at connect(): %ld\n", WSAGetLastError());
closesocket(ConnectSocket);
ConnectSocket = INVALID_SOCKET;
scanf("%s", NULL);
}
// ...
}
我不确定这是否能解决您的问题,但请对以下部分进行一些小改动:
ZeroMemory( &hints, sizeof(hints) );
hints.ai_family = AF_INET; // <-- Here
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
我认为这可能是原因,因为我在 MSDN 中找到了这句话:
The values currently supported are AF_INET
or AF_INET6
, which are the
Internet address family formats for IPv4
and IPv6
. Other options for
address family (AF_NETBIOS for use with NetBIOS, for example) are
supported if a Windows Sockets service provider for the address family
is installed [link].
也许这部分会造成您的问题。
我正在使用 C 和 Winsock2 设置一个简单的服务器和客户端应用程序。有一天我设法让它工作,但第二天,它一直给我错误 10061(连接被拒绝)。我没有更改我的安全设置或我认为相关的任何内容。
我可以通过在地址栏中键入 localhost:30000 使用 Web 浏览器连接到服务器。我查看了我的代码,将其与官方 Winsock 教程进行了比较,但似乎没有任何效果。
由于服务器似乎完美无缺,这里是客户端代码,直到它失败的部分:
#if defined __WIN32__ || defined _WIN32 || defined WIN32
// define WINVER for MinGW
#ifndef WINVER
#define WINVER 0x0501
#endif
#include <winsock2.h>
#include <ws2tcpip.h>
#else
//(this will be for Linux code that I haven't implemented yet)
#endif
#include <stdio.h>
#include <sys/types.h>
#include <process.h>
#define PORTNUM "30000"
#define BUFLEN 512
int main(int argc, char *argv[]) {
WSADATA wsaData;
// Create new SOCKET object
SOCKET ConnectSocket = INVALID_SOCKET;
// Create socket for client
struct addrinfo *result = NULL,
*ptr = NULL,
hints;
// Initialize Winsock
int iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != 0) {
fprintf(stderr, "WSAStartup failed: %d\n", iResult);
exit(1);
}
ZeroMemory( &hints, sizeof(hints) );
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
// Resolve the server address and port
iResult = getaddrinfo(argv[1], PORTNUM, &hints, &result);
if (iResult != 0) {
printf("getaddrinfo failed: %d\n", iResult);
WSACleanup();
return 1;
}
// Attempt to connect to the first address returned by
// the call to getaddrinfo
ptr=result;
// Create a SOCKET for connecting to server
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;
}
// Connect to server.
iResult = connect(ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
if (iResult == SOCKET_ERROR) {
printf("Error at connect(): %ld\n", WSAGetLastError());
closesocket(ConnectSocket);
ConnectSocket = INVALID_SOCKET;
scanf("%s", NULL);
}
// ...
}
我不确定这是否能解决您的问题,但请对以下部分进行一些小改动:
ZeroMemory( &hints, sizeof(hints) );
hints.ai_family = AF_INET; // <-- Here
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
我认为这可能是原因,因为我在 MSDN 中找到了这句话:
The values currently supported are
AF_INET
orAF_INET6
, which are the Internet address family formats forIPv4
andIPv6
. Other options for address family (AF_NETBIOS for use with NetBIOS, for example) are supported if a Windows Sockets service provider for the address family is installed [link].
也许这部分会造成您的问题。