區網內P2P不用SERVER的C#聊天程式(C# P2P-chat-systems)

區網內P2P不用SERVER的C#聊天程式(C# P2P-chat-systems)

區網內P2P不用SERVER的C#聊天程式(C# P2P-chat-systems)

 

Today we are going to work on a peer-to-peer, P2P, chat system. The approach that I’m taking requires that you have two computers on a local area network, or you could have one computer and use virtual machines. Basically, you can only run one peer on a single computer.

 

資料來源: https://www.dreamincode.net/forums/topic/231058-peer-to-peer-chat-advanced/

 

GITHUB: https://github.com/jash-git/CS_P2P-chat-systems

 

主要程式碼:(包含 執行序UDP接收訊息 / 執行序修改UI /UDP廣播發送 )

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace P2P_chat_systems
{
    public partial class ChatForm : Form
    {
        delegate void AddMessage(string message);

        string userName;

        const int port = 54545;
        const string broadcastAddress = "255.255.255.255";

        UdpClient receivingClient;
        UdpClient sendingClient;

        Thread receivingThread;

        public ChatForm()
        {
            InitializeComponent();
        }

        private void ChatForm_Load(object sender, EventArgs e)
        {
            this.Hide();

            using (LoginForm loginForm = new LoginForm())
            {
                loginForm.ShowDialog();

                if (loginForm.UserName == "")
                    this.Close();
                else
                {
                    userName = loginForm.UserName;
                    this.Show();
                }
            }

            textBox1.Focus();

            InitializeSender();
            InitializeReceiver();
        }

        private void InitializeSender()
        {
            sendingClient = new UdpClient(broadcastAddress, port);
            sendingClient.EnableBroadcast = true;
        }

        private void InitializeReceiver()
        {
            receivingClient = new UdpClient(port);

            ThreadStart start = new ThreadStart(Receiver);
            receivingThread = new Thread(start);
            receivingThread.IsBackground = true;
            receivingThread.Start();
        }
        private void Receiver()
        {
            IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, port);
            AddMessage messageDelegate = MessageReceived;

            while (true)
            {
                byte[] data = receivingClient.Receive(ref endPoint);
                string message = Encoding.ASCII.GetString(data);
                Invoke(messageDelegate, message);
            }
        }

        private void MessageReceived(string message)
        {
            richTextBox1.Text += message + "\n";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text.TrimEnd();

            if (!string.IsNullOrEmpty(textBox1.Text))
            {
                string toSend = userName + ":\t" + textBox1.Text;
                byte[] data = Encoding.ASCII.GetBytes(toSend);
                sendingClient.Send(data, data.Length);
                textBox1.Text = "";
            }

            textBox1.Focus();
        }
    }
}

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *