1

I have to send the client's IP according to the protocol, but I don't know how.

I am making Socket TCP communication server in C according to the protocol.

As soon as the client accesses, the data must be sent to that IP.

The protocol includes: SendIP(16BYTE) DestinationIP(16BYTE) ...

IP digits must match like 000.000.000.000- (ex. 127.000.000.001)

Please give me some hint!

I put the IP I received in the structure and received the digits of . If the subtracted value was not 4, I shifted the digits one by one.

struct UserData {
    char Ip_Address[16];
    char port[10];
    char strConnTime[30]; 
    char strCloseTime[30];
};
UserData m_user_list[10];

while(1)
{
dlg->accept_sock = accept(dlg->server_sock, (SOCKADDR*)&dlg->accept_addr, &size);
        if (dlg->accept_sock == SOCKET_ERROR)
        {
            break;
        }

strcpy(m_user_list[index].Ip_Address, inet_ntoa(dlg->accept_addr.sin_addr));

        int count = 0;
        int dotcount[3];
        for (int i = 0; i <= 16; i++)
        {
            if (m_user_list[index].Ip_Address[i] == '.')
            {
                dotcount[count++] = i;
            }
        }
        if (dotcount[0] - dotcount[1] != 4)
        {
            for (int j = 0; j <= 16; j++)
            {
                m_user_list[index].Ip_Address[j + 1] = m_user_list[index].Ip_Address[j];
            
            }   
        }
}
Lieu
  • 53
  • 4

1 Answers1

0

I use the strtok

    while (1)
    {
#if 1
        dlg->accept_addr = { 0 };
        dlg->accept_addr.sin_family = AF_INET;
        dlg->accept_addr.sin_port = htons(30110);
        dlg->accept_addr.sin_addr.s_addr = htonl(INADDR_ANY);

        int size = sizeof(SOCKADDR_IN);
#endif
        dlg->accept_sock = accept(dlg->server_sock, (SOCKADDR*)&dlg->accept_addr, &size);
        if (dlg->accept_sock == SOCKET_ERROR)
        {
            break;
        }
        strcpy(m_user_list[index].Ip_Address, inet_ntoa(dlg->accept_addr.sin_addr));

        int count = 0;
        char *digitIp[4] = { NULL, };
        char* pIpAddr = strtok(m_user_list[index].Ip_Address, ".");
        char copyIP[16] = { NULL, };

        while (pIpAddr != NULL)
        {
            digitIp[count] = pIpAddr;
            count++;

            pIpAddr = strtok(NULL, ".");
        }
        count = 0;
        sprintf(copyIP, "%03s.%03s.%03s.%03s-", digitIp[0], digitIp[1], digitIp[2], digitIp[3]);


        accept_list.push_back(dlg->accept_sock);
        strcpy(m_user_list[index].Ip_Address, copyIP);
}
Lieu
  • 53
  • 4