'Argument out of range was unhandled' error occur when button is clicked due to no number or no such index number of client connected is selected. Error show to this code:
workerSocket = (Socket)m_workerSocketList[comboBox1.SelectedIndex];
i want to give a message box say 'Please select a correct number'. what should be coded to handle this error?
here is the code:
void indmsgbtn_Click(object sender, EventArgs e)
{
string msg = richtxtindmsg.Text;
msg = "Private Admin Message: " + msg + "\n";
byte[] byData = System.Text.Encoding.ASCII.GetBytes(msg);
Socket workerSocket = null;
workerSocket = (Socket)m_workerSocketList[comboBox1.SelectedIndex];
if (workerSocket != null)
{
if (workerSocket.Connected)
{
workerSocket.Send(byData);
}
}
}
Loading
VulpesPosted Dec 3, 2011, 6:34 AM
Sam HobbsPosted Dec 4, 2011, 5:01 PM
There is an easier way to do this. You can make a class that represents connections; you probably have done that already. Then you can add objects of that class as combobox items. Then all you have to do is to cast the sected combobox item to the connection object. Look at the articles in this web site for details about how to do that; I assume it is described very well in articles here. If not then let us know you need help with it.
haan jaePosted Dec 4, 2011, 9:50 AM
anyway, thanks again for helping me. :)
VulpesPosted Dec 4, 2011, 7:06 AM
haan jaePosted Dec 4, 2011, 6:20 AM
no, the code doesn't solve as well. i try putting another catch after catch for ArgumentOutOfRangeException, but obviously doesn't work as well.
catch
{
workerSocket = null;
MessageBox.Show("Please select number.");
}
but the index number of connected client will keep adding even through client disconnect and reconnect, until the server system restart, then only the index number will start with 1 again.
VulpesPosted Dec 4, 2011, 4:49 AM
haan jaePosted Dec 4, 2011, 1:40 AM
but i realize there is one problem, the message box will not show again if select the index number that match the connected client who already disconnected. like if client no 3 disconnect, i tried to select number 3 in comboBox to send message, see whether the message box will show out or not, but it didnt show. Any idea why this happen??
Anyway, thanks again for the help.