I’d like to match the digit of IP with C.

huangapple go评论60阅读模式
英文:

I'd like to match the digit of IP with C

问题

根据协议,我必须发送客户端的IP地址,但我不知道如何做。

我正在使用C语言制作Socket TCP通信服务器,根据协议。

一旦客户端访问,数据必须发送到该IP地址。

协议包括:SendIP(16字节) DestinationIP(16字节) ...

IP地址的数字必须匹配,如000.000.000.000-(例如127.000.000.001)。

请给我一些提示!

我将接收到的IP地址放在结构体中,并提取了点号的数字。如果减去的值不是4,我逐个移动了数字。

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];
        }
    }
}

希望这些提示对你有所帮助!

英文:

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-&gt;accept_sock = accept(dlg-&gt;server_sock, (SOCKADDR*)&amp;dlg-&gt;accept_addr, &amp;size);
		if (dlg-&gt;accept_sock == SOCKET_ERROR)
		{
			break;
		}

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

		int count = 0;
		int dotcount[3];
		for (int i = 0; i &lt;= 16; i++)
		{
			if (m_user_list[index].Ip_Address[i] == &#39;.&#39;)
			{
				dotcount[count++] = i;
			}
		}
		if (dotcount[0] - dotcount[1] != 4)
		{
			for (int j = 0; j &lt;= 16; j++)
			{
				m_user_list[index].Ip_Address[j + 1] = m_user_list[index].Ip_Address[j];
			
			}	
		}
}

答案1

得分: 0

这是您提供的代码部分的翻译:

	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);
}

希望这对您有所帮助。如果您需要进一步的翻译或有其他问题,请随时提出。

英文:

I use the strtok

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

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

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

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

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


		accept_list.push_back(dlg-&gt;accept_sock);
		strcpy(m_user_list[index].Ip_Address, copyIP);
}

huangapple
  • 本文由 发表于 2023年2月6日 10:46:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/75356931.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定