• 大小: 336KB
    文件类型: .rar
    金币: 2
    下载: 1 次
    发布日期: 2021-06-18
  • 语言: C#
  • 标签: C#  UDP  

资源简介

用C#实现了一个简单的UDP通讯 客户端与移动端采用UDP通讯

资源截图

代码片段和文件信息

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

namespace UDPclient
{
    public partial class Form1 : Form
    {
        Socket socketClient;
        Thread threadSend;
        
       
        public Form1()
        {
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = false;
        }

        private void Form1_Load(object sender EventArgs e)
        {
            textBox1.Text = “192.168.1.255“;
            textBox2.Text = “20018“;
            comboBox1.Items.Add(“GPS“);
            comboBox1.Items.Add(“加速度计“);
            comboBox1.SelectedIndex = 0;
        }

        private void button1_Click(object sender EventArgs e)
        {
            timer1.Enabled = true;
            socketClient = new Socket(AddressFamily.InterNetwork SocketType.Dgram ProtocolType.Udp);
            IPEndPoint ipe = new IPEndPoint(IPAddress.Broadcast int.Parse(textBox2.Text));
            threadSend = new Thread(new ThreadStart(SendMsg));
            threadSend.IsBackground = true;
            threadSend.Start();
           
        }
        void SendMsg()
        {
            EndPoint endPoint = new IPEndPoint(IPAddress.Broadcast int.Parse(textBox2.Text));
            listBox1.Items.Add(GetAccelemeData());
            socketClient.SetSocketOption(SocketOptionLevel.Socket SocketOptionName.Broadcast 1);
            try
            {
                socketClient.SendTo(Encoding.UTF8.GetBytes(GetAccelemeData()) endPoint);
            }
            catch(Exception se)
            {
                listBox1.Items.Add(se.Message);
            }
          
        }
        private string GetAccelemeData()
        {
            Random myradom = new Random();
            int data1 = myradom.Next(100 1000);
            int data2 = myradom.Next(10 100);

            Random GPS1random = new Random();
            int time = GPS1random.Next(100 999);
            string timeTest = DateTime.UtcNow.ToString(“HHmmss.fff“);
            timeTest = string.Format(“{0}.{1}“ timeTest time);

            int data3 = myradom.Next(10 20);
            int data4 = myradom.Next(1 10);
            int data5 = myradom.Next(1 5);
            string str = string.Format(“{0}1.1{1}4{2}{3}{4}{5}“ timeTest data1 data2 data3 data4 data5);
            return str;
        }

        private void timer1_Tick(object sender EventArgs e)
        {
            this.button1.PerformClick();
        }

        private void button2_Click(object sender EventArgs e)
        {
            timer1.Enabled = false;
        }
    }
}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

    ..A..H.     44544  2018-12-01 22:10  UDP\UDPclient\.vs\UDPclient\v15\.suo

     文件          0  2018-11-21 14:08  UDP\UDPclient\.vs\UDPclient\v15\Server\sqlite3\db.lock

     文件     593920  2018-12-01 22:10  UDP\UDPclient\.vs\UDPclient\v15\Server\sqlite3\storage.ide

     文件        189  2018-11-21 14:08  UDP\UDPclient\UDPclient\App.config

     文件        189  2018-11-21 14:08  UDP\UDPclient\UDPclient\bin\Debug\UDPclient.exe.config

     文件      30208  2018-12-01 22:10  UDP\UDPclient\UDPclient\bin\Debug\UDPclient.pdb

     文件       2912  2018-12-01 22:10  UDP\UDPclient\UDPclient\Form1.cs

     文件       6908  2018-12-01 22:10  UDP\UDPclient\UDPclient\Form1.Designer.cs

     文件       6011  2018-12-01 22:10  UDP\UDPclient\UDPclient\Form1.resx

     文件       1443  2018-11-23 15:23  UDP\UDPclient\UDPclient\obj\Debug\DesignTimeResolveAssemblyReferences.cache

     文件       7164  2018-11-21 14:11  UDP\UDPclient\UDPclient\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache

     文件          0  2018-11-21 14:08  UDP\UDPclient\UDPclient\obj\Debug\TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs

     文件          0  2018-11-21 14:08  UDP\UDPclient\UDPclient\obj\Debug\TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs

     文件          0  2018-11-21 14:08  UDP\UDPclient\UDPclient\obj\Debug\TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs

     文件         42  2018-12-01 22:07  UDP\UDPclient\UDPclient\obj\Debug\UDPclient.csproj.CoreCompileInputs.cache

     文件       1647  2018-12-01 22:09  UDP\UDPclient\UDPclient\obj\Debug\UDPclient.csproj.FileListAbsolute.txt

     文件       1012  2018-12-01 22:10  UDP\UDPclient\UDPclient\obj\Debug\UDPclient.csproj.GenerateResource.cache

     文件      11490  2018-11-23 15:23  UDP\UDPclient\UDPclient\obj\Debug\UDPclient.csprojAssemblyReference.cache

     文件      13867  2018-12-01 22:07  UDP\UDPclient\UDPclient\obj\Debug\UDPclient.csprojResolveAssemblyReference.cache

     文件      11776  2018-12-01 22:10  UDP\UDPclient\UDPclient\obj\Debug\UDPclient.exe

     文件        180  2018-12-01 22:10  UDP\UDPclient\UDPclient\obj\Debug\UDPclient.Form1.resources

     文件      30208  2018-12-01 22:10  UDP\UDPclient\UDPclient\obj\Debug\UDPclient.pdb

     文件        180  2018-11-21 14:46  UDP\UDPclient\UDPclient\obj\Debug\UDPclient.Properties.Resources.resources

     文件        521  2018-11-21 14:08  UDP\UDPclient\UDPclient\Program.cs

     文件       1316  2018-11-21 14:08  UDP\UDPclient\UDPclient\Properties\AssemblyInfo.cs

     文件       2831  2018-11-21 14:08  UDP\UDPclient\UDPclient\Properties\Resources.Designer.cs

     文件       5612  2018-11-21 14:08  UDP\UDPclient\UDPclient\Properties\Resources.resx

     文件       1096  2018-11-21 14:08  UDP\UDPclient\UDPclient\Properties\Settings.Designer.cs

     文件        249  2018-11-21 14:08  UDP\UDPclient\UDPclient\Properties\Settings.settings

     文件       3606  2018-11-21 14:46  UDP\UDPclient\UDPclient\UDPclient.csproj

............此处省略62个文件信息

评论

共有 条评论