0

我正在创建一个winsock UDP 程序。我正在使用的代码如下所示。

我总是收到端口分配错误。

我无法理解为什么总是分配的端口为零。如果有人可以帮助我解决这个问题......

void UDPecho(const char *, const char *);

void errexit(const char *, ...);

#define LINELEN  128
#define WSVERS  MAKEWORD(2, 0)

void main(int argc, char *argv[])
{
    char *host = "localhost";
    char *service = "echo";
    WSADATA wsadata;
    switch (argc) {
    case 1:
        host = "localhost";
        break;

    case 3:
        service = argv[2];
        /* FALL THROUGH */

    case 2:
        host = argv[1];
        break;

    default:
        fprintf(stderr, "usage: UDPecho [host [port]]\n");
        exit(1);

    }

    if (WSAStartup(WSVERS, &wsadata))
        errexit("WSAStartup failed\n");

    UDPecho(host, service);

    WSACleanup();

    exit(0);
}

void UDPecho(const char *host, const char *service)
{
    char buf[LINELEN+1];
    SOCKET s;
    int nchars;
    struct hostent *phe;
    struct servent *pse;
    struct protoent *ppe;
    struct sockaddr_in sin, my_sin;
    int type, status, client_port, size;
    char *transport = "udp";

    memset(&sin, 0, sizeof(sin));
    sin.sin_family = AF_INET;

    /* Map service name to port number */
    if ( pse = getservbyname(service, transport) )
        sin.sin_port = pse->s_port;
    else if ( (sin.sin_port = htons((u_short)atoi(service)))== 0)
        errexit("can't get \"%s\" service entry\n", service);

    /* Map host name to IP address, allowing for dotted decimal */
    if ( phe = gethostbyname(host) )
        memcpy(&sin.sin_addr, phe->h_addr, phe->h_length);
    else if ( (sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE)
        errexit("can't get \"%s\" host entry\n", host);

    printf("Our target server is at address %s\n", inet_ntoa(sin.sin_addr));
    printf("The size of an FD set is %d\n", sizeof(FD_SET));

    /* Map protocol name to protocol number */
    if ( (ppe = getprotobyname(transport)) == 0)
        errexit("can't get \"%s\" protocol entry\n", transport);

    /* Use protocol to choose a socket type */
    if (strcmp(transport, "udp") == 0)
        type = SOCK_DGRAM;
    else
        type = SOCK_STREAM;

    /* Allocate a socket */
    s = socket(PF_INET, type, ppe->p_proto);
    if (s == INVALID_SOCKET)
        errexit("can't create socket: %d\n", GetLastError());

    size = sizeof(sin);
    memset(&my_sin, 0, sizeof(sin));
    getsockname (s, (struct sockaddr *) &my_sin, &size);
    client_port = ntohs(my_sin.sin_port);

    if (client_port != 0)
        printf ("We are using port %2d\n", client_port);
    else {
        printf("No port assigned yet\n");
    }
}


void errexit(const char *format, ...)
{
    va_list args;
    va_start(args, format);
    vfprintf(stderr, format, args);
    va_end(args);
    WSACleanup();

    exit(1);
}
4

1 回答 1

3

除非您在套接字上发出 asendto()或 a ,否则UDP 不会绑定到侦听端口。bind()后者使您可以选择要侦听的端口。 Sendto()另一方面,将为您选择一个临时端口。我希望端口将保持为零,直到您执行这两件事中的一件。

澄清

在一些评论之后,我对此进行了更多研究。根据Single UNIX Specification,调用的结果socket()是一个未绑定的套接字。通过调用显式绑定套接字bind()或隐式绑定sendto()

将套接字的名称视为包含其(地址族、协议、本地 IP 地址和本地端口号)的元组。前两个在socket()call 中指定,后两个在 call中指定bind()。在无连接协议的情况下,sendto()对断开连接的套接字的调用将导致隐式绑定到操作系统选择的端口号。

最令人惊讶的是,我能找到的关于这种行为的唯一参考是在Microsoft 文档sendto()的备注部分。

如果套接字未绑定,系统会为本地关联分配唯一值,然后将套接字标记为已绑定。在这种情况下,应用程序可以使用 getsockname(Windows 套接字)来确定本地套接字名称。

状态的单一 UNIX 规范getsockname()

如果套接字尚未绑定到本地名称,则地址指向的对象中存储的值未指定。

It seems that a successful return with an unspecified result is the "standard" behavior... hmmm... The implementations that I have tried all return successfully with a socket address of 0.0.0.0:0 which corresponds to INADDR_ANY with an unspecified port. After calling either bind() or sendto(), getsockname() returns a populated socket address though the address portion might still be INADDR_ANY.

于 2009-10-04T18:17:33.560 回答