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 7775683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T17:41:45+00:00 2026-06-01T17:41:45+00:00

I am currently working on a UDP application that allows two users to talk

  • 0

I am currently working on a UDP application that allows two users to talk to eachother using the winsock librarary. Currently when my program runs, it first saves the sockaddress to a vector and then when the user sends a message it compares the address to the first address in the vector. When I debug and compare the values being compared they are exactly the same yet my if statment goes to the else (it thinks that the addresses dont equal each other)

this is the code I have:

#include <WinSock2.h>

sockaddr    clientAddress;

recvfrom( hSocket, msg, MAXLINE, 0, &clientAddress, &cbClientAddress );

myVector.pushback(clientAddress);

if (&clientAddresses[0] == &clientAddress)
{
//is the same address
}
else
{
//not the same address
}

Ive also tried being more specific with using .sa_data after the clientAddress[0] and &clientAddress.

Thanks

  • 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-01T17:41:47+00:00Added an answer on June 1, 2026 at 5:41 pm

    Multiple issues:

    • Before the call to recvfrom, you need to initialize cbClientAddress (of type socklen_t) to the number of bytes of address information you are ready to receive, like this:

       struct sockaddr clientAddress;
       socklen_t cbClientAddress;
      
       cbClientAddress = sizeof(clientAddress);
       recvfrom(hSocket, msg, MAXLINE, 0, &clientAddress, &cbClientAddress);
      
    • After the call, cbClientAddress will be overwritten with the actual length of the address that was received. This will be shorter than sizeof(struct sockaddr). In fact it will be equal to sizeof(struct sockaddr_in) because this is a UDP/IP socket.

    • You must compare only the part of the structure that actually contains data, not the whole structure. The unused portion of the structure (the difference in size between struct sockaddr_in and struct sockaddr) might be garbage. You don’t want to compare it. This will necessitate not only memorizing the contents of the structure itself in a vector, but also the significant length.

    • When comparing the saved address against the one you’ve just received, use this kind of pseudocode. Don’t try to compare the whole structure (including trailing unused portions).

      if (
          (saved_length == this_length) &&
          (memcmp(saved_sockaddr, this_sockaddr, this_length) == 0)
      ) {
          it's a match
      }
      
    • Your code &clientAddresses[0] == &clientAddress checks if the addresses of the structures are equal. This means you’re testing to see if it’s the same structure instead of what you want, which is testing if it’s a structure with the same contents. Use memcmp as per the pseudocode above to compare the contents.

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

Sidebar

Related Questions

Am currently working on an application that requires users to submit posts and comments
I am currently working on a basic send and receive program using UDP in
im currently working in a simple program that implements plugins whith dll libraries (using
I'm working on a project that discovers/configures remote devices using UDP broadcasts. These devices
I'm currently developing application using DirectSound for communication on an intranet. I've had working
I'm currently working on a UDP socket application and I need to build in
Were currently working on a script that reads the current users information from out
Currently working on an ASP.NET MVC 3.0 application and using FormsAuthentication . When the
Im currently working on an RMI client that will talk to an RMI server
Currently working on a bespoke ecommerce application using Codeigniter. I have a products table

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.