【问题标题】:Receiving SOCKET_ERROR when using WinSock使用 WinSock 时收到 SOCKET_ERROR
【发布时间】:2018-01-09 14:56:49
【问题描述】:

我浏览了论坛并搜索了几个小时,但找不到确切的解决方案。如果您能给我一些有关此代码有什么问题的信息,我将不胜感激。我已经尝试了很多东西,但似乎没有任何效果。很多代码摘自微软的文章:Complete WinSock client code

C++ 代码:

#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif


#include "stdafx.h"
#include <winsock2.h>
#include <ws2tcpip.h>
#include <iphlpapi.h>

#pragma comment(lib, "Ws2_32.lib")

int main() {
WSADATA wsaData;

#define DEFAULT_BUFLEN 512
#define DEFAULT_PORT "27015"
SOCKET ConnectSocket = INVALID_SOCKET;
struct addrinfo *result = NULL,
    *ptr = NULL,
    hints;
char *sendbuf = "this is a test";
char recvbuf[DEFAULT_BUFLEN];
int iResult;
int recvbuflen = DEFAULT_BUFLEN;

// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0) {
    printf("WSAStartup failed with error: %d\n", iResult);
    return 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("127.0.0.1", NULL, &hints, &result);
if (iResult != 0) {
    printf("getaddrinfo failed with error: %d\n", iResult);
    WSACleanup();
    return 1;
}

// Attempt to connect to an address until one succeeds
for (ptr = result; ptr != NULL; ptr = ptr->ai_next) {

    // Create a SOCKET for connecting to server
    ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype,
        ptr->ai_protocol);
    if (ConnectSocket == INVALID_SOCKET) {
        printf("socket failed with error: %ld\n", WSAGetLastError());
        WSACleanup();
        return 1;
    }

    // Connect to server.
    iResult = connect(ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
    if (iResult == SOCKET_ERROR) {
        closesocket(ConnectSocket);
        ConnectSocket = INVALID_SOCKET;
        continue;
    }
    break;
}

freeaddrinfo(result);

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

// Send an initial buffer
iResult = send(ConnectSocket, sendbuf, (int)strlen(sendbuf), 0);
if (iResult == SOCKET_ERROR) {
    printf("send failed with error: %d\n", WSAGetLastError());
    closesocket(ConnectSocket);
    WSACleanup();
    return 1;
}

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

// shutdown the connection since no more data will be sent
iResult = shutdown(ConnectSocket, SD_SEND);
if (iResult == SOCKET_ERROR) {
    printf("shutdown failed with error: %d\n", WSAGetLastError());
    closesocket(ConnectSocket);
    WSACleanup();
    return 1;
}

// Receive until the peer closes the connection
do {

    iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
    if (iResult > 0)
        printf("Bytes received: %d\n", iResult);
    else if (iResult == 0)
        printf("Connection closed\n");
    else
        printf("recv failed with error: %d\n", WSAGetLastError());

} while (iResult > 0);

// cleanup
closesocket(ConnectSocket);
WSACleanup();
getchar();
return 0;

}

【问题讨论】:

  • 那么你在哪一行得到SOCKET_ERROR?您是否尝试过调用WSAGetLastError 来检索错误代码?
  • 有什么比它不起作用更具体的吗?例如,WSAGetLastError() 调用的结果?您的问题应包括 exact 输出。我看到您至少得到该错误结果,但您介意与我们其他人分享吗?
  • 我很抱歉。 iResult == SOCKET_ERROR 在“// 连接到服务器。”下面的行上。让我试试。
  • 收到 10049 (WSAEADDRNOTAVAIL)。描述(在微软的页面上)说可能有很多原因。例如,在本地计算机上无效,但我不知道为什么会这样。
  • 为什么我被否决了?我做错什么了吗?我尽力以最友好的方式提出问题,但始终准确地提供哪些信息并不总是那么容易。

标签: c++ mfc


【解决方案1】:

您忘记指定要连接的 TCP 端口号。这是getaddrinfo 的第二个参数。

例如这样:

  iResult = getaddrinfo("127.0.0.1", "80", &hints, &result);

【讨论】:

  • 现在我的连接被拒绝(10061)。我不知道为什么会这样。防火墙?
  • 你需要运行一些服务器进程来监听指定的端口。
  • 我设法连接到服务器,并正确接收到字节。非常感谢您的帮助!
猜你喜欢
  • 2013-05-20
  • 1970-01-01
  • 1970-01-01
  • 2013-12-14
  • 2011-04-25
  • 1970-01-01
  • 1970-01-01
  • 2011-11-06
  • 1970-01-01
相关资源
最近更新 更多