当前位置:   article > 正文

C#udp通信_c# udpclient怎么防止端口被占用

c# udpclient怎么防止端口被占用

本人是做嵌入式开发的,上位机只会一点,入门阶段,下面程序是自己做的测试程序一部分

1. 包含空间

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Net;
  8. using System.Net.Sockets;
  9. using System.Text;
  10. using System.Threading;
  11. using System.Threading.Tasks;
  12. using System.Windows.Forms;
  13. using System.IO;

2. 新建socket

  1. private void button1_Click(object sender, EventArgs e)
  2. {
  3. if (button1.Text == "connect")
  4. {
  5. try
  6. {
  7. client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  8. client.Bind(new IPEndPoint(IPAddress.Parse(txtIP.Text), int.Parse(txtPort.Text)));
  9. //t = new Thread(sendMsg);
  10. t2 = new Thread(ReciveMsg);
  11. t2.Start();
  12. buttonen(true);
  13. button1.Text = "disconnect";
  14. }
  15. catch
  16. {
  17. MessageBox.Show("connect message is error", "错误");//出错提示
  18. }
  19. }
  20. else
  21. {
  22. t2.Abort();
  23. client.Close();
  24. button1.Text = "connect";
  25. buttonen(false);
  26. }

3.发送数据

  1. private void button2_Click(object sender, EventArgs e)
  2. {
  3. byte[] Data = new byte[((sendBox1.Text.Length) / 2)+36];//作用同上集
  4. //EndPoint point = new IPEndPoint(IPAddress.Parse("192.168.3.29"), 16800);
  5. EndPoint point = new IPEndPoint(IPAddress.Parse(sendboxip.Text), int.Parse(sendport.Text));
  6. if (radioButton2.Checked) //字符模式
  7. {
  8. try
  9. {
  10. string msg = sendBox1.Text;
  11. client.SendTo(Encoding.Default.GetBytes(msg), point);
  12. }
  13. catch
  14. {
  15. MessageBox.Show("send message is error", "错误");//出错提示
  16. }
  17. }
  18. else
  19. {
  20. try
  21. {
  22. if (0 == (sendBox1.Text.Length) % 2)
  23. {
  24. for (int i = 0; i < (sendBox1.Text.Length) / 2; i++)
  25. {
  26. Data[i] = Convert.ToByte(sendBox1.Text.Substring(i * 2, 2), 16);
  27. // client.SendTo(Data, point);
  28. }
  29. client.SendTo(Data, point);
  30. // client.Send(Data, 0);
  31. }
  32. else
  33. MessageBox.Show("send message len is error", "错误");//出错提示
  34. }
  35. catch
  36. {
  37. MessageBox.Show("send message is error", "错误");//出错提示
  38. }
  39. }
  40. }

3.接收udp数据

  1. void ReciveMsg()
  2. {
  3. while (true)
  4. {
  5. EndPoint point = new IPEndPoint(IPAddress.Any, 0);//用来保存发送方的ip和端口号
  6. byte[] buffer = new byte[2048];
  7. int length = client.ReceiveFrom(buffer, ref point);//接收数据报
  8. recv = "";
  9. recv += Encoding.UTF8.GetString(buffer, 0, length);
  10. //recv += "\r\n";
  11. if(length > 0)
  12. {
  13. if (radioButton3.Checked) //字符形似
  14. {
  15. AddContent(recv, richTextBox1);
  16. AddContentline(richTextBox1);
  17. }
  18. else
  19. {
  20. for (int i = 0; i < length; i++)
  21. {
  22. string str = Convert.ToString(buffer[i], 16).ToUpper();//转换为大写十六进制字符串
  23. AddContent(str, richTextBox1);
  24. //textBox1.AppendText("0x" + (str.Length == 1 ? "0" + str : str) + " ");//空位补“0
  25. }
  26. AddContentline(richTextBox1);
  27. }
  28. }
  29. //txtReciv.Text += recv;
  30. }
  31. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/人工智能uu/article/detail/895533
推荐阅读
相关标签
  

闽ICP备14008679号