i modified the code, now can send private message by selecting number of client in comboBox, this is done when i remove the for loop coz if use the for loop, total messages will be sent at once depends on total number of clients connected.
but still have problem that when any client disconnect and reconnect again, private message cannot send anymore.
Any help will be appreciated, thanks in advance. sorry and pls ask again if the explanation is unclear.
here is the code i tried:
void indmsgbtn_Click(object sender, EventArgs e)
{
try
{
string msg = richtxtindmsg.Text;
msg = "Private Admin Message: " + msg + "\n";
byte[] byData = System.Text.Encoding.ASCII.GetBytes(msg);
//Socket workerSocket = null;
// for (int i = 0; i < m_workerSocketList.Count; i++)
//{
Socket workerSocket = (Socket)m_workerSocketList[comboBox1.SelectedIndex];
if (workerSocket != null)
{
if (workerSocket.Connected)
{
try
{
workerSocket.Send(byData);
}
catch (SocketException ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
catch (SocketException se)
{
MessageBox.Show(se.Message);
}
}
Sam HobbsPosted Dec 4, 2011, 12:45 PM
If there is a way to asynchronously determine that a client disconnects, then you could use a handler to remove that client from the combobox. You probably need to use an Invoke somehow to update the form from the event handler.
Something else you could do is to determine if a client is still connected before trying to send something to it. You could do that at least once in indmsgbtn_Click.
My guess is that you should do both, but I do not know how to do either. I think there are articles in this web site that will help.
haan jaePosted Dec 4, 2011, 1:33 AM
Any idea why? can it be solved? Thanks for reply and sorry for such long explanation
Sam HobbsPosted Dec 3, 2011, 3:31 PM
You say "cannot send anymore"; why not? What happens? Is there an error message?