首页 新闻 会员 周边 捐助

C#无线网络连接UDP协议收发数据问题?

0
悬赏园豆:5 [待解决问题]

当启动软件发送指令后,关闭软件,二次打开软件无法收到数据???请问是什么原因???

收数据采用的是异步模式。程序中有很多问题请绕过。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp2
{
    public partial class Form1 : Form
    {
        IPAddress address;
        IPEndPoint endPoint;
        //UdpClient udpClient;
        IPEndPoint iPEndPoint1 = new IPEndPoint(IPAddress.Parse("169.254.4.178"), 0);
        IPEndPoint iPEndPoint = new IPEndPoint(IPAddress.Parse("192.168.10.2"), 0);
        //IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Parse("192.168.1.231"), 0);
        //Socket socketClient = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
        static byte[] buffer = new byte[1024];

        public Form1()
        {
            InitializeComponent();
            InitializeComponent();
            address = IPAddress.Parse("192.168.10.1");
            endPoint = new IPEndPoint(address, 8889);
            Socket socketClient = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            socketClient.Bind(iPEndPoint);
            socketClient.Connect(endPoint);
            //udpClient = new UdpClient(iPEndPoint);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Socket socketClient = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            socketClient.Bind(iPEndPoint);
            socketClient.Connect(endPoint);
            string cmd = "command";
            byte[] mybyte = System.Text.Encoding.ASCII.GetBytes(cmd);
            socketClient.SendTo(mybyte, mybyte.Length, SocketFlags.None, endPoint);
            
            socketClient.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveMessage), socketClient);
            //socketClient.Shutdown(SocketShutdown.Both);
            //socketClient.Close();

        }

        private void button2_Click(object sender, EventArgs e)
        {
            Socket socketClient = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            socketClient.Bind(iPEndPoint);
            socketClient.Connect(endPoint);
            string cmd = "takeoff";
            byte[] mybyte = System.Text.Encoding.ASCII.GetBytes(cmd);
            socketClient.SendTo(mybyte, mybyte.Length, SocketFlags.None, endPoint);
            //socketClient.Shutdown(SocketShutdown.Both);
            socketClient.Close();
        }

        /// <summary>  
        /// 接收信息  
        /// </summary>  
        /// <param name="ar"></param>  
        public static void ReceiveMessage(IAsyncResult ar)
        {
            
            try
            {
                var socket = ar.AsyncState as Socket;

                //方法参考:http://msdn.microsoft.com/zh-cn/library/system.net.sockets.socket.endreceive.aspx  
                var length = socket.EndReceive(ar);
                //读取出来消息内容  
                var message = Encoding.ASCII.GetString(buffer, 0, length);

                //显示消息  
                //WriteLine(message, ConsoleColor.White);
                MessageBox.Show(message, "提示");

                //接收下一个消息(因为这是一个递归的调用,所以这样就可以一直接收消息了)  
                socket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveMessage), socket);
            }
            catch (Exception ex)
            {
                var socket = ar.AsyncState as Socket;
                socket.Close();
            }
            
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Socket socketClient = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            socketClient.Bind(iPEndPoint);
            socketClient.Connect(endPoint);
            string cmd = "land";
            byte[] mybyte = System.Text.Encoding.ASCII.GetBytes(cmd);
            socketClient.SendTo(mybyte, mybyte.Length, SocketFlags.None, endPoint);
            //socketClient.Shutdown(SocketShutdown.Both);
            socketClient.Close();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            Socket socketClient = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            socketClient.Bind(iPEndPoint);
            socketClient.Connect(endPoint);
            //string turn = "cw 360";
            //string fly = "forward 20";
            //byte[] angle = System.Text.Encoding.ASCII.GetBytes(turn);
            //byte[] mybytel = System.Text.Encoding.ASCII.GetBytes(fly);
            //for (int i = 1; i <= 10; i++)
            //{
            //    socketClient.SendTo(angle, angle.Length, SocketFlags.None, endPoint);
            //    socketClient.SendTo(mybytel, mybytel.Length, SocketFlags.None, endPoint);
            //    Delay(2000);
            //}
            string cmd = "battery?";
            byte[] mybyte = System.Text.Encoding.UTF8.GetBytes(cmd);
            socketClient.SendTo(mybyte, mybyte.Length, SocketFlags.None, endPoint);
            //socketClient.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveMessage), socketClient);
            //socketClient.Shutdown(SocketShutdown.Both);
            socketClient.Close();
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            //socketClient.Close();
        }
    }
}
一叶沉浮的主页 一叶沉浮 | 初学一级 | 园豆:197
提问于:2018-03-23 22:39
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册