0

I am using UDP sockets to connect many clients to one server. After recvfrom()ing a client's login packet, I store their struct addrinfo * in an array like so:

struct addrinfo * userAddrs[64];

recvfrom(sockfd, req, 4096, 0, serverAddr->ai_addr, &serverAddr->ai_addrlen);
userAddrs[userindex] = (struct addrinfo *) malloc(sizeof(struct addrinfo));
memcpy(userAddrs[userindex], serverAddr, sizeof(struct addrinfo));

Then the the server receives messages from clients and sends it to everyone on that channel:

// user x channel matrix. 1 means listening on that channel.
int userchannel_matrix[64][64];
for(int i = 0; i < 64; i++){
    if(userchannel_matrix[i][channelindex] == 1){
       sendto(sockfd, &textsay, sizeof (struct text_say), 0, userAddrs[i]->ai_addr, userAddrs[i]->ai_addrlen);
    }
}

However, this ends up sending all of the messages to one client. Ex: If three clients are subscribed to channel 4 and one of them sends a message, that client will receive all 3 messages instead of each client receiving one. What am I doing wrong here?

6
  • 1
    Shouldn't you be using the sockaddr family of structures? struct addrinfo is for use with getaddrinfo only.
    – goji
    Oct 28, 2012 at 23:56
  • I used addrinfo for my client to send messages to the server and it worked fine. My server can also send messages to the clients. Oct 28, 2012 at 23:59
  • 2
    Well, there must be something going wrong with your userAddrs array and the data it contains. Have you tried printing out the ip/ports in presentation format and seeing what the go is? Also in the code sample you've provided, I don't see userindex being incremented. Is this being done 'outside' the sample?
    – goji
    Oct 29, 2012 at 0:02
  • Is there an easy way to print those out, or do I have to go grab each individual value for a printf()? Also yes, userindex is found based on the IP and port of the client, by looking at the serverAddr from the call to recvfrom(). Oct 29, 2012 at 0:05
  • 1
    Use inet_ntop and ntohs like: stackoverflow.com/questions/13105713/… Altho this uses some C++ and is ipv4 specific, you get the idea.
    – goji
    Oct 29, 2012 at 0:08

2 Answers 2

2

The basic problem is that struct addrinfo contains a pointer to a struct sockaddr, which you have clearly only initialized once. So every recvfrom() overwrites the same piece of sockaddr data (addrinfo.ai_addr), and every userAddr[] item points to the same one.

As @Troy suggested, you should be using struct sockaddr. Or else allocate all the addrinfo structures in the array first,, each with its own struct sockaddr *ai_addr pointers.

1
  • Found this out as soon as you posted it, I forgot that there were pointers even within the addrinfo struct... thanks! Oct 30, 2012 at 3:07
1

In addition to what others said, what you are implementing is basically a manual multicast, so why not just use an actual multicast instead? That way, your server can `sendto() a single message to a single multicast IP group, and the OS will handle sending copies of the message to every client that has subscribed to that multicast IP group for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.