private void LeaveChat() { UdpClient client = new UdpClient(); IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("230.230.230.230"), 8005); try { TalkPacket msg = new TalkPacket(0); client.Send(msg.getBytes(), msg.MsgLength, endPoint); textBoxName.Enabled = true; buttonConnect.Text = "Подключиться"; Connected = false; Requested = false; cancelTokenSource.Cancel(); this.Invoke(new MethodInvoker(() => { ListMessages.Items.Add(" " + DateTime.Now.ToLongTimeString() + " Вы покинули нас."); })); } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { client.Close(); } }
private void ConnectChat() { textBoxName.Text = UserName; UdpClient client = new UdpClient(); IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("230.230.230.230"), 8005); try { TalkPacket msg = new TalkPacket(1, UserName); client.Send(msg.getBytes(), msg.MsgLength, endPoint); client.Close(); textBoxName.Enabled = false; buttonConnect.Text = "Отключиться"; Connected = true; token = cancelTokenSource.Token; receiveUDP = new Task(() => ReceiveConnection()); receiveTCP = new Task(() => ReceiveMessage()); receiveUDP.Start(); receiveTCP.Start(); this.Invoke(new MethodInvoker(() => { ListMessages.Items.Add(" " + DateTime.Now.ToLongTimeString() + " Вы присоединились к разговору."); })); } catch (Exception ex) { client.Close(); MessageBox.Show(ex.Message); } }
private void ReceiveConnection() { try { while (!token.IsCancellationRequested) { UdpClient client = new UdpClient(8005); client.JoinMulticastGroup(IPAddress.Parse("230.230.230.230"), 100); IPEndPoint remoteIp = null; byte[] data = client.Receive(ref remoteIp); TalkPacket msg = new TalkPacket(data); Sub sub = new Sub(Encoding.Unicode.GetString(msg.Data), new IPEndPoint(remoteIp.Address, 8006)); client.Close(); if (msg.Type == 1) { if (!Subs.Contains(sub)) { Subs.Add(sub); } this.Invoke(new MethodInvoker(() => { ListMessages.Items.Add(" " + DateTime.Now.ToLongTimeString() + " " + sub.name + "(" + sub.endPoint.ToString() + ") " + " присоединился к разговору."); })); msg = new TalkPacket(1, UserName); Socket sender = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); sender.Connect(sub.endPoint.Address, 8006); sender.Send(msg.getBytes(), SocketFlags.None); sender.Close(); } else if (msg.Type == 0) { foreach (Sub Ssub in Subs) { if (Ssub.endPoint == sub.endPoint) { Subs.Remove(Ssub); break; } } this.Invoke(new MethodInvoker(() => { ListMessages.Items.Add(" " + DateTime.Now.ToLongTimeString() + " " + sub.name + "(" + sub.endPoint.ToString() + "): " + " покинул нас."); })); } else { MessageBox.Show("An error occured!"); } } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
private void ReceiveConnection(ListBox lb) { UdpClient client = new UdpClient(8005); client.JoinMulticastGroup(IPAddress.Parse("230.230.230.230"), 50); IPEndPoint remoteIp = null; try { while (!token.IsCancellationRequested) { byte[] data = client.Receive(ref remoteIp); TalkPacket msg = new TalkPacket(data); Sub sub = new Sub(msg.Data.ToString(), remoteIp); if (msg.Type == 1) { if (!Subs.Contains(sub)) { Subs.Add(sub); } lb.Items.Add(" " + sub.name + " присоединился к разговору."); TcpClient sender = new TcpClient(remoteIp); NetworkStream stream = sender.GetStream(); msg = new TalkPacket(1, UserName); stream.Write(msg.getBytes(), 0, msg.MsgLength); stream.Close(); sender.Close(); } else if (msg.Type == 0) { if (Subs.Contains(sub)) { Subs.Remove(sub); lb.Items.Add(" " + sub.name + " покинул нас."); } } else { MessageBox.Show("An error occured!"); } } } catch (Exception ex) { MessageBox.Show(ex.Message); } client.Close(); }
private void buttonSend_Click(object sender, EventArgs e) { if (Connected && textBoxMsg.Text.Length > 0) { TalkPacket msg = new TalkPacket(2, textBoxMsg.Text); ListMessages.Items.Add(" " + DateTime.Now.ToLongTimeString() + " Вы: " + textBoxMsg.Text); textBoxMsg.Text = ""; Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); foreach (Sub sub in Subs) { client.Connect(sub.endPoint); client.Send(msg.getBytes(), SocketFlags.None); client.Close(); } } else { MessageBox.Show("Вы не подключены к чату."); } }
private void buttonSend_Click(object sender, EventArgs e) { if (Connected) { TalkPacket msg = new TalkPacket(3, textBoxMsg.Text); ListMessages.Items.Add(" Вы: " + textBoxMsg.Text); textBoxMsg.Text = ""; foreach (Sub sub in Subs) { IPEndPoint iep = sub.endPoint; TcpClient client = new TcpClient(iep); NetworkStream stream = client.GetStream(); stream.Write(msg.getBytes(), 0, msg.MsgLength); stream.Close(); client.Close(); } } else { MessageBox.Show("Вы не подключены к чату."); } }
private void LeaveChat(ListBox lb) { UdpClient client = new UdpClient(); IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("230.230.230.230"), 8005); try { TalkPacket msg = new TalkPacket(0); client.Send(msg.getBytes(), msg.MsgLength, endPoint); textBoxName.Enabled = true; buttonConnect.Text = "Подключиться"; Connected = false; cancelTokenSource.Cancel(); lb.Items.Add(" Вы покинули нас."); } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { client.Close(); } }