Like Tree1Likes

Need help finding Fast Way to send a message to multiple sockets in a list object?

This is a discussion on Need help finding Fast Way to send a message to multiple sockets in a list object? within the C# Programming forums, part of the General Programming Boards category; Im making a chat application, i already finished it but sometimes the messages are REALLY delayed for some clients. I ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    35

    Need help finding Fast Way to send a message to multiple sockets in a list object?

    Im making a chat application, i already finished it but sometimes the messages are REALLY delayed for some clients. I have two methods, one called checkForMessages that checks the message on the sockets, and sendMessages which sendsthe message to all sockets. I have a list object of sockets that keeps trakc of the connections.

    Here is my code for both methods
    (this is the server code btw)

    Code:
    public void checkForMessages(List<Socket> s)
            {
                Byte[] byteBuffer = new Byte[1024];
                bool result;
                for (int i = 0; i < s.Count; ++i)
                {
                    result = s[i].Poll(1000, SelectMode.SelectRead);
                    if (result)
                    {
                        Array.Clear(byteBuffer, 0, byteBuffer.Length);
                        s[i].Receive(byteBuffer);
                        sendMessages(s,byteBuffer);
    
                    }
                }
            }
    
    
    
    public void sendMessages(List<Socket> s,Byte[] bufferMessage)
            {
                for (int i = 0; i < s.Count; ++i)
                {
                        Socket sendMessage = s[i];
                        sendMessage.Send(bufferMessage);
                }
    
    
    
            }
    Is there any other way i can send/recieve messages without it being so delayed sometimes?!
    Last edited by shivam1992; 10-04-2012 at 07:39 AM.

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    Your sending could be optimized to do it in parallel:

    Code:
        public void sendMessages(List<Socket> s, Byte[] bufferMessage)
        {
          s.AsParallel().ForAll( socket => socket.Send(bufferMessage) );
        }
    However, this will probably not be your delay. Check your receiving function. It will poll each socket for a second. Add 10 chatters and your program takes 10 seconds to recognize that one has sent something even before replicating it to it's clients.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Location
    england
    Posts
    209
    Just guessing here, but the delay could be from blocking on receive. My guess is based on you using Send rather than BeginSend.

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    35
    Quote Originally Posted by nvoigt View Post
    Your sending could be optimized to do it in parallel:

    Code:
        public void sendMessages(List<Socket> s, Byte[] bufferMessage)
        {
          s.AsParallel().ForAll( socket => socket.Send(bufferMessage) );
        }
    However, this will probably not be your delay. Check your receiving function. It will poll each socket for a second. Add 10 chatters and your program takes 10 seconds to recognize that one has sent something even before replicating it to it's clients.
    Yeah its definitely my receive code, What would be a solution for this?

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    35
    Quote Originally Posted by theoobe View Post
    Just guessing here, but the delay could be from blocking on receive. My guess is based on you using Send rather than BeginSend.
    im using a non blocking method by polling the socket so that cant be it

  6. #6
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    Well, you told your "Poll" to wait for 1000. If that's too long, you could try 100 or 10 or 1 I guess. If you feel adventurous, you could try to receive in parallel, too.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    35
    Quote Originally Posted by nvoigt View Post
    Well, you told your "Poll" to wait for 1000. If that's too long, you could try 100 or 10 or 1 I guess. If you feel adventurous, you could try to receive in parallel, too.
    okay i reduced the time but i still get a delay, that's odd, hmmm,the delay is random, sometimes it is delayed other times it isn't. Could it be because in my receive code i'm receiving a message then sending it to all clients, then receiving another message then sending it, could that slow it down? Could i just add all the received messages in a queue and then call send after all the messages are received? and send the contents of the queue out to the clients?
    Last edited by shivam1992; 10-04-2012 at 09:32 AM.

  8. #8
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    How many clients do you have, how long is your message and how long is the delay? A delay of a few seconds would be normal for simple code if you have a lot of clients (>1000) or a big message (some megabytes) or very short delays in debug mode (< 1s). If you are sending "hello world" between 10 clients, no delay should be visible.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  9. #9
    Registered User
    Join Date
    Nov 2011
    Posts
    35
    Quote Originally Posted by nvoigt View Post
    How many clients do you have, how long is your message and how long is the delay? A delay of a few seconds would be normal for simple code if you have a lot of clients (>1000) or a big message (some megabytes) or very short delays in debug mode (< 1s). If you are sending "hello world" between 10 clients, no delay should be visible.
    Well even with one client its delayed by a few seconds, sometimes its like a 10 second delay and thats just for one client connected, the delay is so random, its definitely in my recieve code, should i do what i said in my post about the queue? would that speed it up?

  10. #10
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    If it's actually in the seconds-range, maybe writing logs or debugging will help. Find out what happens in the time of the delay. How do you know when to call your receive function by the way? Is that some sort of timer or just an endless loop?
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  11. #11
    Registered User
    Join Date
    Nov 2011
    Posts
    35
    Quote Originally Posted by nvoigt View Post
    If it's actually in the seconds-range, maybe writing logs or debugging will help. Find out what happens in the time of the delay. How do you know when to call your receive function by the way? Is that some sort of timer or just an endless loop?
    its in an endless loop, here is the loop, this loop resides in the main function, the previous lines before it just instantiate the class and its attributes, here is the code:
    Client Queue is a list object that holds the connections
    Code:
    while (true)
                    {
                        server.checkForDisconnections(server.ClientQueue);
                        server.checkForMessages(server.ClientQueue);
                        server.PollResult = server.IncomingConnection.Poll(100000, SelectMode.SelectRead);
                        if (server.PollResult)
                        {
                            server.AcceptedConnection = server.IncomingConnection.Accept();
                            server.ClientQueue.Add(server.AcceptedConnection);
                            Console.WriteLine("A client has connected");
                        }
    
                    }

  12. #12
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    What do you think that 100000 in your Poll function call does?
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  13. #13
    Registered User
    Join Date
    Nov 2011
    Posts
    35
    Quote Originally Posted by nvoigt View Post
    What do you think that 100000 in your Poll function call does?
    It waits for a socket to be accepted without the .accepted() call blocking it, i lowered the time for it to literally 1 and i was getting the delay still, i use the poll as a non blocking way so accept doesn't block the entire program

  14. #14
    Registered User
    Join Date
    Nov 2011
    Posts
    35
    Quote Originally Posted by nvoigt View Post
    What do you think that 100000 in your Poll function call does?
    Do you want me to zip up the client and server programs in two seperate zip files and you can run them? and see what im talking about in terms of the "delay"

  15. #15
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    I'd rather help you find it, so can can solve your problems on your own in the future

    Have you tried to track down the delay? Which function takes that long? Have you printed some logs or measured the time of the functions?
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. MPI send message
    By mena samy in forum C Programming
    Replies: 5
    Last Post: 03-05-2011, 07:20 AM
  2. Send ACK message
    By daghenningsorbo in forum C Programming
    Replies: 7
    Last Post: 11-07-2009, 11:14 AM
  3. TCP/IP Sockets in C (problem with send() and recv(): how to loop them)
    By ferenczi in forum Networking/Device Communication
    Replies: 3
    Last Post: 11-18-2008, 06:38 AM
  4. is it possible to send a structure using sockets?
    By kopite in forum Networking/Device Communication
    Replies: 7
    Last Post: 12-03-2003, 07:21 AM
  5. how do i send a message from 1 ip to the other ????
    By yorge in forum C++ Programming
    Replies: 4
    Last Post: 04-18-2002, 03:01 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21