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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:12:59+00:00 2026-05-28T19:12:59+00:00

I have created a windows service socket programme to lisen on specific port and

  • 0

I have created a windows service socket programme to lisen on specific port and accept the client request. It works fine.

protected override void OnStart(string[] args)
    {

      //Lisetns only on port 8030          
       IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Any, 8030);

      //Defines the kind of socket we want :TCP
       Socket  serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

        //Bind the socket to the local end point(associate the socket to localendpoint)
            serverSocket.Bind(ipEndPoint);

            //listen for incoming connection attempt
            // Start listening, only allow 10 connection to queue at the same time
            serverSocket.Listen(10);

           Socket handler = serverSocket.Accept();

    }

But I need the service programme to listen on multiple port and accept the client request on any available port.

So I enhanced the application to bind to port 0(zero), so that it can accept the request on any available port.

But then I got the error 10061

No connection could be made because the target machine actively refused it.

I am unable to know whats the reason of getting this error.

Can anybody please suggest the way to enhance the code to accept the request on any port.

But the client need to send request to connect to specific port. e.g client1 should connect to port 8030, client2 should connect to port 8031.

  • 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-28T19:13:00+00:00Added an answer on May 28, 2026 at 7:13 pm

    So I enhanced the application to bind to port 0(zero), so that it can accept the request on any available port.

    Wrong. 0 means that the OS should assign a port. A server can only listen at one port at a time. The listen socket just accepts new connections.

    The new connection will have the same local port, but the combination of Source (ip/port) and destination (ip/port) in the IP header is used to identify the connection. That’s why the same listen socket can accept multiple clients.

    UDP got support for broadcasts if that’s what you are looking for.

    Update:

    A very simplified example

      Socket client1 = serverSocket.Accept(); // blocks until one connects
      Socket client2 = serverSocket.Accept(); // same here
    
      var buffer = Encoding.ASCII.GetBytes("HEllo world!");
      client1.Send(buffer, 0, buffer.Count); //sending to client 1
      client2.Send(buffer, 0, buffer.Count); //sending to client 2
    

    Simply keep calling Accept for each client you want to accept. I usually use the asynchronous methods (Begin/EndXXX) to avoid blocking.

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

Sidebar

Related Questions

I have already created a windows service that monitors the network for some specific
I have created a windows service in c# .Net 4.0. In VS it works
I have created a Windows Service in VB.net for building xML from a database.
I have created a C# Windows service but it fails to start. I get
I have created a simple windows service that periodically checks a remote database via
We have created a WCF service hosted in a windows service that handles Authentication
I have created an installation package using Wix which installs a Windows service on
Currently, I have a project with a Windows Service. I also created another Setup
I have created windows service project in vs2008. I have created simple serivce project
I have created a Windows Service (called MuskOx) which runs on my development machine

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.