C# Socket网络通信_UDP(UdpClient)

简单Socket-Udp通信

在本文中涉及到通信,仅做简单的通信(服务端接受消息,由客户端发出消息)
如有需要,根据需求可直接copy使用

关键字UdpClient

需要引用命名空间:

using System.Net;
using System.Net.Sockets;

服务端:

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

namespace Socket_UdpClient_Server
{
    class Program
    {
        static void Main(string[] args)
        {
            SocketStart();  //开启Udp通信
        }

        private static UdpClient udpServer;

        /// <summary>
        /// 开启Udp通信,监听端口号7701
        /// </summary>
        private static void SocketStart()
        {
            int port = 7701;
            IPAddress ip = IPAddress.Parse("192.168.11.83");
            IPEndPoint localPoint = new IPEndPoint(ip, port);       //定义终结点   本机ip和端口号


            udpServer = new UdpClient(localPoint);                  //绑定终结点
            new Thread(() =>
            {
                while (true)
                {
                    try
                    {
                        IPEndPoint ep = new IPEndPoint(IPAddress.Any, 0);               //可接受任意连接至本机此端口发来的消息
                        byte[] data = udpServer.Receive(ref ep);                        //开启端口号,接受消息

                        string content = Encoding.GetEncoding("GBK").GetString(data);
                        Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " 收到客户端 " + ep.ToString() +" 发来的消息: \r\n"+ content);
                    }
                    catch
                    {
                        Console.WriteLine(" 消息接收失败!");
                    }
                }
            }).Start();
        }
    }
}

客户端

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

namespace Socket_UdpClient_Client
{
    class Program
    {
        static void Main(string[] args)
        {
            string content = "今天,仍然是元气满满的一天啊啊啊啊啊啊啊啊!";  //向服务端发送的内容
            Socket(content);        //开启Udp通信
        }


        /// <summary>
        /// 开启Udp通信,发送消息
        /// </summary>
        /// <param name="content"></param>
        private static void Socket(string content)
        {
            int localPort = 12615;
            IPAddress localIP = IPAddress.Parse("192.168.11.83"); 
            IPEndPoint localPoint = new IPEndPoint(localIP, localPort);     //设置终结点 本地ip和端口号

            int remotePort = 7701;
            IPAddress remoteIP = IPAddress.Parse("192.168.11.83");
            IPEndPoint remotePoint = new IPEndPoint(remoteIP, remotePort);  //设置需要连接的终端终结点 终端ip和端口号

            UdpClient udpClient = new UdpClient(localPoint);
            udpClient.Connect(remotePoint);                                //Udp连接

            try
            {
                byte[] data = Encoding.GetEncoding("GBK").GetBytes(content);
                udpClient.Send(data, data.Length);
                Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "向服务端 "+remotePoint.ToString()+" 发送成功");
            }
            catch
            {
                Console.WriteLine("发送失败!");
            }
        }
    }
}

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MelanceXin

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值