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

  1. #16
    Registered User
    Join Date
    Nov 2011
    Posts
    35
    Quote Originally Posted by nvoigt View Post
    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?
    No im not sure how to do that hahah :P! I debugged it but i didnt see any difference! I definitely think its the logic in the recieve code thats "inefficient" what do you think about that? Do you think theres a more efficient way of recieving messages and sending them? Because what im doing is recieving one then calling send and sending that message out to all users then recieving another one then calling send and sending it out to all users and repeating that process till all the messages are sent, both methods are using a loop to send to all users. DO you think i should recieve all messages then send them all out at once? or is that less efficient?

  2. #17
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    I think you should start to pinpoint where your delay is coming from. If you say you have a visible delay, then find out where it is. Print to console and observe where it's lagging. I'm really at a loss how to help you if you didn't even try the simplest debugging technique for three straight days.
    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. #18
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Quote Originally Posted by shivam1992 View Post
    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
    If you specify a nonzero value for the first parameter of poll, it isn't nonblocking anymore - it blocks for a length of time up to the time you specified in Poll().

    Lastly, there's not much reason to loop over a socket list and calling Poll() - use Socket.Select() instead, which is much more efficient at polling a set of sockets.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  4. #19
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Quote Originally Posted by shivam1992 View Post
    DO you think i should recieve all messages then send them all out at once? or is that less efficient?
    Much less efficient unless you expect a huge, huge volume of traffic. Think about it like this - if the main loop is working properly, the time from a message coming in until the message is dispatched to all other clients should be on the order of milliseconds. How likely is it for two clients to both send a message within milliseconds of each other versus how likely is it that they won't?

    I think your problem is likely to be the blocking code you have. Of the snippets you've posted, you've demonstrated a 100 second long block in your main loop, plus a n-second long block in receiving (where n is the number of connections). There could well be blocks in checkForDisconnections() as well.

    If you're going to do this in the current design (check for disconnections, messages, and accept connections in a single loop), every poll() everywhere in the code should have a timeout of 0 so it's nonblocking. Any block anywhere in the code has the potential to stop your entire main loop until that block times out.

    There are some other designs you could consider too:

    1. To split the three main functions into separate threads, and block indefinitely on each thread until it has something to do. You'd need to handle thread synchronization on your list of sockets, so there's some overhead in terms of locking, but from a multitasking operating system point of view, blocking until an event is better than polling.

    2. To use event-driven programming via use of the BeginAccept(), BeginReceive(), etc. methods, and use this to notify you when you need to do something. It still needs to be done in a thread-safe manner, but the framework would handle most of the details, except for thread safety on shared data such as the socket list.
    Last edited by Cat; 10-11-2012 at 12:25 AM.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  5. #20
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    I would use the asynchronous versions of the socket methods and not the blocking versions. You could try the AsParallel but I doubt it would yield anything different than the async versions. Both the async and AsParallel approach are better than the blocking approach. Note that AsParallel is not going to magically fix all threading issues.

    C# makes this quite simple. I think my vote would be what Cat said and use the event driven non-blocking async methods and the appropriate lock mechanism to ensure it is all thread safe. It has been some time since I have done any of this in C# but if I remember correctly it did not take but a few hours to get it all working.

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, 08:20 AM
  2. Send ACK message
    By daghenningsorbo in forum C Programming
    Replies: 7
    Last Post: 11-07-2009, 12:14 PM
  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, 07: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, 08: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