private void connectedUsers_DoubleClick(object sender, EventArgs e) { if (connectedUsers.SelectedItem != null) { string target = connectedUsers.SelectedItem.ToString(); if (!IMs.ContainsKey(target)) { ChatIM im = new ChatIM(this, target);//,"this is a test"); //im.addMessage("hello"); IMs.Add(connectedUsers.SelectedItem.ToString(), im); im.ShowDialog(); } } }
private void ReceiveCallback(IAsyncResult ar) { try { // Retrieve the state object and the client socket // from the asynchronous state object. StateObject state = (StateObject)ar.AsyncState; Socket client = state.socket; int bytesRead = client.EndReceive(ar); state.sb.Append(Encoding.ASCII.GetString(state.dataBuffer, 0, bytesRead)); response = state.sb.ToString(); if (response.Contains("connect:") && response.IndexOf("connect:") == 0) { string result = response.Substring(8); int index = result.IndexOf(","); if (index > -1) { bool done = false; while (done == false) { string user = result.Substring(0, index); if (user != userText.Text) { AddUsers(user);// connectedUsers.Items.Add(user); } result = result.Substring(index + 1); index = result.IndexOf(","); if (index == -1) { connectedUsers.Items.Add(result); done = true; } } } else { if (userText.Text != result) { AddUsers(result);// connectedUsers.Items.Add(result); } } } else if (response.Contains("disconnect:") && response.IndexOf("disconnect:") == 0) { string user = response.Substring(11); RemoveUsers(user);// connectedUsers.Items.Remove(user); } else if (response.Contains("IM_") && response.IndexOf("IM_") == 0) { string message = response.Substring(3); string user = message.Substring(0, message.IndexOf(":")); //string message = str.Substring(str.IndexOf(":") + 1); if (!IMs.ContainsKey(user)) { ChatIM im = new ChatIM(this, user); IMs.Add(user, im); im.addMessage(message); im.ShowDialog(); } else { IMs[user].addMessage(message); } } else if (response == "Server_Disconnect") { Disconnect(); return; } else if (response == "USER_EXISTS") { MessageBox.Show("The user you entered in already exists"); Disconnect(); return; } else { UpdateTextBox(response); } state.sb.Clear(); client.BeginReceive(state.dataBuffer, 0, state.bufferSize, 0, new AsyncCallback(ReceiveCallback), state); } catch (Exception e) { Console.WriteLine(e.ToString()); Disconnect(); } }