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.
Printable View
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.
Simple, just loop through.
Code:For i=0 to winsock.ubound
winsock(i).senddata Text1.text
next
Thank you :)
Edit: Fixed.
Removed because I made error in reply
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.
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
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?
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.
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.
Also jmsrickland, using on error resume, is a bad coding practice. You don't ever want to just step through errors unless absolutely necessary.
Not in this case.
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:
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.Code:If winsock(i).State = sckConnected Then
The only people that say that are anti On Error Resume Next fanatics:D.
.....and your logical argument is ......... ? :)
Are you saying that it is my particular usage that is wrong or are you saying that all usages are wrong?