C - Print connecting clients address

  • Thread starter James889
  • Start date
In summary, the conversation is about a novice trying to implement a client server program in C and having trouble with a function to print the connecting peer's address. One person suggests checking for bad values and initializing pointers to 0, while another points out an error in the code with passing a pointer instead of the content of a structure.
  • #1
James889
192
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
  • #2
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:
  • #3
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.
 
  • #4
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:
  • #5

Related to C - Print connecting clients address

1. What is "C - Print connecting clients address"?

"C - Print connecting clients address" is a function in the C programming language that allows you to print the address of a client that is connected to a specific server. This can be useful in networking applications to identify and communicate with connected clients.

2. How do I use the "C - Print connecting clients address" function?

To use this function, you will need to include the header file in your C program. Then, you can use the getpeername() function to retrieve the address of the connected client. This function takes in the socket file descriptor and a struct to store the address information.

3. Can this function be used for both IPv4 and IPv6 addresses?

Yes, the "C - Print connecting clients address" function can be used for both IPv4 and IPv6 addresses. The getpeername() function supports both address types and will return the appropriate address information based on the socket file descriptor provided.

4. What happens if there are multiple clients connected to the server?

If there are multiple clients connected to the server, you will need to use the accept() function to accept incoming connections and create a new socket for each connected client. Then, you can use the getpeername() function on each individual socket to retrieve the address of each connected client.

5. Is there a way to print the client's address in a human-readable format?

Yes, you can use the inet_ntop() function to convert the address information retrieved by getpeername() into a human-readable format. This function takes in the address family, a pointer to the address information, and a buffer to store the converted address. You can then use printf() to print the converted address in a desired format.

Similar threads

  • Programming and Computer Science
Replies
7
Views
1K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
8
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
2K
  • Computing and Technology
Replies
13
Views
7K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
2
Views
11K
  • Programming and Computer Science
Replies
7
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
Back
Top