John_D
John_D

Reputation: 29

how to convert from string to sockaddr in c in windows

I am new to C and am trying to read IP addresses from a .csv excel sheet and assign them to a sockaddr. I have the following code but it is not working.

Edit: Debugging shows the value of S_addr after the assignment is 3435973836 for an ip address which is "192.168.137.1"

If it matters; token is declared as char *token;

typedef struct node
{
    int node_id;
    int group_id;
    struct sockaddr_in  node_addr;
} node;

struct sockaddr_in ip4addr;
struct node strN_read;

while (fgets(readLine, 1024, input_file) != NULL)
{
    token = strtok_s(readLine, ",", &strtk);
    strN_read.node_id = atol(token);
    token = strtok_s(NULL, ",", &strtk);
    strN_read.group_id = atol(token);
    token = strtok_s(NULL, ",", &strtk);
    InetPton(AF_INET, PTSTR(token), &ip4addr.sin_addr);
    strN_read.node_addr.sin_addr.S_un.S_addr = ip4addr.sin_addr.S_un.S_addr;
    // ...
    // ...
}

Replacing

InetPton(AF_INET, PTSTR(token), &ip4addr.sin_addr);

with

WSAStringToAddress((LPWSTR)(token), AF_INET, NULL, (LPSOCKADDR)&ip4addr, &addrSize);

also gives the same result.

Upvotes: 0

Views: 868

Answers (0)

Related Questions