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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:23:26+00:00 2026-05-27T05:23:26+00:00

My task is to implement a two player game played between two computers connected

  • 0

My task is to implement a two player game played between two computers connected via TCP. One of the requirement is that only the winner is given the choice play again or not. In case the server wins and decides not to play further, the client should restart as a server and accept new connections.

My approach:
If game LOST (in client mode), close sockfd and recreate another one. Then use setsockopt to allow rebinding using SO_REUSEADDR, then calling bind.

int yes = 1;
if ( setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1 )
{
    perror("setsockopt");
}

if ( bind(sockfd, (struct sockaddr*)&svr, sizeof(svr) ) == -1 )
{
    perror("server: bind");
}

But still, I’m getting the same “Address already in use” error. I’ve tried sleeping for 150 seconds before recreating the socket and this method works.

NOTE: I’m testing this on the same PC. It may work on two linked PCs but its a necessity to make it work on the same PC. Please help.

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

    Since you are running this on the same system, it sounds like you have a race condition. The client is trying to bind() the socket just before the server has closed it (assuming both server and client are setting SO_REUSEADDR on their sockets).

    You need to implement some kind of handshake that allows the server to inform the client after it has closed the listening socket – perhaps the server should close the listening socket before it closes the active socket from the last game?

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

Sidebar

Related Questions

I am attempting to implement xmpp chat/collaboration between two C# desktop clients that exchange
Assume that you're working a x86 32-bits system. Your task is to implement the
I need to implement something. Something that could do some certain task in my
I have two tables one to store a task and one to evaluate the
I have such task - to create control that union two controls (DataGrid from
Task: implement paging of database records suitable for different RDBMS. Method should work for
Simple task like make AJAX request , pass one parameter and return result, can
Imagine I have two (three, four, whatever) tasks that have to run in parallel.
I have two systems I'd like to integrate, one which uses a completely in-house
Just preparing myself for a task that i feel could be quite troublesome, and

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.