C - Print connecting clients address

  • Thread starter Thread starter James889
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around a C programming issue related to implementing a client-server program, specifically focusing on a function intended to print the address of connecting peers. Participants are exploring potential causes of a core dump when executing this function.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant expresses uncertainty about whether their approach to printing the peer's address is correct and mentions encountering a core dump.
  • Another participant points out that a variable, ptr, was initialized with malloc but questions its type, suggesting it should be a char pointer.
  • A third participant emphasizes the importance of checking pointers for bad values and initializing them to NULL, suggesting a modification to the malloc statement to ensure all allocated memory is initialized.
  • Concerns are raised regarding the use of the ampersand in the inet_ntop function call, with one participant questioning whether it is correctly passing the content of p->ai_addr.
  • Another participant references external documentation to clarify that p->ai_addr is a pointer, indicating that the current usage could lead to problems.
  • There is a suggestion to remove the ampersand from the inet_ntop call as a necessary adjustment.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the correct implementation details, as there are multiple viewpoints regarding pointer usage and initialization practices.

Contextual Notes

Limitations include potential misunderstandings about pointer types and the handling of memory allocation, as well as the specific requirements of the inet_ntop function.

James889
Messages
190
Reaction score
1
Hi,

I'm currently trying to implement a simple client server program in C.

Needless to say i ran into troubles, and being a novice, i turned here for some advice.

I have a function to print the connecting peers address. I don't even know if this is the right way of doing it.

For some reason this function dumps core.

Code:
char * print_peer(struct addrinfo *client) {

        if(client == NULL)
         exit();

        void *ptr = malloc(INET_ADDRSTRLEN);
        struct addrinfo *p = client;
        for (; p != NULL; p = p->ai_next) {

                if(p->ai_family == AF_INET){
                 inet_ntop(AF_INET,&(p->ai_addr),ptr,INET_ADDRSTRLEN);
                 break;
                }

        }
        return ptr;
}
 
Technology news on Phys.org
Unless defined in some include file, ptr is an unitialized variable. correction - ptr was initialized, I'm not sure what the problem is.
 
Last edited:
rcgldr said:
Unless defined in some include file, ptr is an unitialized variable.

To be fair he did initialize the variable with the malloc statement (but for some reason it's a void and not a char pointer).

For the OP though, you should always check all your pointers for bad values and make sure you always initialize them to 0 (i.e. NULL) whenever you create a structure (you can over-ride your malloc statement to do this for all allocated memory so you don't have to do this every-time you call malloc).

I recommend doing this and then printing debug statements to the console, a file, or standard output in case of an error.
 
James889 said:
Code:
                 inet_ntop(AF_INET,&(p->ai_addr),ptr,INET_ADDRSTRLEN);
I'm wondering about &(p->ai_addr). If ai_addr is a pointer, then the call is passing a pointer to p->ai_addr instead of the content of p->ai_addr.
 
Last edited:

Similar threads

Replies
7
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 8 ·
Replies
8
Views
5K
  • · Replies 2 ·
Replies
2
Views
11K
  • · Replies 13 ·
Replies
13
Views
7K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 7 ·
Replies
7
Views
75K
  • · Replies 13 ·
Replies
13
Views
21K