Results 1 to 18 of 18

Thread: [RESOLVED] Send Message To All (Winsock)

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2008
    Posts
    113

    Resolved [RESOLVED] Send Message To All (Winsock)

    I'm currently building a server/client chat program.

    There's a list of all connected clients in a list box (lstUsers).

    I want to send the data in my text1.text to all of the clients' text1.texts.

  2. #2
    Fanatic Member Mxjerrett's Avatar
    Join Date
    Apr 2006
    Location
    Oklahoma
    Posts
    939

    Re: Send Message To All (Winsock)

    Simple, just loop through.

    Code:
    For i=0 to winsock.ubound
    winsock(i).senddata Text1.text
    next

    If a post has been helpful please rate it.
    If your question has been answered, pull down the tread tools and mark it as resolved.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Feb 2008
    Posts
    113

    Re: Send Message To All (Winsock)

    Thank you

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Feb 2008
    Posts
    113

    Re: [RESOLVED] Send Message To All (Winsock)

    Edit: Fixed.
    Last edited by JoshA56; Jun 30th, 2008 at 11:20 PM.

  5. #5
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: [RESOLVED] Send Message To All (Winsock)

    Removed because I made error in reply

  6. #6
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: [RESOLVED] Send Message To All (Winsock)

    For i=0 to winsock.ubound
    winsock(i).senddata Text1.text
    next


    It's not quite that simple.

    You need to make sure that the client is connected before you send any messages otherwise you are going to get some errors.

  7. #7
    Fanatic Member Mxjerrett's Avatar
    Join Date
    Apr 2006
    Location
    Oklahoma
    Posts
    939

    Re: [RESOLVED] Send Message To All (Winsock)

    I was writing as if it was all of them connected

    For a simple check
    Code:
    For i=0 to winsock.ubound
    If winsock(i).state=7 then
    winsock(i).senddata Text1.text
    end if
    next

    If a post has been helpful please rate it.
    If your question has been answered, pull down the tread tools and mark it as resolved.

  8. #8
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: [RESOLVED] Send Message To All (Winsock)

    You know that. I know that. But maybe he doesn't know that. And then he uses your code. And then a client dissconnects. And then he gets an error. And then he doesn't know why. And then he posts back here asking why. So why not just avoid that in the first place with the correct way of doing it?

  9. #9
    New Member
    Join Date
    Aug 2011
    Posts
    1

    Re: [RESOLVED] Send Message To All (Winsock)

    I have a problem with this code...
    When i use it, my server sends messages, but clients do not receive them right away(when i send a single message to a client(later on) it receives the new message and the mass message that i sent before to everyone.

    there is a solution that is not so effective:i put a - msgbox "message sent" -after the send command...in that case everyone gets the message, but i have to click OK to close the message box for each connected user...and i have about 250+ users...that `s 2 much clicking for me x)

  10. #10
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: [RESOLVED] Send Message To All (Winsock)

    Quote Originally Posted by Mxjerrett View Post
    I was writing as if it was all of them connected

    For a simple check
    Code:
    For i=0 to winsock.ubound
    If winsock(i).state=7 then
    winsock(i).senddata Text1.text
    end if
    next
    Why not just use On Error Resume Next instead of If winsock(i).state=7 then

    Code:
    On Error Resume Next
    
    For i=0 to winsock.ubound
      winsock(i).senddata Text1.text
    next
    
    On Error Goto 0


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  11. #11
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: [RESOLVED] Send Message To All (Winsock)

    Quote Originally Posted by wakawaki View Post
    I have a problem with this code...
    When i use it, my server sends messages, but clients do not receive them right away(when i send a single message to a client(later on) it receives the new message and the mass message that i sent before to everyone.

    there is a solution that is not so effective:i put a - msgbox "message sent" -after the send command...in that case everyone gets the message, but i have to click OK to close the message box for each connected user...and i have about 250+ users...that `s 2 much clicking for me x)
    Since you did not post your code I have no way of knowing how you are handling the messages at the receiving end. I think you are probably having data buffering problems (if you are even buffering at all)

    Using message boxes will not solve your problem since you are only stating what you believe has occured but your problem may be at the client's end or both. Don't use message boxes; if you need to see a textual status you should use a listbox, a label, or anything that you can print to without having to click out of it.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  12. #12
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: [RESOLVED] Send Message To All (Winsock)

    Bearing in mind this thread is over 3 years old and is marked as Resolved I suspect you'll get more attention if you start your own thread describing the problem and showing the code you are using.

  13. #13
    Fanatic Member Mxjerrett's Avatar
    Join Date
    Apr 2006
    Location
    Oklahoma
    Posts
    939

    Re: [RESOLVED] Send Message To All (Winsock)

    Also jmsrickland, using on error resume, is a bad coding practice. You don't ever want to just step through errors unless absolutely necessary.

    If a post has been helpful please rate it.
    If your question has been answered, pull down the tread tools and mark it as resolved.

  14. #14
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: [RESOLVED] Send Message To All (Winsock)

    Not in this case.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  15. #15
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: [RESOLVED] Send Message To All (Winsock)

    Quote Originally Posted by jmsrickland View Post
    Why not just use On Error Resume Next instead of If winsock(i).state=7 then
    Because:

    (1) The If statement more accurately describes to someone reading the code the conditions under which a message will be sent to a socket. 'On Error Resume Next' just informs the reader that errors are to be ignored. It's also easier to add code at a later date to do something if a socket is not connected. (ie add an 'Else' clause.)

    (2) There's also a potential performance impact since the SendData method will be called and the underlying protocols will do some work prior to finding out and reporting the error which will invoke the error handler which in turn will ignore it.

    (3) You are also assuming that the only possible error that could be raised is due to the client socket not being connected and could therefore miss something that would otherwise be important, causing unpredictable results, with no error messages being output, leading to hours of debugging.

    (4) Finally, you are denying access to any other active error handler in the routine to the error condition for reporting /correction.

    So I'd say there are 4 good reasons not to use On Error Resume Next in this situation. (There is (IMHO) a 5th reason, that of, it's 'Good Programming Practice' to never ignore an error)

    In fact, for readability, I'd go one step further:
    Code:
    If winsock(i).State = sckConnected Then
    rather than use '7' (not everyone knows that a .State of 7 = socket is connected), and I'd probably start the loop at 1 rather than 0 as 'normally' element 0 of the control array is the listening socket and will, therefore, never be connected.
    Last edited by Doogle; Aug 19th, 2011 at 01:25 AM. Reason: Typo

  16. #16
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: [RESOLVED] Send Message To All (Winsock)

    The only people that say that are anti On Error Resume Next fanatics.
    Last edited by jmsrickland; Aug 19th, 2011 at 10:22 AM.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  17. #17
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: [RESOLVED] Send Message To All (Winsock)

    .....and your logical argument is ......... ?
    Last edited by Doogle; Aug 19th, 2011 at 11:56 AM.

  18. #18
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: [RESOLVED] Send Message To All (Winsock)

    Are you saying that it is my particular usage that is wrong or are you saying that all usages are wrong?


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width