C - Print connecting clients address

  • Thread starter Thread starter James889
  • Start date Start date
AI Thread Summary
The discussion focuses on troubleshooting a client-server program in C, specifically a function designed to print the addresses of connecting peers. The function encounters a core dump, prompting the need for advice. Key issues identified include the handling of pointers and memory allocation. The pointer `ptr` is correctly initialized with `malloc`, but there is confusion regarding the use of `&(p->ai_addr)`, which is problematic since `ai_addr` is already a pointer. It is suggested to remove the ampersand to avoid passing a pointer to a pointer. Additionally, best practices are emphasized, such as checking pointer values, initializing structures to NULL, and using debug statements for error tracking. Overall, the discussion highlights the importance of careful pointer management and debugging in C programming.
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:
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Back
Top