标题:udpclient类应用实例 客户机程序
只看楼主
weiwei2023
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2023-4-12
结帖率:0
 问题点数:0 回复次数:0 
udpclient类应用实例 客户机程序
using System;
using
using
namespace testUdpClient
{
    class Class1
    {
        static void Main(string[]args)
        {
             string sendString="你好,继续努力";
             Send(sendString);
             Send("quit");
             Console.ReadLine();
        }
        private static void Send(string message)
        {
            UdpClient udpClient=new udpClient(8001);
            try
            {
                Console.WriteLine("向服务器发送数据:{0}",message);
                byte[]sendBytes=System.Text.Encoding.Unicode.GetBytes(message);
                udpClient.Send(sendBytes,sendBytes.Length,"127.0.0.1",8000);
                if(message=="quit")
                {
                    Console.WriteLine("已经向对方发送quit消息,请按回车键退出程序。");
                    return;
                }
                IPEndPoint myHost=null;
                byte[]getBytes=udpClient.Receive(ref myHost);
                string getString=System.Text.Encoding.Unicode.GetString(getBytes);
                Console.WriteLine("接收消息:{0}",getString);
                udpClient.Close();
            }
            catch(Exception err)
            {
                Console.WriteLine(err.ToString());
            }
        }
    }
}
收到的鲜花
  • 龙胆草2023-04-18 10:05 送鲜花  1朵   附言:感谢分享!
搜索更多相关主题的帖子: WriteLine string System Console Send 
2023-04-12 13:35



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




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

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