无法从套接字指针取消引用 ip 和端口

Trouble dereferencing ip and port from socket pointer

我正在做一个简单的 send/receive,为了在线程中执行每个请求,我设置了我的处理函数,如下所示:

更新:使用 std::thread 并在使用后正确关闭套接字。我没有使用循环来读取 atm,因为我只是发送远低于 1024 缓冲区限制的小测试消息。不幸的是,我仍然收到 ip 和端口的奇怪数据。

int P2PServer::socketReceiveHandler(int s) {
    struct sockaddr_in *sin = (struct sockaddr_in *)&s;
    
    char buffer[1024] = {0};
    int reader;
    reader = read(s, buffer, 1024);
    if (reader <= 0) {
        return 0;
    } else {
        std::cout << reader << std::endl; // let's see
    }

    char ip[INET_ADDRSTRLEN];
    int port=0;
    inet_ntop(AF_INET, &(sin->sin_addr), ip, sizeof(ip));
    port = htons(sin->sin_port);
    strcpy(ip, inet_ntoa(sin->sin_addr));
    printf ("Connection from %s:%d\n", ip, port);

    printf("Connection from: %d.%d.%d.%d", buffer[197], buffer[198], buffer[199], buffer[200]);
    //int port = 0;                         // Start with zero
    port |= buffer[204] & 0xFF;             // Assign first byte to port using bitwise or.
    port <<= 8;                             // Shift the bits left by 8 
    port |= buffer[205] & 0xFF;             // (so the byte from before is on the correct position)
    printf(" Port: %d\n\n", port);


    char const* message = "I am an amazing server!";
    std::cout << "received: " << buffer << std::endl; 
    send(s, message, strlen(message) , 0 );
    printf("Server : Message has been sent ! \n");

    if (close(s) == -1) {
        p2putils::logit("Close problems");
        std::cout << "errno: " << errno << std::endl;
    }
    
    return 0;
}

函数调用如下:

std::thread peerThread (&P2PServer::socketReceiveHandler,this, incomingSocket);
peerThread.join();

这是服务器的输出:

./p2pserver 127.0.0.1 10001
Node usage for peers: ./p2pserver <ip> <port> <masternode>
If this is not the master node, restart with 3rd parameter
P2P Server: 127.0.0.1 is listening on PORT: 10001
 

Waiting for incoming connections...
23
Connection from 8.233.176.178:0
Connection from: 0.0.0.0 Port: 0

received: I am an amazing client.
Server : Message has been sent ! 
 

Waiting for incoming connections...

这是客户端的输出:

./p2pserver 127.0.0.1 10002 127.0.0.1
P2P Server: 127.0.0.1 is listening on PORT: 10002
Client : Message has been sent ! 
I am an amazing server!
 

Waiting for incoming connections...

所以挽回局面的函数是 getpeername()

这是我的工作解决方案:

int P2PServer::socketReceiveHandler(int s) {
    struct sockaddr_in addr;
    
    char buffer[1024] = {0};
    int reader;
    reader = read(s, buffer, 1024);
    if (reader <= 0)
        return 0;

    socklen_t len;
    len = sizeof(addr);
    getpeername(s, (struct sockaddr*)&addr, &len);
    std::cout << "Connection from: " 
              << inet_ntoa(addr.sin_addr) 
              << " : " 
              << ntohs(addr.sin_port) << std::endl;

    char const* message = "I am an amazing server!";
    std::cout << "received: " << buffer << std::endl; 
    send(s, message, strlen(message) , 0 );
    printf("Server : Message has been sent ! \n");

    if (close(s) == -1) {
        p2putils::logit("Close problems");
        std::cout << "errno: " << errno << std::endl;
    }
    
    return 0;
}

部分输出如下:

P2P Server: 127.0.0.1 is listening on PORT: 10001
 

Waiting for incoming connections...
Connection from: 127.0.0.1 : 58560