-2

I was writing my first client in C when I reached the point where I have to send a socket to the server. When i try to get its address I get a

Resource temporarily unavailable

and I can't find what is causing this problem.

dadesSoftConfig->ipServer has inside localhost

    struct hostent *ent;

    ent = gethostbyname(dadesSoftConfig->ipServidor);
    if (ent == NULL) {
        int errnum = errno;
        fprintf(stderr, "Client finalitzat: %s\n", strerror(errnum));
        exit(EXIT_FAILURE);
    }

I do not send any socket, wait for any data yet when I do this call , this is happening at the very beginning, even before the register phase of my protocol.

As requested, this is a print of the dadesSoftConfig:

DEBUG_INFO:     Nom: SW-01
                Mac: 89F107457A36
                Server: localhost
                Server-port: 2019

And this is how I print it:

void print_software_configuration(TDadesSoftConfig *dades) {
    char *msg;
    msg = (char *) malloc(sizeof(char) * 75);
    if (sprintf(msg, "\tNom: %s \t\tMac: %s \t\tServer: %s \t\tServer-port: %d\n", 
                dades->nom, dades->mac, dades->ipServidor, dades->serverPort) < 0) {
        fprintf(stderr, "No s'ha pogut mostrar el contingut llegit\n");
    } else {
        print_debug_info(msg);
    }
    free(msg);
}

I tried sending "127.0.0.1" to gethostbyname() function and the code perfectly works, even when I store it to my struct. Any idea why it doesn't work when sending "localhost"?

Jo4nP4l4u
  • 85
  • 2
  • 9
  • Possible duplicate of [Errno: 11, Resource Temporarily Unavailable](https://stackoverflow.com/questions/13554691/errno-11-resource-temporarily-unavailable) – Superlokkus Feb 27 '19 at 17:54
  • There may be nothing wrong with your code; the system may be swamped with work and not have sufficient resources. In theory, you should wait a short while and try again — the resource is temporarily unavailable and will become available again soon, probably. OTOH, it is also quite possible that the resource won't become available, especially if you've already allocated a lot of the resource and not freed it. – Jonathan Leffler Feb 27 '19 at 18:00
  • Given the extra information added to the question, we probably need to see an MCVE ([MCVE]). It won't contain any of the code called after this block — and you'll have print statements to show what's in `dadesSoftConfig->ipServidor` so we can see what you're requesting. Maybe there are simply too many hosts identified by your request? It should be quite small as you don't need much of the setup work. Note that the MCVE _must_ (repeat **must**) reproduce the error. – Jonathan Leffler Feb 27 '19 at 18:04
  • I'd recommend to use getaddrinfo, because gethostbyname is obsoleted – Dmitry Feb 27 '19 at 18:11
  • 2
    For starters, you should be using `h_errno`, not `errno`. – SergeyA Feb 27 '19 at 18:21
  • I tried reading the name of the server from the file and directly using the function and still the same result. Could it be the NET config of my pc? – Jo4nP4l4u Feb 27 '19 at 18:35
  • @SergeyA or `gethostbyname(2)_r()`, which outputs the `h_errno` value to a caller-supplied variable. – Remy Lebeau Feb 27 '19 at 18:49
  • @RemyLebeau here is no arguing that `_r` versions are unquestionably better than legacy. It is also true that getaddrinfo should be preferred. – SergeyA Feb 27 '19 at 18:52
  • I'm studying networks at this moment, professor told us to use this functions, can't argue with this. The _r version maybe should be fine, I'll have to ask him. – Jo4nP4l4u Feb 27 '19 at 18:59

1 Answers1

0

As pointed by my prints, the data structure was correct, but the data it contained was not. Info should be displayed separated by \t\t not a \n\t\t.

The problem was solved when setting correctly the delimiters to strtok() function which parses the input.

Jo4nP4l4u
  • 85
  • 2
  • 9