Socket编程-UDP协议

前两天主要学习了一下tcp协议,今天有空又把udp协议也熟悉了一下。

对于tcp和udp的区别,这里大概提一下。

tcp主要是面向连接的协议,也就是在通信前需要先建立一条从服务器端到客户端的链路。tcp传输数据是数据流的形式。

udp是无连接的协议,在通信前不需要建立一条链路,但需要指定你发送的目标位置。发送的数据以数据报的形式传输。

tcp是一个可靠的传输协议。udp是不可靠的,可能会产生丢包现象,但是udp占用资源要少。

具体的区别百度上讲的很清楚。不多讲了。

由于udp是无连接的所以也就没有服务器与客户端之分了。只是发送和接受数据之分。接受数据的需要绑定自己的ip和端口号,这样别的客户端才能找到你。

接受端(可以理解为服务端):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Socket编程_UDP协议_服务器端
{
    class Program
    {
        private static Socket udpServer;
        static void Main(string[] args)
        {
            //[1]创建Socket对象,udp数据采用数据报的形式传输
            udpServer=new Socket(AddressFamily.InterNetwork,SocketType.Dgram,ProtocolType.Udp);
            //[2]绑定IP跟端口号
            udpServer.Bind(new IPEndPoint(IPAddress.Parse("172.25.14.165"), 7788));
            //[3]接受数据 udp是面向无连接的所以无需监听。直接通过ip和端口号,接受和发送数据报
            new Thread(ReceiveMessage){IsBackground = true}.Start();//开启线程
            Console.ReadLine();
        }

        private static void ReceiveMessage()
        {
            while (true)
            {
                byte[] data = new byte[1024];
                EndPoint remotePoint = new IPEndPoint(IPAddress.Any, 0); //远程的ip和端口号,即客户端 
                int length = udpServer.ReceiveFrom(data, ref remotePoint); //ref 指着这个方法可修改remotePoint的参数
                string message = Encoding.UTF8.GetString(data);
                Console.WriteLine("从ip:" + (remotePoint as IPEndPoint).Address + ":" + (remotePoint as IPEndPoint).Port +
                                  "接受了数据:" + message);
            }
        }
    }
}

发送端(客户端):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace Socket_UDP协议_客户端
{
    class Program
    {
        static void Main(string[] args)
        {
            //[1]创建Socket
            Socket udpClient = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

            while (true)
            {
                //[2]发送数据
                string message = Console.ReadLine();
                byte[] data = new byte[1024];
                data = Encoding.UTF8.GetBytes(message);
                //发送目标位置
                EndPoint serverPoint = new IPEndPoint(IPAddress.Parse("172.25.14.165"), 7788);
                udpClient.SendTo(data, serverPoint);
            }
            udpClient.Close();
            Console.ReadKey();
        }
    }
}

大概就是这样了。比Tcp的逻辑要简单点。后面还会有对于Socket封装好的类的使用。敬请关注了。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值