3

c ++を使用してtcpでサーバーおよびクライアントアプリケーションを作成しました。クライアントはサーバーにメッセージを送信するだけです。コードでは、サーバーはクライアントに関するいくつかの情報をコンソールに表示します: IP アドレス、ポートなど 別のアプリケーションがこのリストにアクセスするため、サーバーにクライアントの IP アドレスを保存する必要があります

サーバーに接続するすべてのクライアントのIPアドレスを保存するにはどうすればよいですか? サーバーコード:

WSADATA wsaData; 
        SOCKET ListeningSocket, NewConnection; 
        SOCKADDR_IN ServerAddr, SenderInfo;  quantity
        int Port = 7171;
        char recvbuff[1024];
        int ByteReceived, i, nlen;

        ListeningSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
        if (ListeningSocket == INVALID_SOCKET)
        {
            printf("Server: Error at socket, error code: %ld.\n", WSAGetLastError());
            WSACleanup();
            return 1;
        }

        ServerAddr.sin_family = AF_INET; 
        ServerAddr.sin_port = htons(Port);
        ServerAddr.sin_addr.s_addr = htonl(INADDR_ANY);

        if (bind(ListeningSocket, (SOCKADDR *)&ServerAddr, sizeof(ServerAddr)) == SOCKET_ERROR)
        {
            printf("Server: bind failed! Error code: %ld.\n", WSAGetLastError());
            closesocket(ListeningSocket);
            WSACleanup();
            return 1;
        }

        if (listen(ListeningSocket, 5) == SOCKET_ERROR)
        {
            printf("Server: listen: Error listening on socket %ld.\n", WSAGetLastError());
            closesocket(ListeningSocket);
            WSACleanup();
            return 1;
        }
        else
        {
            printf("Server: listening for connections...\n\n");
        }

        while(1)
        {
            NewConnection = SOCKET_ERROR;
                while(NewConnection == SOCKET_ERROR)
                {
                    NewConnection = accept(ListeningSocket, NULL, NULL);
                    printf("Server: New client got connected, ready to receive and send data...\n\n");
                    ByteReceived = recv(NewConnection, recvbuff, sizeof(recvbuff), 0);

                    if ( ByteReceived > 0 )
                    {
                        getsockname(ListeningSocket, (SOCKADDR *)&ServerAddr, (int *)sizeof(ServerAddr));
                        printf("Server: IP(s) used by Server: %s\n", inet_ntoa(ServerAddr.sin_addr)); 
                        printf("Server: port used by Server: %d\n\n", htons(ServerAddr.sin_port));
                        memset(&SenderInfo, 0, sizeof(SenderInfo));
                        nlen = sizeof(SenderInfo);
                        getpeername(NewConnection, (SOCKADDR *)&SenderInfo, &nlen);
                        printf("Server: IP used by Client: %s\n", inet_ntoa(SenderInfo.sin_addr));
                        printf("Server: Port used by Client: %d\n", htons(SenderInfo.sin_port));
                        printf("Server: Bytes received: %d\n", ByteReceived);
                        printf("Server: Message from client: \"");

                        for(i=0;i < ByteReceived;i++)
                        {
                            printf("%c", recvbuff[i]);
                        }
                        printf("\"");
                        }
                        else if ( ByteReceived == 0 )
                        {
                            printf("Server: Connection closed!\n");
                        }
                        else
                        {
                            printf("Server: recv failed with error code: %d\n", WSAGetLastError());
                        }
                    }
                }
4

1 に答える 1

4

ベクトルに inet_ntoa(SenderInfo.sin_addr) を収集できます。

WSADATA wsaData; 
        SOCKET ListeningSocket, NewConnection; 
        SOCKADDR_IN ServerAddr, SenderInfo;  quantity
        int Port = 7171;
        char recvbuff[1024];
        int ByteReceived, i, nlen;

        ListeningSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
        if (ListeningSocket == INVALID_SOCKET)
        {
            printf("Server: Error at socket, error code: %ld.\n", WSAGetLastError());
            WSACleanup();
            return 1;
        }

        ServerAddr.sin_family = AF_INET; 
        ServerAddr.sin_port = htons(Port);
        ServerAddr.sin_addr.s_addr = htonl(INADDR_ANY);

        if (bind(ListeningSocket, (SOCKADDR *)&ServerAddr, sizeof(ServerAddr)) == SOCKET_ERROR)
        {
            printf("Server: bind failed! Error code: %ld.\n", WSAGetLastError());
            closesocket(ListeningSocket);
            WSACleanup();
            return 1;
        }

        if (listen(ListeningSocket, 5) == SOCKET_ERROR)
        {
            printf("Server: listen: Error listening on socket %ld.\n", WSAGetLastError());
            closesocket(ListeningSocket);
            WSACleanup();
            return 1;
        }
        else
        {
            printf("Server: listening for connections...\n\n");
        }
        std::vector<std::string> vClientIPs; // client ip string vector
        while(1)
        {
            NewConnection = SOCKET_ERROR;
                while(NewConnection == SOCKET_ERROR)
                {
                    NewConnection = accept(ListeningSocket, NULL, NULL);
                    printf("Server: New client got connected, ready to receive and send data...\n\n");
                    ByteReceived = recv(NewConnection, recvbuff, sizeof(recvbuff), 0);

                    if ( ByteReceived > 0 )
                    {
                        getsockname(ListeningSocket, (SOCKADDR *)&ServerAddr, (int *)sizeof(ServerAddr));
                        printf("Server: IP(s) used by Server: %s\n", inet_ntoa(ServerAddr.sin_addr)); 
                        vClientIPs.push_back(std::string(inet_ntoa(ServerAddr.sin_addr))); //insert client ip
                        printf("Server: port used by Server: %d\n\n", htons(ServerAddr.sin_port));
                        memset(&SenderInfo, 0, sizeof(SenderInfo));
                        nlen = sizeof(SenderInfo);
                        getpeername(NewConnection, (SOCKADDR *)&SenderInfo, &nlen);
                        printf("Server: IP used by Client: %s\n", inet_ntoa(SenderInfo.sin_addr));
                        printf("Server: Port used by Client: %d\n", htons(SenderInfo.sin_port));
                        printf("Server: Bytes received: %d\n", ByteReceived);
                        printf("Server: Message from client: \"");

                        for(i=0;i < ByteReceived;i++)
                        {
                            printf("%c", recvbuff[i]);
                        }
                        printf("\"");
                        }
                        else if ( ByteReceived == 0 )
                        {
                            printf("Server: Connection closed!\n");
                        }
                        else
                        {
                            printf("Server: recv failed with error code: %d\n", WSAGetLastError());
                        }
                    }
                }
                WRITE_IP_SOMEWHERE(vClientIPs); // you can write this string vector to a file or database
于 2013-06-12T13:07:58.867 に答える