简体   繁体   English

AF_INET为什么不能与SOCK_STREAM一起使用?

[英]Why isn't AF_INET working with SOCK_STREAM?

I'm just starting out on gaining a better understanding of socket programming, and I'm trying to build a simple program that can send and receive messages. 我只是开始对套接字编程有一个更好的了解,并且我正在尝试构建一个可以发送和接收消息的简单程序。 I've run into an issue with binding a socket to an address to use it. 我遇到了将套接字绑定到地址以使用它的问题。 Here is what I have- 这是我所拥有的

#include "stdafx.h"

using namespace std;

int main()
{
    bool devbuild = true;

    WSADATA mainSdata;
    SOCKET sock = INVALID_SOCKET;
    sockaddr tobind;
    tobind.sa_family = AF_INET;
    char stringaddr[] = "192.168.1.1";
    inet_pton(AF_INET,stringaddr,&tobind);


    //initiating Windows Socket API (WSA)
    if (WSAStartup(2.2, &mainSdata) == 0)
    {
        if (devbuild == true)
        {
            printf("WSA successfully started...\n");
        }
    }
    else
    {
        printf("WSA failed to set up, press [ENTER] to exit...\n");
        pause();
        return 1;
    }

    //instantiating the socket
    sock = WSASocket(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, NULL);
    if (sock != INVALID_SOCKET)
    {
        if (devbuild == true)
        {
            printf("Socket successfully created...\n");
        }
    }
    else
    {
        printf("Socket failed to set up, press [ENTER] to exit...\n");
        pause();
        return 2;
    }

    //binding the socket
    if (bind(sock, &tobind, sizeof(tobind)) == 0)
    {
        if (devbuild == true)
        {
            printf("Socket successfully bound...\n");
        }
    }
    else
    {
        printf("Socket failed to bind, press [ENTER] to exit...\n");
        printf("Last WSA error was: %d", WSAGetLastError());
        pause();
        return 3;
    }


    pause();


    return 0;
}

I'm getting a return of 3, with WSA error code 10047 我得到的回报为3,WSA错误代码为10047

10047 - WSAEAFNOSUPPORT Address family not supported by protocol family. 10047-WSAEAFNOSUPPORT协议族不支持地址族。 An address incompatible with the requested protocol was used. 使用了与请求的协议不兼容的地址。 All sockets are created with an associated address family (that is, AF_INET for Internet Protocols) and a generic protocol type (that is, SOCK_STREAM). 所有套接字都使用关联的地址系列(即Internet协议的AF_INET)和通用协议类型(即SOCK_STREAM)创建。 This error is returned if an incorrect protocol is explicitly requested in the socket call, or if an address of the wrong family is used for a socket, for example, in sendto. 如果在套接字调用中显式请求了错误的协议,或者套接字使用了错误的系列地址(例如,在sendto中),则会返回此错误。

This doesn't make sense, because I am only using SOCK_STREAM and AF_INET, which support one another. 这没有任何意义,因为我只使用了相互支持的SOCK_STREAM和AF_INET。

I believe one problem (possibly not the only problem, but this is what jumps out at me) is in this line: 我相信这行中有一个问题(可能不是唯一的问题,但这是我跳出来的问题):

inet_pton(AF_INET,stringaddr,&tobind);

The problem is that you are passing &tobind as the final argument, and tobind is a sockaddr , but inet_pton() expects its third argument to point to a struct in_addr instead when using AF_INET (the fact that inet_pton() takes a void-pointer rather than a typed pointer for its third argument makes this kind of mistake really easy to make). 问题是,你是通过&tobind作为最后一个参数,并tobindsockaddr ,但inet_pton()预计其第三个参数指向一个struct in_addr而不是使用时AF_INET (事实上, inet_pton()需要一个空指针,而而不是为其第三个参数使用类型化的指针,这样的错误确实很容易造成)。

So what you should be doing instead is (note added error checking also): 因此,您应该做的是(还要注意添加的错误检查):

if (inet_pton(AF_INET,stringaddr,&tobind.sin_addr) != 1)
   printf("inet_pton() failed!\n");

Also, you need to make tobind be of type struct sockaddr_in rather than just a sockaddr , and also you need to zero out the struct before using it: 另外,您需要使tobind成为struct sockaddr_in类型而不是sockaddr ,并且还需要在使用结构之前将其归零:

struct sockaddr_in tobind;
memset(&tobind, 0, sizeof(tobind));   // make sure the uninitialized fields are all zero
tobind.sa_family = AF_INET;
[...]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM