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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:02:24+00:00 2026-05-14T05:02:24+00:00

I’m binding a client TCP socket to a specific local port. To handle the

  • 0

I’m binding a client TCP socket to a specific local port. To handle the situation where the socket remains in TIME_WAIT state for some time, I use setsockopt() with SO_REUSEADDR on a socket.

It works on Linux, but does not work on Windows, I get WSAEADDRINUSE on connect() call when the previous connection is still in TIME_WAIT.

MSDN is not exactly clear what should happen with client sockets:

[…] For server applications that need to bind multiple sockets to the same port number, consider using setsockopt (SO_REUSEADDR). Client applications usually need not call bind at all—connect chooses an unused port automatically. […]

How do I avoid this?

  • 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-14T05:02:24+00:00Added an answer on May 14, 2026 at 5:02 am

    When you create a socket with socket(), it has only a type and a protocol family. The ideal is to bind() it to a local address:port too.

    The error you mentioned normally happens when the last connection to the same host:port didn’t have a graceful shutdown (FIN/ACK FIN/ACK). In these cases, the socket stays in TIME_WAIT state for a certain period of time (OS dependent, but adjustable).

    What happens then is when you try to connect() to the same host and same port, it uses the default socket’s name/address/port/etc, but this combination is already in use by your zombie socket. To avoid this, you can change the local address:port used to establish the connection by calling bind() after the socket creation, providing the sockaddr struct filled with your local address and a random port.

    int main() {
        int ret, fd;
        struct sockaddr_in sa_dst;
        struct sockaddr_in sa_loc;
        char buffer[1024] = "GET / HTTP/1.1\r\nHost: www.google.com\r\n\r\n";
    
        fd = socket(AF_INET, SOCK_STREAM, 0);
    
        // Local
        memset(&sa_loc, 0, sizeof(struct sockaddr_in));
        sa_loc.sin_family = AF_INET;
        sa_loc.sin_port = htons(LOCAL_RANDOM_PORT);
        sa_loc.sin_addr.s_addr = inet_addr(LOCAL_IP_ADDRESS);
    
        ret = bind(fd, (struct sockaddr *)&sa_loc, sizeof(struct sockaddr));
        assert(ret != -1);
    
        // Remote
        memset(&sa_dst, 0, sizeof(struct sockaddr_in));
        sa_dst.sin_family = AF_INET;
        sa_dst.sin_port = htons(80);
        sa_dst.sin_addr.s_addr = inet_addr("64.233.163.104"); // google :)
    
        ret = connect(fd, (struct sockaddr *)&sa_dst, sizeof(struct sockaddr));
        assert(ret != -1);
    
        send(fd, buffer, strlen(buffer), 0);
        recv(fd, buffer, sizeof(buffer), 0);
        printf("%s\r\n", buffer);
    }
    

    UPDATE: As using a specific local port is a requirement, consider setting SO_LINGER with l_onoff=1 and l_linger=0 so your socket won’t block upon close/closesocket, it will just ignore queued data and (hopefully) close the fd. As a last resort you can adjust the TIME_WAIT delay by changing the value of this registry key (highly discouraged!):

    HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\TcpTimedWaitDelay
    
    • 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 have just tried to save a simple *.rtf file with some websites and
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a French site that I want to parse, but am running into

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.