Home > Software design >  Thread-safe split ip:port in c
Thread-safe split ip:port in c

Time:10-13

I have a text containing a list of ip:port and I have a multi-threading program that splits them to IP and port separately I used strtok but it gives a lot of errors and problems and makes the program gets segmentation fault every time it needs make to strdup char pointer which very annoying I used strchr but it only gives me the port but can't use it to extract the IP the goal is extract IP as a string and the port as integer and never gets segmentation failed whatever

example:

This is the start function of the thread

void* startthread() {
    do {
        char* proxy = "127.0.0.1:443";

        char* buffered = strdup(proxy);

        char* host = strtok(buffered,":");
        int port = atoi(strtok(NULL,":"));

        printf("%s:%d",host,port);
    }while(1);
}

gdb

Thread 8 "main" received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x7ffff4a34700 (LWP 47410)]
__GI_____strtol_l_internal (nptr=0x0, endptr=endptr@entry=0x0,

CodePudding user response:

One fairly simple way to handle this (that, in keeping with your tags) is compatible with both C and C ) would be to use sscanf:

char host[256];
int port;

sscanf(proxy, "%5[^:]:%d", host, &port);

But yes, strtok and multithreading is a lousy combination. Then agin, strtok and almost anything is a lousy combination.

I suppose to be complete, I need to point out that that the [^:] scan format depends on implementation defined behavior. In theory, an implementation is allowed to treat this literally--as meaning either ^ or :, rather than "anything except a colon"--but I know of only one implementation ever (Borland's compilers for MS-DOS) that did so, and it's long-since obsolete.

CodePudding user response:

In this simple case you can replace strtok with strchr.

void* startthread() {
    do {
        const char* proxy = "127.0.0.1:443";

        char* buffered = strdup(proxy);
        if(buffered != NULL) {

            char* host = buffered;
            int port = 443; // or any useful default value

            char* colon = strchr(buffered, ':')
            if(colon != NULL) { // found?
                *colon = '\0'; // cut off port number from host or IP address
                port = atoi(colon 1); // I suggest to use strtol instead of atoi
            } // otherwise there is nothing to cut off, and use default port
            printf("%s:%d",host,port);

            free(buffered);
        } else {
            // handle error
        }
    }while(1);
}
  • Related