打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
C# Socket 入门3 UPD
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.Net;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // 1.创建套节字
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

            // 2.填充IP
            IPEndPoint ipe = new IPEndPoint(IPAddress.Any, 4321);

            // 3.绑定
            socket.Bind(ipe);

            // 等待客户机连接
            Console.WriteLine("This is a Server, host name is {0}", Dns.GetHostName());
            Console.WriteLine("Waiting for a client...");

            // 4.得客户机IP
            IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
            EndPoint remote = (EndPoint)sender;

            // 5.接收客户机数据
            byte[] buffer = new byte[1024];
            socket.ReceiveFrom(buffer, ref remote);

            Console.WriteLine(remote.ToString());
            Console.WriteLine(Encoding.Unicode.GetString(buffer));
            Console.ReadKey();
        }
    }
}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
c#Socket通信实例
C# Socket 网络编程
一个简单的C# UDP通讯实例(服务器端及客户端)
C# Udp Socket例子 - 一路前行 - 博客园
C# Socket编程笔记
实现了本机两个进程之间的通信 c#.net socket
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服