⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 formmain.cs

📁 Udp与TCP简单例子,写的不好,切午奸笑.
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

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


namespace UDPClient
{
    public partial class FormMain : Form
    {

        private UdpClient client;
        private IPEndPoint receivePoint;

        public delegate void SetTextCallback(string text);//多线程显示文本委托

        public FormMain()
        {
            InitializeComponent();
        }

        private void FormMain_Load(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 显示文本,防止在线程中显示文本导致控件共享冲突
        /// </summary>
        /// <param name="text"></param>
        public void ShowDisplayText(string text)
        {
            if (this.textBoxDisplay.InvokeRequired)
            {
                SetTextCallback d = new SetTextCallback(ShowDisplayText);
                this.Invoke(d, new object[] { text });
            }
            else
            {
                this.textBoxDisplay.Text += text;
            }
        }

        /// <summary>
        /// 等待接收数据包线程
        /// </summary>
        public void waitForPackets()
        {
            byte[] data = System.Text.Encoding.ASCII.GetBytes("connect?");
            client.Send(data, data.Length, receivePoint);
        
            while (true)
            {
                /* Receive 方法将阻止,直到数据报从远程主机到达为止。如果数据可用,
                 * 则 Receive 方法将读取入队的第一个数据报,并将数据部分作为字节数组返回。
                 * 此方法使用发送方的 IPAddress 和端口号来填充 remoteEP 参数。
                 * */
                data = client.Receive(ref receivePoint);
                ShowDisplayText("\r\n收到:"+System.Text.Encoding.ASCII.GetString(data));//转换并显示收到的数据
                //client.Send(data, data.Length, receivePoint);//将内容重新发回客户端

            }
        }

        private void buttonStart_Click(object sender, EventArgs e)
        {
            int pid = int.Parse(this.textBoxPort.Text.ToString());
            IPAddress ip = IPAddress.Parse(this.textBoxIpAddr.Text);//得到目标IP地址

            client = new UdpClient(7000);//本机监听端口确定
            receivePoint = new IPEndPoint(ip, pid);

            Thread readThread = new Thread(new ThreadStart(waitForPackets));
            readThread.Start();
        }

        private void textBoxInput_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                string str = this.textBoxInput.Text;
                this.textBoxDisplay.Text += "\r\nSend:" + str;

                byte[] data = System.Text.Encoding.ASCII.GetBytes(str);

                client.Send(data, data.Length, "localhost", 5000);
                this.textBoxInput.Clear();
            }
        }

        private void FormMain_FormClosed(object sender, FormClosedEventArgs e)
        {
            System.Environment.Exit(System.Environment.ExitCode);
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -