UDP发送但未接收

时间:2014-04-27 09:53:23

标签: c# udp

我正在创建一个UDP套接字,通过PC和连接到PC的无线终端设备发送和接收数据。我可以成功发送我想要发送的数据,但是当接收到从无线电设备发送到PC的数据时,没有任何反应,代码就停在“Byte [] receiveBytes = receivingUdpClient.Receive(ref ipep);”在接收函数中,没有任何事情发生在后面。无线电设备的IP地址= 192.168.1.191,端口号= 50000.我也尝试通过“Byte [] receiveBytes = receivingUdpClient.Receive(ref RemoteIpEndPoint);”但仍然没有代码也停止此时后来没有任何事情发生。下面是我的完整代码。非常感谢任何帮助,提前谢谢。

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

namespace UDP_Socket
{
public partial class Form1 : Form
    {

    #region Importand Definitions
    public UdpClient udpClient; 
    public String IPaddress;
    public String PortNumber;
    public IPEndPoint ipep;
    #endregion

    private void Form1_Load(object sender, EventArgs e)
    {
       Connect();
    }

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
       if (udpClient.Client.Connected == true)
       {
           udpClient.Client.Close();
           udpClient.Client.Dispose();
       }           
    }

    private void Add_Btn_Click(object sender, EventArgs e)
    {
       RadioCheck();
       Receive();  
    }

    void RadioCheck()
    {
       byte[] data = Encoding.ASCII.GetBytes("at");
       udpClient.Send(data , data .Length);
    }

    void Connect()
    {
        IPaddress = "192.168.1.191"; 
        PortNumber = "50000";

        //Uses a remote endpoint to establish a socket connection.
        udpClient = new UdpClient();
        IPAddress ipAddress = IPAddress.Parse(IPaddress);
        IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, Convert.ToInt32(PortNumber));
        ipep = ipEndPoint;
        try
        {
            udpClient.Connect(ipEndPoint);
            MessageBox.Show("Successfully connected to: " + ipep.ToString(), "Connection", MessageBoxButtons.OK);                
        }
        catch (SocketException ex)
        {
            Console.WriteLine(ex.ToString());
        MessageBox.Show("Problem connecting to: " + ipep.ToString(), "Connection", MessageBoxButtons.OK);                
        }
    }

    void Receive()
    {
        //Creates a UdpClient for reading incoming data.
        UdpClient receivingUdpClient = new UdpClient(50000);

        //Creates an IPEndPoint to record the IP Address and port number of the sender.  
        // The IPEndPoint will allow you to read datagrams sent from any source.
        IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);

        try
        {
            Byte[] receiveBytes = receivingUdpClient.Receive(ref ipep);//RemoteIpEndPoint);

            string returnData = Encoding.ASCII.GetString(receiveBytes);

            Console.WriteLine("This is the message you received " +
                                            returnData.ToString());
            Console.WriteLine("This message was sent from " +
                                            ipep.Address.ToString() +
                                            " on their port number " +
                                            ipep.Port.ToString()); //RemoteIpEndPoint
        }
        catch (Exception e)
        {
            Console.WriteLine(e.ToString());
        }
    }

  }
}

0 个答案:

没有答案