Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.Net.Sockets;
- using System.Net;
- namespace WindowsFormsApp1
- {
- public partial class Form1 : Form
- {
- bool alive = false; // будет ли работать поток для приема
- UdpClient client;
- const int LOCALPORT = 8001; // порт для приема сообщений
- const int REMOTEPORT = 8001; // порт для отправки сообщений
- const int TTL = 20;
- const string HOST = "235.5.5.1"; // хост для групповой рассылки
- IPAddress groupAddress; // адрес для групповой рассылки
- string userName; // имя пользователя в чате
- public Form1()
- {
- InitializeComponent();
- logoutButton.Enabled = false;
- sendButton.Enabled = false;
- groupAddress = IPAddress.Parse(HOST);
- }
- private void loginButton_Click(object sender, EventArgs e)
- {
- userName = userNameTextBox.Text;
- userNameTextBox.ReadOnly = true;
- try
- {
- client = new UdpClient(LOCALPORT);
- // присоединяемся к групповой рассылке
- client.JoinMulticastGroup(groupAddress, TTL);
- // запускаем задачу на прием сообщений
- Task receiveTask = new Task(ReceiveMessages);
- receiveTask.Start();
- // отправляем первое сообщение о входе нового пользователя
- string message = userName + " вошел в чат";
- byte[] data = Encoding.UTF8.GetBytes(message);
- client.Send(data, data.Length, HOST, REMOTEPORT);
- loginButton.Enabled = false;
- logoutButton.Enabled = true;
- sendButton.Enabled = true;
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
- // метод приема сообщений
- private void ReceiveMessages()
- {
- alive = true;
- try
- {
- while (alive)
- {
- IPEndPoint remoteIp = null;
- byte[] data = client.Receive(ref remoteIp);
- string message = Encoding.UTF8.GetString(data);
- // добавляем полученное сообщение в текстовое поле
- this.Invoke(new MethodInvoker(() =>
- {
- string time = DateTime.Now.ToShortTimeString();
- chatTextBox.Text = time + " " + message + "\r\n" + chatTextBox.Text;
- }));
- }
- }
- catch (ObjectDisposedException)
- {
- if (!alive)
- return;
- throw;
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
- // обработчик нажатия кнопки sendButton
- private void sendButton_Click(object sender, EventArgs e)
- {
- try
- {
- string message = String.Format("{0}: {1}", userName, messageTextBox.Text);
- byte[] data = Encoding.UTF8.GetBytes(message);
- client.Send(data, data.Length, HOST, REMOTEPORT);
- messageTextBox.Clear();
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
- // обработчик нажатия кнопки logoutButton
- private void logoutButton_Click(object sender, EventArgs e)
- {
- ExitChat();
- }
- // выход из чата
- private void ExitChat()
- {
- string message = userName + " покидает чат";
- byte[] data = Encoding.UTF8.GetBytes(message);
- client.Send(data, data.Length, HOST, REMOTEPORT);
- client.DropMulticastGroup(groupAddress);
- alive = false;
- client.Close();
- loginButton.Enabled = true;
- logoutButton.Enabled = false;
- sendButton.Enabled = false;
- }
- // обработчик события закрытия формы
- private void Form1_FormClosing(object sender, FormClosingEventArgs e)
- {
- if (alive)
- ExitChat();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement