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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T13:14:25+00:00 2026-06-03T13:14:25+00:00

I am currently wrapping BSD sockets for the first time and also unit test

  • 0

I am currently wrapping BSD sockets for the first time and also unit test my results along the way. Anyways I came across a problem while writing a simple test to test my Acceptor and TcpSocket class which is related to reusing the local host address, i.e.

Pseudocode:

//server thread
{
    //binds, listens and accepts on port 50716 on localhost
    TcpAcceptor acceptor(Resolver::fromService("50716"));
    //i get a ECONNREFUSED error inside the accept function when trying to create newSock
    TcpSocket newSock = acceptor.accept();
}

//connect in the main thread
TcpSocket connectionSocket(Resolver::resolve(Resolver::Query("localhost", "50716")));

Is it even possible to listen and connect on the same host/port? Is there any way to run a simple client/server test on the same machine/host?

Thanks!

EDIT:

Cool, things work now! Just for reference, I also noticed in the process that you don’t even need to use a thread, even if you use blocking sockets to perform a simple test, if you decouple listen from accept like this:

//server socket
TcpAcceptor acceptor;
acceptor.bind(Resolver::fromService("0"));
acceptor.listen();

//client socket, blocks until connection is established
TcpSocket clientSock(SocketAddress("127.0.0.1", acceptor.address().port()));

//accept the connection, blocks until one accept is done
TcpSocket connectionSock = acceptor.accept();

//send a test message to the client
size_t numBytesSent = connectionSock.send(ByteArray("Hello World!"));

//read the message on the client socket
ByteArray msg(12);
size_t bytesReceived = clientSock.receive(msg);
std::cout<<"Num Bytes received: "<<bytesReceived<<std::endl;
std::cout<<"Message: "<<msg<<std::endl;

building the tests like this allows for nice and simple test cases even for the blocking functions.

  • 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-03T13:14:27+00:00Added an answer on June 3, 2026 at 1:14 pm

    Yes, it’s possible. There is no such restriction that a server and a client must be different processes. One thread can open/listen a socket and other thread can connect to it.

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

Sidebar

Related Questions

I'm currently having an issue wrapping my brain around the notion of converting my
Currently I'm trying to write my first Python library and I've encountered the following
I'm having a very hard time wrapping my head around this problem (it might
I'm currently writing a monad wrapping OpenGL called GL and I want to be
I'm having a bit of an issue wrapping my head around something. I'm currently
I'm currently wrapping a command line tool ( espeak ) with Tcl/Tk, and I
Currently I'm wrapping up an application at the moment, and I'm at the stage
I'm currently refactoring some code on a project that is wrapping up, and I
Currently, my anythingslider has too many slides to fit in the wrapper without wrapping
I'm working on an input system, wrapping DirectInput and XInput. Currently XInput devices are

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.