标题:udpclient类的应用实例
取消只看楼主
weiwei2023
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2023-4-12
结帖率:0
已结贴  问题点数:20 回复次数:0 
udpclient类的应用实例
using System;
using
using
using System.Text;
namespace UdpClientServer
{
    class class1
    {
        static void Main()
        {
            StarListener();
            Console.ReadLine();
        }
        private static void StarListener()
        {
            UdpClient udpServer=new UdpClient(8000);
            IPEndPoint myHost=null;
            {
                while(true)
                {
                    try
                    Console.WriteLine("等待接受...");
                    byte[]getBytes=udpServer.Receive(ref myHost);
                    string getString=Encoding.Unicode.GetString(getBytes,0,getBytes.Length);
                    Console.WriteLine("接受信息:{0}",getString);
                    if(getString=="quite")break;
                    string sendString="你好,多保重!";
                    Console,WriteLine("发送消息:{0}",sendString);
                    byte[]sendBytes=Encoding.Unicode.GetBytes(sendString);
                    udpServer.Send(sendBytes,sendBytes.Length,"127.0.0.1",8001);
                }
                udpServer.Close();
                Console.WriteLine("对方已经退出,请按回车键退出。");
            }
            catch(Exception err)
            {
                Console.WriteLine(err.ToString());
            }
        }
    }
}
搜索更多相关主题的帖子: using 实例 System WriteLine Console 
2023-04-12 13:06



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-511886-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.053959 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved