0

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.

John_D
  • 29
  • 6
  • 2
    And exactly *how* is it not working? "It is not working" is not a proper problem description? – Antti Haapala -- Слава Україні Nov 20 '17 at 13:10
  • `TEXT("token"),` is a string literal. You just want `token,` – 001 Nov 20 '17 at 13:12
  • Sorry, that was a mistake i made when trying out another option and forgot to correct it back. I have edited back @JohnnyMopp – John_D Nov 20 '17 at 13:18
  • Did you test what you read from the file, what `strtok` produces from that or what `strtok` produces if you provide known good string content? – Gerhardh Nov 20 '17 at 13:25
  • yes, debugging shows me token has exactly what it is supposed to have i.e, "192.168.137.1" – John_D Nov 20 '17 at 13:30
  • 1
    3435973836 is 0xcccccccc, which is the initial value for memory in Visual Studio debug builds – Karsten Koop Nov 20 '17 at 13:39
  • Check the return value from `InetPton`. If it's not `1`, call [`WSAGetLastError()`](https://msdn.microsoft.com/en-us/library/windows/desktop/ms741580(v=vs.85).aspx) – 001 Nov 20 '17 at 13:45
  • I did that and yes it returns 0 and the error code is 10022@JohnnyMopp – John_D Nov 20 '17 at 13:57
  • That's [WSAEINVAL](https://msdn.microsoft.com/en-us/library/windows/desktop/ms740668(v=vs.85).aspx#WSAEINVAL). Are you compiling your project for UNICODE? – 001 Nov 20 '17 at 14:07
  • yes. it is set for unicode character set – John_D Nov 20 '17 at 14:16
  • `InetPton` is expecting a wide string. You need to either convert `token` to wide string ([MultiByteToWideChar()](https://msdn.microsoft.com/en-us/library/windows/desktop/dd319072(v=vs.85).aspx)) or explicitly call the mult-byte version (`InetPtonA`). – 001 Nov 20 '17 at 14:19
  • just used InetPtoA(). This time no error but the value is not as I expected it. It is 25798848. Thanks for your help. – John_D Nov 20 '17 at 14:24
  • That's the correct value. See [Why do inet\_ntoa and inet\_ntop "reverse" the bytes?](//stackoverflow.com/q/30153966) – 001 Nov 20 '17 at 14:34
  • ow yes, thanks a million! – John_D Nov 20 '17 at 14:35
  • Curious that code used `atol()` and not `atoi()` with `int node_id; int group_id;` – chux - Reinstate Monica Nov 20 '17 at 16:25
  • Post definition of `readLine` – chux - Reinstate Monica Nov 20 '17 at 16:26
  • they gave me the desired result, is there something wrong with them? Here is the definition of readLine:- `char readLine[1024] = { '\0' };` @chux – John_D Nov 21 '17 at 07:37
  • @John_D `node_id` is an `int`. `atol()` return a `long`. "is there something wrong with them?" --> `atol()` and `atoi()` provide different results outside the`int` range. It also would be more clear to use `strN_read.node_id = atoi(token);` and not raise compiler warnings like "conversion to 'int' from 'long int' may alter its value". This keep focus on your main issue. – chux - Reinstate Monica Nov 21 '17 at 16:13

0 Answers0