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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:21:03+00:00 2026-05-31T23:21:03+00:00

So I tried the following program to make an async server, when its sync

  • 0

So I tried the following program to make an async server, when its sync its easy and i can only speak with the server with 1 connection because the second one isn’t allowed. I make a class Server.cs with my server implementation. For the clinet I use Microsoft Telnet.

Program.cs:

static void Main(string[] args)
    {
        Server myServer = new Server();
        myServer.StartListening();
        Console.ReadKey();
    }

Server.cs

class Server
{
    //Delegates
    delegate void socketConnection();
    socketConnection myConnection;

    //Needed for connections
    public IPEndPoint serverIPEP { get; set; }
    public Socket serverSocket { get; set; }
    public int numberOfConnections { get; set; }
    public List<Socket> activeConnections;

    //Constructors
    public Server()
    {
        serverIPEP = new IPEndPoint(IPAddress.Any, 9080);
        serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        numberOfConnections = 10;
        activeConnections = new List<Socket>(numberOfConnections);
    }

    public Server(IPEndPoint serverIPEP)
    {
        this.serverIPEP = serverIPEP;
        serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        numberOfConnections = 10;
        activeConnections = new List<Socket>(numberOfConnections);
    }

    public Server(Socket serverSocket)
    {
        serverIPEP = new IPEndPoint(IPAddress.Any, 9080);
        this.serverSocket = serverSocket;
        numberOfConnections = 10;
        activeConnections = new List<Socket>(numberOfConnections);
    }

    public Server(IPEndPoint serverIPEP, Socket serverSocket, int numberOfConnections)
    {
        this.serverIPEP = serverIPEP;
        this.serverSocket = serverSocket;
        this.numberOfConnections = numberOfConnections;
        activeConnections = new List<Socket>(numberOfConnections);
    }

    public void StartListening()
    {
        serverSocket.Bind(serverIPEP);
        serverSocket.Listen(numberOfConnections);

        Console.WriteLine("Waiting for connections...");

        //Here I made a loop with the callback :) I don't think its the cleverest implementation
        myConnection = new socketConnection(GetConnections);
        IAsyncResult connectionResult = myConnection.BeginInvoke(callBack, this);

        //connectionResult.AsyncWaitHandle.WaitOne();
    }

    private void GetConnections()
    {
        Socket clientSocket = serverSocket.Accept();
        activeConnections.Add(clientSocket);
    }

    private void callBack(IAsyncResult itfAR)
    {
        Console.WriteLine("Current thread: " + System.Threading.Thread.CurrentThread.ManagedThreadId);
        myConnection.BeginInvoke(callBack, this);
    }

The problem is that when I connect with 2 clients it tells me that they are on the same thread, and I want them to be on separated threads. It tells me:

Waiting for connections...
Current thread: 7
Current thread: 7

Shouldn’t there be another number like thread 7 and then thread 9? Or it happens like this because I connect to the server from the same computer?

  • 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-31T23:21:04+00:00Added an answer on May 31, 2026 at 11:21 pm

    Shouldn’t there be another number like thread 7 and then thread 9? Or it happens like this because I connect to the server from the same computer?

    Delegate.BeginInvoke grabs a thread from the thread pool. The reason you are seeing the same thread Id is because that thread is idle. If the thread was busy then BeginInvoke will grab some other thread. For example:

     private void callBack(IAsyncResult itfAR)
        {
            Console.WriteLine("Current thread: " + System.Threading.Thread.CurrentThread.ManagedThreadId);
            myConnection.BeginInvoke(callBack, this);
            Thread.Sleep(100000);
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have tried the following program for writing the contents into Spreadsheet. I downloaded
I tried following the README file in Ruby 1.9.1 but I can't compile it
I tried the following example program to understand the usage of the makefiles in
So I have the following program: https://github.com/eWizardII/homobabel/blob/master/Experimental/demo_async_falcon.py However, when it's run I only get
I am writing a program that should have the following layout: I've tried this
Tried following the instructions here: How to use Google app engine with my own
I tried following the answer to this question , but could not get xsd.exe
Have you managed to get Aptana Studio debugging to work? I tried following this,
I want all the lines with assert_equal and without amazon. I tried following but
I'm trying to open a pop-up menu from a NSToolbarItem. I tried following this

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.