Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8757761
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:22:50+00:00 2026-06-13T14:22:50+00:00

I am using UDP sockets to connect many clients to one server. After recvfrom()

  • 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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T14:22:51+00:00Added an answer on June 13, 2026 at 2:22 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am supposed to connect to external server using UDP sockets in C#.. I
I am implementing a really basic server-client model in Java, by using UDP sockets
I've written a simple UDP server using Netty. The server listens on one port
I am trying to setup a simple UDP client and server using Ruby. The
I'm currently using Socket class on both server and client to establish udp connection,
I need a UDP server that can communicate with many clients. My current thinking
I am writing simple client-server program. Client send some messages to server using UDP
I'm writing a program to communicate with certain patient monitor using connection-less (UDP) sockets.
I am writing a program in java using sockets with UDP. For a number
I tried to make an asynchronous UDP client using boost::asio I get some code

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.