I am trying so break IP into two parts but it's not working. can anyone point out the Problem
void encode_ip_with_port(unsigned char *tlvbuf) {
// 100.100.100.100:65000
// abcd:abcd:abcd:abcd:abcd:abcd:abcd:abcd:12344
// Ipv4
struct in_addr addr;
// Remove Port
char *ipv4 = NULL ;
char *port = NULL;
printf("Input : %s\n ",tlvbuf);
char input = ":";
//char str[]="this, by the way, is a 'sample'";
ipv4 = strtok(tlvbuf, &input);
port = strtok(NULL, ":");
printf("Ipv4 : %s\n",ipv4);
printf("port : %s\n",port);
if (!inet_pton(AF_INET,ipv4 , &addr)) {
fprintf(stderr, "Could not convert address\n");
}
}
Here ipv4
is printing ipv4 : 100.100.100.100:65000
it should print 100.100.100.100
strtok
expects a string as input. You need to change the following:Add:
Change:
Working example:
Output: