向聊天客户端添加安全性

问题描述

这是我的基本聊天程序,允许用户添加自己的IP并接收IP,以及用于通信的自定义端口。

在Windows窗体设计中,我有:

文本框--- textLocalIp

文本框--- textLocalPort

文本框--- textFriendIp

文本框--- textFriendPort

文本框--- textMessage

列表框--- listMessage

按钮---开始

按钮---发送

所以这是一个非常基本的客户端,但是我需要的帮助是使其安全/更加安全,因为当前的安全性还不好。

也许就像具有不同IP的隧道,这样您就不必知道接收方的IP,只需知道接收方提供的IP,然后再将其发送给接收方即可。

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;
using System.Net.Sockets;



namespace Client
{
    public partial class Form1 : Form
    {
        Socket sck;
        EndPoint epLocal,epRemote;

        public Form1()
        {
            InitializeComponent();

            sck = new Socket(AddressFamily.InterNetwork,SocketType.Dgram,ProtocolType.Udp);
            sck.SetSocketOption(SocketOptionLevel.Socket,SocketOptionName.ReuseAddress,true);

            textLocalIp.Text = GetLocalIP();
            textFriendIp.Text = GetLocalIP();

        }

        private string GetLocalIP()
        {
            IPHostEntry host;
            host = Dns.GetHostEntry(Dns.GetHostName());

            foreach (IPAddress ip in host.AddressList)
            {
                if (ip.AddressFamily == AddressFamily.InterNetwork)
                {
                    return ip.ToString();
                }
            }

            return "127.0.0.1";

        }


        private void MessageCallBack(IAsyncResult aResult)
        {
            try
            {
                int size = sck.EndReceiveFrom(aResult,ref epRemote);
                if (size > 0)
                {
                    byte[] recievedData = new byte[1464];

                    recievedData = (byte[])aResult.AsyncState;

                    ASCIIEncoding eEncoding = new ASCIIEncoding();
                    string receivedMessage = eEncoding.GetString(recievedData);
                    listMessage.Items.Add("Sender: "+receivedMessage);

                }

                byte[] buffer = new byte[1500];
                sck.BeginReceiveFrom(buffer,buffer.Length,SocketFlags.None,ref epRemote,new AsyncCallback(MessageCallBack),buffer);

            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.ToString());
            }

        }

 private void button1_Click(object sender,EventArgs e)
        {
            try
            {
                System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
                byte[] msg = new byte[1500];
                msg = enc.GetBytes(textMessage.Text);

                sck.Send(msg);

                listMessage.Items.Add("Local:" + textMessage.Text);
                textMessage.Clear();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());

            }
        }

        private void start_Click(object sender,EventArgs e)
        {
            try
            {
                epLocal = new IPEndPoint(IPAddress.Parse(textLocalIp.Text),Convert.ToInt32(textLocalPort.Text));
                sck.Bind(epLocal);

                epRemote = new IPEndPoint(IPAddress.Parse(textFriendIp.Text),Convert.ToInt32(textFriendPort.Text));
                sck.Connect(epRemote);

                byte[] buffer = new byte[1500];
                sck.BeginReceiveFrom(buffer,buffer);

                start.Text = "Connected";
                start.Enabled = false;
                send.Enabled = true;
                textMessage.Focus();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)