C++ 建立从每个节点到每个其他节点的TCP连接时出现的问题

C++ 建立从每个节点到每个其他节点的TCP连接时出现的问题,c++,sockets,tcp,C++,Sockets,Tcp,我有一些n个节点,我想通过TCP将它们相互连接起来。我正在实现以下伪代码: Node ids in {1, 2, ..., n} For node having node id k : act as the client and try to connect to nodes with id > k act as the server and listen for connection requests from nodes with id < k while tra

我有一些n个节点,我想通过TCP将它们相互连接起来。我正在实现以下伪代码:

Node ids in {1, 2, ..., n}
For node having node id k :
  act as the client and try to connect to nodes with id > k
  act as the server and listen for connection requests from nodes with id < k
    while tracking which of the nodes connected in each step

我们无法调试psuedo代码。但听起来您正在打开一个具有相同端口号的侦听端口。只设置监听端口onceokay,我正在编辑问题并发布实际代码。所有节点都在不同的主机上吗?另外,在您发布的代码中,客户端尝试在启动之前连接到服务器,我不知道这是如何工作的。是的,不同的主机。connect函数中有一个无限while循环,以便稍后重新启动服务器。您似乎没有指定要在绑定中侦听的端口号。您只需传递主机查找的结果。在这里查看将套接字设置为lsiten的示例
// port for the tcp connection
int port = 25551;
// ip address of the local node
string ip_addr;
// sockets for talking to the nodes
vector <int> sockets;

static int exchange_node_rank(int sock) {
    char msg[10];
    sprintf(msg, "%d", node_rank);
    write(sock, msg, sizeof(msg));
    read(sock, msg, sizeof(msg));
    int rank;
    sscanf(msg, "%d", &rank);
    return rank;
}

// servername is only for client, it *is* NULL for server
static pair<int, int> sock_connect(const char *servername) {
    struct addrinfo *resolved_addr = NULL;
    struct addrinfo *iterator;
    char service[6];
    int sockfd = -1;
    int listenfd = 0;
    int tmp;
    struct addrinfo hints = {.ai_flags = AI_PASSIVE,
                             .ai_family = AF_INET,
                             .ai_socktype = SOCK_STREAM};
    if(sprintf(service, "%d", port) < 0) goto sock_connect_exit;
    /* Resolve DNS address, use sockfd as temp storage */
    sockfd = getaddrinfo(servername, service, &hints, &resolved_addr);
    if(sockfd < 0) {
        fprintf(stderr, "%s for %s:%d\n", gai_strerror(sockfd), servername,
                port);
        goto sock_connect_exit;
    }
    /* Search through results and find the one we want */
    for(iterator = resolved_addr; iterator; iterator = iterator->ai_next) {
        sockfd = socket(iterator->ai_family, iterator->ai_socktype,
                        iterator->ai_protocol);
        if(sockfd >= 0) {
            if(servername) {
                /* Client mode.  Initiate connection to remote */
                while((tmp = connect(sockfd, iterator->ai_addr,
                                     iterator->ai_addrlen))) {
                }
            } else {
                /* Server mode.
           Set up listening socket an accept a connection */
                listenfd = sockfd;
                sockfd = -1;
                if(bind(listenfd, iterator->ai_addr, iterator->ai_addrlen))
                    goto sock_connect_exit;
                listen(listenfd, 1);
                sockfd = accept(listenfd, NULL, 0);
            }
        }
    }
sock_connect_exit:
    if(listenfd) close(listenfd);
    if(resolved_addr) freeaddrinfo(resolved_addr);
    if(sockfd < 0) {
        if(servername)
      fprintf(stderr, "Couldn't connect to %s:%d\n", servername, port);
        else {
            perror("server accept");
            fprintf(stderr, "accept() failed\n");
        }
    }
    int rank = exchange_node_rank(sockfd);
    return pair<int, int>(sockfd, rank);
}

void establish_tcp_connections (const vector <string> & ip_addrs) {
  // try to connect to nodes greater than node_rank
  for(int i = num_nodes - 1; i > node_rank; --i) {
    cout << "trying to connect to node rank " << i << endl;
    pair <int, int> p = sock_connect(ip_addrs[i].c_str());
    cout << "connected to node rank " << i << endl << endl;
    assert(p.second == i);
    // set the socket
    sockets[i] = p.first;
  }

  // listen to nodes trying to connect (having id less than node_rank).
  // make sure that the caller is correctly identified with its id!
  for(int i = node_rank - 1; i >= 0; --i) {
    cout << "listening for nodes with lesser rank" << endl;
    pair <int, int> p = sock_connect((char *)NULL);
    cout << "connected to node rank " << p.second << endl << endl;
    assert(p.second < node_rank);
    // set the socket
    sockets[p.second] = p.first;
  }
}