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

  • SEARCH
  • Home
  • 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 4333944
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T10:27:42+00:00 2026-05-21T10:27:42+00:00

I have a problem with a c++ socket. I’m using CAsyncSocket from MFC that

  • 0

I have a problem with a c++ socket.
I’m using CAsyncSocket from MFC that i want to join a multicast group.
Also I need to have multiple listener on this group and here is where i get in trouble.
I found some examples on the web but it doesn’t seems to work.
Here is my code:

//create socket on port 17233   
BOOL bRet = Create(17233,SOCK_DGRAM, FD_READ);

//set reuse socket option  
BOOL bMultipleApps = TRUE;
bRet = SetSockOpt(SO_REUSEADDR, (void*)&bMultipleApps, sizeof(BOOL), SOL_SOCKET);

//join multicast group
ip_mreq m_mrMReq;           // Contains IP and interface of the host group
m_mrMReq.imr_multiaddr.s_addr = inet_addr((LPCSTR)"224.30.0.1");    /* group addr */ 
m_mrMReq.imr_interface.s_addr = htons(INADDR_ANY);      /* use default */

int uRes =setsockopt(m_hSocket, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char FAR *)&m_mrMReq, sizeof(m_mrMReq));

There are no errors when i run this.
But when i try to run another instance of the app it fails to create a new socket on that port because the port is in use.

I have done this in C# and it worked fine like this:

IPEndPoint ipep = new IPEndPoint(IPAddress.Any, port);
s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);
s.Bind(ipep);
IPAddress ip = IPAddress.Parse(mcastGroup);
s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(ip, IPAddress.Any));
s.SetSocketOption(SocketOptionLevel.IP,SocketOptionName.MulticastTimeToLive, int.Parse("1"));

So if any body sees a problem with my code or have some tips i will gladly appreciated.

EDIT 1:
Is CAsyncSocket a TCP socket?

EDIT 2:
After reading Can two applications listen to the same port?
I think i made a confusion. I need a Multicast UDP port that can be access by multiple application using SO_REUSEADDR

Edit for Clarification:

BOOL bRet = Create(17233,SOCK_DGRAM, FD_READ)

Creates an UDP socket and bind’s to to port 17223.
For SetSockOpt(SO_REUSEADDR, (void*)&bMultipleApps, sizeof(BOOL), SOL_SOCKET); to work you need to set it before binding as @Hasturkun said.
The final working code looks like this:

    BOOL bRet = Socket(SOCK_DGRAM, FD_READ);
    if(bRet != TRUE)
    {
        UINT uErr = GetLastError();
        std::cout<<"Error:"<<uErr<<std::endl;
        return FALSE;
    }else{
        std::cout<<"Create sock: OK"<<std::endl;
    }

    //add reuse
    BOOL bMultipleApps = TRUE;      /* allow reuse of local port if needed */
    SetSockOpt(SO_REUSEADDR, (void*)&bMultipleApps, sizeof(BOOL), SOL_SOCKET);

    //bind
    bRet = Bind(17233, NULL);
    if(bRet != TRUE)
    {
        UINT uErr = GetLastError();
        std::cout<<"Error(BIND):"<<uErr<<std::endl;
    }else{
        std::cout<<"BIND sock: OK"<<std::endl;
    }

Thanks,
Gabriel

;

  • 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-05-21T10:27:43+00:00Added an answer on May 21, 2026 at 10:27 am

    You should be able to separate the creation of the socket from the binding, create the socket using Socket, eg.

    BOOL bRet = Socket(SOCK_DGRAM, FD_READ);
    

    Then bind it with Bind after setting the sockopt

    BOOL bMultipleApps = TRUE;
    bRet = SetSockOpt(SO_REUSEADDR, (void*)&bMultipleApps, sizeof(BOOL), SOL_SOCKET);
    
    bRet = Bind(17233, NULL);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have strange problem with receiving data from socket. On client im using air
I have this problem.. I'm receiving data from a client..Using a socket connection. But
I have a problem using DatagramSocket. The problem is that I can't run two
I have a problem with a socket library that uses WSAASyncSelect to put the
I have a problem using a socket: socket = new Socket(hostname, port); os =
i have the problem when i want to try web socket technology on my
I have a problem with sending an array using socket.io. The client creates an
I have a problem that when dowloading a image from flickr.com,the python function urllib.urlretrieve()
I have problem with receiving an image over TCP socket [.net 4.0] Server: Socket
I have one problem, I learn how work with socket and I write programm

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.