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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:44:49+00:00 2026-05-17T20:44:49+00:00

I have around 5000 modem (thin clients), and I want to communicate with them,

  • 0

I have around 5000 modem (thin clients), and I want to communicate with them, one of a my method is like this : string GetModemData(modemID), now I have an open port in server that listens to modem and I’m using socket programming to send data to modems (calling related function), but when i want send data to multiple modem in a same time and get response from them, I don’t know what should i do? I can send data to one modem and waiting for its response and then send another data to other modems (sequential), but the problem is client should be wait long time to get answer(may be some different client want to get some information from modems so they all will be wait into the Q or something like this), I think one way to solving this problem is to use multiple port and listen for each modem to related port, but it takes too many ports and also may be memory usage going up and exceed my available memory space, so some lost may be occurred (is this true?). what should to do ? I’d thinking about Parallelism, but i think its not related i should to wait for one port, because i don’t know should to pass current received data to which client. I’m using asp.net.

currently I’m doing like this:

private void StartListener()
    {
        ModemTcpListener = new TcpListener(ModemPort);
        //ClientTcpListener = new TcpListener(ClientPort);

        ModemTcpListener.Start();
        ModemTcpListener.BeginAcceptTcpClient(new AsyncCallback(DoAcceptModemCallback), ModemTcpListener);
    }

and in return

private void DoReadModemCallback(IAsyncResult ar)
         {
             try
             {
                 bool bRet = ar.AsyncWaitHandle.WaitOne(420000);
                 Modem modem = ar.AsyncState as Modem;
                 if (!bRet || modem == null)
                 {
                     return;
                 }
           }
           catch{}
            // now send data to which client?????? if i'm going to use async????
}

and :

private void DoAcceptModemCallback(IAsyncResult ar)
        {
            try
            {
                ModemTcpListener.BeginAcceptTcpClient(new AsyncCallback(DoAcceptModemCallback), ModemTcpListener);
                TcpClient tcpClient = ModemTcpListener.EndAcceptTcpClient(ar);
                Modem modem= new Modem(tcpClient, "");
                tcpClient.GetStream().BeginRead(modem.Buffer, 0, tcpClient.ReceiveBufferSize, new AsyncCallback(DoReadModemCallback), modem);
                ModemTcpListener.BeginAcceptTcpClient(new AsyncCallback(DoAcceptModemCallback), ModemTcpListener);
                Log.Write("a Modem connect ...");
            }
            catch (Exception ex) 
            {
            }
        }
  • 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-17T20:44:50+00:00Added an answer on May 17, 2026 at 8:44 pm

    Heres an example keeping track of all your clients. I’ve compacted it for readability. You should really split it up into multiple classes.

    I’m using Pool (which I just created and commited) and SimpleServer. Both classes are part of a library that I’m currently building (but far from done).

    Don’t be afraid of having 5000 sockets open, they do not consume much resources when you are using asynchronous operations.

        public class SuperServer
        {
            private List<ClientContext> _clients = new List<ClientContext>();
            private SimpleServer _server;
            private Pool<byte[]> _bufferPool;
    
            public SuperServer()
            {
                // Create a buffer pool to be able to reuse buffers
                // since your clients will most likely connect and disconnect
                // often.
                //
                // The pool takes a anonymous function which should return a new buffer.
                _bufferPool = new Pool<byte[]>(() => new byte[65535]);
            }
    
            public void Start(IPEndPoint listenAddress)
            {
                _server = new SimpleServer(listenAddress, OnAcceptedSocket);
    
                // Allow five connections to be queued (to be accepted)
                _server.Start(5); 
            }
    
            // you should handle exceptions for the BeginSend
            // and remove the client accordingly.
            public void SendToAll(byte[] info)
            {
                lock (_clients)
                {
                    foreach (var client in _clients)
                        client.Socket.BeginSend(info, 0, info.Length, SocketFlags.None, null, null);
                }
            }
    
            // Server have accepted a new client.
            private void OnAcceptedSocket(Socket socket)
            {
                var context = new ClientContext();
                context.Inbuffer = _bufferPool.Dequeue();
                context.Socket = socket;
    
                lock (_clients)
                    _clients.Add(context);
    
                // this method will eat very few resources and
                // there should be no problem having 5000 waiting sockets.
                context.Socket.BeginReceive(context.Inbuffer, 0, context.Inbuffer.Length, SocketFlags.None, OnRead,
                                            context);
            }
    
            //Woho! You have received data from one of the clients.
            private void OnRead(IAsyncResult ar)
            {
                var context = (ClientContext) ar.AsyncState;
                try
                {
                    var bytesRead = context.Socket.EndReceive(ar);
                    if (bytesRead == 0)
                    {
                        HandleClientDisconnection(context);
                        return;
                    }
    
                    // process context.Inbuffer here.
                }
                catch (Exception err)
                {
                    //log exception here.
                    HandleClientDisconnection(context);
                    return;
                }
    
                // use a new try/catch to make sure that we start
                // read again event if processing of last bytes failed.
                try
                {
                    context.Socket.BeginReceive(context.Inbuffer, 0, context.Inbuffer.Length, SocketFlags.None, OnRead,
                                                context);
                }
                catch (Exception err)
                {
                    //log exception here.
                    HandleClientDisconnection(context);
                }
            }
    
            // A client have disconnected.
            private void HandleClientDisconnection(ClientContext context)
            {
                _bufferPool.Enqueue(context.Inbuffer);
                try
                {
                    context.Socket.Close();
                    lock (_clients)
                        _clients.Remove(context);
                }
                catch(Exception err)
                {
                    //log exception
                }
            }
    
    
            // One of your modems
            // add your own state info.
            private class ClientContext
            {
                public byte[] Inbuffer;
                public Socket Socket;
            }
    
        }
    

    Used classes:

    • Pool: http://fadd.codeplex.com/SourceControl/changeset/view/58858#1054902
    • SimpleServer: http://fadd.codeplex.com/SourceControl/changeset/view/58859#1054893
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have around 400 GB Live mysql Databases on one server and I like
I have a gird with user data around like 5000 users in the grid.
If I have a single array that has around 5000 elements. I want 50
I have around 100 models, from MCMCglmm, that give output similar to this: >
I have around 100 PNGs (that I created) that I want to create an
I have around 100 rows of text that I want to tokenize, which are
I have around 40 aspx pages in my website. I want to use a
Here's an interesting problem: I have some jQuery that looks like this: $(document).ready(function() {
I have a excel sheet which has around 5000 rows in it.I have used
I have a simple question. I have a few files, one file is around

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.