How to test that the IP port is reachable?

FrankLike 1150015857 at qq.com
Sat Apr 6 03:24:04 UTC 2019


Hi,everyone,

How to test that the IP port is reachable?

In C,you can do this like that, what should I do in D?

/* C Code*/
https://blog.csdn.net/zhangyingchuang/article/details/51957552

#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>    /* inet(3) functions */

#define bool int
#define false 0
#define true 1

bool checkIPandPort(const char* ip, const char* port) {
    struct sockaddr_in addr;
    int fd = socket(AF_INET, SOCK_STREAM, 0);

    memset(&addr, 0, sizeof(addr));
    addr.sin_family = AF_INET;
    addr.sin_port = htons(atoi(port));
    addr.sin_addr.s_addr = inet_addr(ip);

    if (connect(fd, (struct sockaddr *) &addr, sizeof(struct 
sockaddr)) < 0) {
        printf("connect error \n");
        return false;
    }
    return true;
}
----------------------------------------------------------
But how to do it in D?
Thanks.


More information about the Digitalmars-d-learn mailing list