1. 程式人生 > >c# 使用udp傳送和讀取訊息

c# 使用udp傳送和讀取訊息

參考文章:https://msdn.microsoft.com/zh-cn/library/system.net.sockets.udpclient.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-1
下載文件:http://download.csdn.net/detail/zhukangle/9850819
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.IO;
using System.Linq;

using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace C_UDP
{
    public partial class Form1 : Form
    {
        public Form1()
        {
        InitializeComponent();
        }

        private void btnConvert_Click(object sender, EventArgs e)
        {
        }

        private void label1_Click(object sender, EventArgs e)
        {
        }
        UdpClient udpClient = new UdpClient(8765);
        private void btnSele_Click_Send(object sender, EventArgs e)
        {
            // 匿名傳送
            //udpcSend = new UdpClient(0);             // 自動分配本地IPv4地址
            // 實名傳送
            //IPEndPoint localIpep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 12345); // 本機IP,指定的埠號
            //udpcSend = new UdpClient(localIpep);

            //Thread thrSend = new Thread(SendMessage);
            //thrSend.Start("hello world");


            // This constructor arbitrarily assigns the local port number.
               
            try
            {

                udpClient.Connect("192.168.1.178", 8765);
                // Sends a message to the host to which you have connected.
                Byte[] sendBytes = Encoding.ASCII.GetBytes("Is anybody there?");

                udpClient.Send(sendBytes, sendBytes.Length);


                //IPEndPoint object will allow us to read datagrams sent from any source.
                //IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);

                // Blocks until a message returns on this socket from a remote host.
                //Byte[] receiveBytes = udpClient.Receive(ref RemoteIpEndPoint);
                //string returnData = Encoding.ASCII.GetString(receiveBytes);

                // Uses the IPEndPoint object to determine which of these two hosts responded.
                //Console.WriteLine("This is the message you received " +returnData.ToString());
                //Console.WriteLine("This message was sent from " +RemoteIpEndPoint.Address.ToString() + " on their port number " +RemoteIpEndPoint.Port.ToString());

                //udpClient.Close();
                //udpClientB.Close();
                //label1.Text += returnData.ToString()+"This message was sent from " + RemoteIpEndPoint.Address.ToString() + " on their port number " + RemoteIpEndPoint.Port.ToString();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }

        private void btnSele_Click_Recv(object sender, EventArgs e)
        {
            //UdpClient udpClient = new UdpClient(8765);
            //IPEndPoint object will allow us to read datagrams sent from any source.
            IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
            //udpClient.Connect("192.168.1.178", 8765);
            // Blocks until a message returns on this socket from a remote host.
            Byte[] receiveBytes = udpClient.Receive(ref RemoteIpEndPoint);
            string returnData = Encoding.ASCII.GetString(receiveBytes);

            // Uses the IPEndPoint object to determine which of these two hosts responded.
            //Console.WriteLine("This is the message you received " + returnData.ToString());
            // Console.WriteLine("This message was sent from " + RemoteIpEndPoint.Address.ToString() + " on their port number " + RemoteIpEndPoint.Port.ToString());
            udpClient.Close();
            //udpClientB.Close();
            label1.Text += returnData.ToString() + "This message was sent from " + RemoteIpEndPoint.Address.ToString() + " on their port number " + RemoteIpEndPoint.Port.ToString();

        }

    }
}