C#实现UDP通信

C#实现UDP通信

client:

using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
 
namespace udptest
{
    class Program
    {
        static void Main(string[] args)
        {
            byte[] data = new byte[1024];
            string input, stringData;
 
            //构建TCP 服务器
            Console.WriteLine("This is a Client, host name is {0}", Dns.GetHostName());
 
            //设置服务IP,设置TCP端口号
            IPEndPoint ip = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8001);
 
            //定义网络类型,数据连接类型和网络协议UDP
            Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
 
            string welcome = "你好! ";
            data = Encoding.UTF8.GetBytes(welcome);
            server.SendTo(data, data.Length, SocketFlags.None, ip);
 
            EndPoint sender = new IPEndPoint(IPAddress.Any, 0);
            data = new byte[1024];
            int recv = 0;
 
            try
            {
                recv = server.ReceiveFrom(data, ref sender);
                Console.WriteLine("Message received from {0}: ", sender.ToString());
                Console.WriteLine(Encoding.UTF8.GetString(data, 0, recv));
            }
            catch (ArgumentNullException e)
            {
                Console.WriteLine(e.Message);
            }
            catch (SocketException e)
            {
                Console.WriteLine(e.Message);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
 
            while (true)
            {
                input = Console.ReadLine();
                if (input == "exit")
                    break;
 
                server.SendTo(Encoding.UTF8.GetBytes(input), sender);
                data = new byte[1024];
                recv = server.ReceiveFrom(data, ref sender);
                stringData = Encoding.UTF8.GetString(data, 0, recv);
                Console.WriteLine(stringData);
            }
 
            server.Close();
            Console.WriteLine("Stopping Client.");
            Console.ReadKey();
        }
    }
}

server:

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
 
namespace server
{
    class Program
    {
        static void Main(string[] args)
        {
            int recv;
            byte[] data = new byte[1024];
 
            //得到本机IP,设置TCP端口号         
            IPEndPoint ip = new IPEndPoint(IPAddress.Any, 8001);
            Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
 
            //绑定网络地址
            newsock.Bind(ip);
 
            Console.WriteLine("This is a Server, host name is {0}", Dns.GetHostName());
 
            //等待客户机连接
            Console.WriteLine("Waiting for a client");
 
            //得到客户机IP
            IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
            EndPoint Remote = (EndPoint)(sender);
            recv = newsock.ReceiveFrom(data, ref Remote);
            Console.WriteLine("Message received from {0}: ", Remote.ToString());
            Console.WriteLine(Encoding.UTF8.GetString(data, 0, recv));
 
            //客户机连接成功后,发送信息
            string welcome = "你好 ! ";
 
            //字符串与字节数组相互转换
            data = Encoding.UTF8.GetBytes(welcome);
 
            //发送信息
            newsock.SendTo(data, data.Length, SocketFlags.None, Remote);
 
            while (true)
            {
                data = new byte[1024];
 
                //发送接收信息
                recv = newsock.ReceiveFrom(data, ref Remote);
 
                Console.WriteLine(Encoding.UTF8.GetString(data, 0, recv));
                newsock.SendTo(data, recv, SocketFlags.None, Remote);
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值