private void btnSend_Click(object sender, EventArgs e) { try { TextMessagePackage textMessagePackage = new TextMessagePackage(rtbMessageText.Text); PacketHandler.clientList.Find(client => client.clientSocket == clientSocket).clientSocket.BeginSend(textMessagePackage.ToByteArray(), 0, textMessagePackage.ToByteArray().Length, SocketFlags.None, new AsyncCallback(SendCallback), null); //clientSocket.BeginSend(textMessagePackage.ToByteArray(), 0, textMessagePackage.ToByteArray().Length, SocketFlags.None, new AsyncCallback(SendCallback), null); rtbMessages.AppendText("Ja: " + rtbMessageText.Text + "\r\n"); rtbMessageText.Clear(); } catch (Exception ex) { MessageBox.Show("Slanje poruke nije uspjelo!"); } }
private static void TextMessagePacketHandler(byte[] receivedPacket, Socket clientSocket) { //Radimo konverziju primljenog paketa (lambda expressions) TextMessagePackage textMessagePackage = new TextMessagePackage(receivedPacket); //Identificiramo klijenta koji je poslao poruku po socketu Client clientThatSentMessage = clientList.Find(client => client.clientSocket == clientSocket); if (!openedMessageForms.ContainsKey(clientSocket)) { MessageForm NewMessageForm = new MessageForm(clientSocket); openedMessageForms.Add(clientSocket, NewMessageForm); NewMessageForm.ReceiveMessage(textMessagePackage.textMessage, clientThatSentMessage.clientComputerName); NewMessageForm.ShowDialog(); //Application.Run(); } else { MessageForm NewMessageForm = openedMessageForms[clientSocket]; //NewMessageForm.ShowDialog(); //NewMessageForm.Activate(); NewMessageForm.ReceiveMessage(textMessagePackage.textMessage, clientThatSentMessage.clientComputerName); } }