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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:09:17+00:00 2026-06-18T08:09:17+00:00

I’m writing a Socket library and I’m having some trouble with client sockets that

  • 0

I’m writing a Socket library and I’m having some trouble with client sockets that bind to a specific address and port. I’m currently testing my winsock TCP library, although I’m sure there’s no reason why I wouldn’t have the same problems on *nix. So far everything works fine (if I connect() without calling bind() first I can send() and recv()).

I have a server listening on 192.168.1.99:XXXX (my machine’s address).

I then get a socket and bind to 192.168.1.99:0. I get a socket error 10049 (WSAEADDRNOTAVAIL) when I try to connect. The same thing happens if I try to bind to 0.0.0.0:0.

If I try to bind to 127.0.0.1:0 or localhost or leave the hostname NULL (if this is even possible, should I be using the AI_PASSIVE flag?) and then connect I get a 10061 (WSACONNREFUSED).

Anyone know the right way to do this here?

EDIT3: Turns out the issue was that I was calling socket before I called bind and then calling it again before I called connect. I switched it to only call socket before calling bind. I don’t know if anyone else is confused by this whole concept, so I’ll leave this here unless someone feels it should be closed?

EDIT 2: Is the issue that I call socket twice?

EDIT: Here’s some of the functionality

int main() {
    WSADATA wsaData;   // if this doesn't work

    if(WSAStartup(MAKEWORD(2,0), &wsaData) != 0) {
        cout << WSAStartup failed << endl;
        return -1;
    }

    ClientSocket* cs = new ClientSocket("192.168.1.99", "XXXX");

    cs->bind("", "0"); //error 10049 (WSAEADDRNOTAVAIL)
    //cs->bind("", "60000"); //error 10049 (WSAEADDRNOTAVAIL)

    //cs->bind("0.0.0.0", "0"); //error 10049 (WSAEADDRNOTAVAIL)
    //cs->bind("0.0.0.0", "60000"); //error 10049 (WSAEADDRNOTAVAIL)

    //cs->bind("192.168.1.99", "0"); //error 10061 (WSACONNREFUSED)
    //cs->bind("192.168.1.99", "60000"); //error 10061 (WSACONNREFUSED)

    //cs->bind("127.0.0.1", "0"); //error 10061 (WSACONNREFUSED)
    //cs->bind("127.0.0.1", "60000"); //error 10061 (WSACONNREFUSED)

    //cs->bind("localhost", "0"); //error 10061 (WSACONNREFUSED)
    //cs->bind("localhost", "60000"); //error 10061 (WSACONNREFUSED)


    cs->connect(); //connects to the info passed in ctor

    Socket* s = cs->getSocket();

    if(s == NULL) {
        cs->close();
        return -1;
    }

    .
    .
    .
    .
}

//these are the methods exposed by my API
int ClientSocket::bind(std::string hostname, std::string port) {
    if(bound) {
        return -1;
    }
    if(connected) {
        return -2;
    }

    //this defines what specialSockOp will do
    ssop = &ClientSocket::BaseDef::bind;

    info->hostname = hostname;
    if(hostname.compare("") == 0) {
        info->AI_FLAG_PASSIVE = true;
    }
    info->port = port;

    //calls SpecialSocket::socket()
    socket();

    bound = true;

    return 0;
}

int ClientSocket::connect() {
    if(connected) {
        return SOCKET_ERROR;
        //throw exception
    }
    if(bound) {
        ssop = &ClientSocket::BaseDef::connect;
    }
    socket();
    return 0;
}

void SpecialSocket::WinImp::socket() {
    struct addrinfo *results = NULL, *p = NULL, hints;
    int rv;

    ZeroMemory(&hints, sizeof(hints));
    hints.ai_family = (containerClass->info->ipType == IP_DUAL ? AF_UNSPEC :
        containerClass->info->ipType == IPV6 ? AF_INET6 : AF_INET);
    hints.ai_socktype = (containerClass->info->protocol == TCP ? SOCK_STREAM : 
        SOCK_DGRAM);
    hints.ai_protocol = 0;
    if(containerClass->info->AI_FLAG_PASSIVE) {
        hints.ai_flags |= AI_PASSIVE;
    }
    if(containerClass->info->AI_FLAG_CANONNAME) {
        hints.ai_flags |= AI_CANONNAME;
    }

    //cStringHostname returns NULL if an empty string has been passed in
    if((rv = getaddrinfo(containerClass->info->cStringHostname(),
         containerClass->info->cStringPort(), &hints, &results)) != 0) {
            //throw exception
    }

    for(p = results; p != NULL; p = p->ai_next) {
        if((containerClass->info->sock_fd = ::socket(p->ai_family,
                 p->ai_socktype, p->ai_protocol)) == INVALID_SOCKET) {
            closesocket(containerClass->info->sock_fd);
            continue;
        }

        //for a ClientSocket specialSockOp will call either bind or connect
        //corresponding to the function called by ClientSocket
        if(containerClass->specialSockOp(p->ai_addr) == SOCKET_ERROR) {
            closesocket(containerClass->info->sock_fd);
            continue;
        }

        break; //successfully connected
    }

    if(p == NULL) {
        //none of the connections in results were good
        //throw exception - could not connect
    }

    freeaddrinfo(results);
}

int SpecialSocket::WinImp::bind(const void* addr) {
    struct sockaddr* ai_addr = (struct sockaddr*)addr;

    return ::bind(containerClass->getSockFd(), ai_addr, sizeof(*ai_addr));

}

int ClientSocket::WinImp::connect(const void* addr) {
    struct sockaddr* ai_addr = (struct sockaddr*)addr;

    int result = ::connect(containerClass->getSockFd(), ai_addr, sizeof(*ai_addr));

    if(result == SOCKET_ERROR) {
        //these are where my errors are showing up
        cout << "error after connect() is - " << WSAGetLastError() << endl;
        return SOCKET_ERROR;
    }

    containerClass->servInfo->sock_fd = containerClass->getSockFd();

    //I separated the functionality here - Socket can only send/recv
    containerClass->s = new Socket();
    //I use getpeername here to get information about the remote socket

    //Long story but basically sets up the Socket
    containerClass->initRWSocket(containerClass->s, containerClass->servInfo);

    return result;
}

A user can then call ClientSocket.getSocket() which returns a Socket that they are then able to send/recv on. In my case, this is returning NULL since the Socket is never instantiated, because SpecialSocket::connect returns before it does that.

  • 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-18T08:09:18+00:00Added an answer on June 18, 2026 at 8:09 am

    You have to bind to a valid local address that is capable of connecting to the target address of connect, i.e. has a route to it. Clearly in this case:

    • 192.168.1.99 isn’t a local address, it is a remote address
    • 127.0.0.1 is a local address but you can only reach 127.0.0.1 from it, not any remote addresses
    • 0.0.0.0 is a valid bind-address for a listening socket but not for an active (outbound) socket.

    This is one of several reasons why binding active sockets is a bad idea: in a multi-homed host you have to iterate over the local addresses to choose a suitable one, i.e. replicate the routing that TCP would do for you automatically if you didn’t do the bind() at all.

    The explanation given in your edit doesn’t agree with the errors you got.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported

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.