Hello World

RGKC4KXB.png

看不清? “右键,在新标签页打开图片” 会吗?

**我的项目(服务端):7 Socket-UDP Practice.zip
**我的项目(游戏端):_7 Socket-UDP Practice.zip


总结心得

(服务端总结)

private static EndPoint getIPandPORT = new  IPEndPoint(IPAddress.Any, 0); 
//指定监听对象,面向所有端口和地址!!!

//udp 协议不需要连接

int length = t.ReceiveFrom(data, ref getIPandPORT);  //当收到消息的时候才会继续执行 
//上面应该定义一下 GetIPandPORT 为 IPENDPOINT(IPAddress.Any,0)

t = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
需要Bind方法,不需要Listen
(获取本地IP地址方法)
        static string GetlocalIp()
        {
            string AddressIP = String.Empty;
            foreach (IPAddress _IPAdddress in Dns.GetHostEntry(Dns.GetHostName()).AddressList)
            {
                if (_IPAdddress.AddressFamily.ToString() == "InterNetwork")
                {
                    AddressIP = _IPAdddress.ToString();
                }
            }
            return AddressIP;
        }

(游戏端总结)

    private Socket t = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
    private EndPoint e = new IPEndPoint(IPAddress.Parse("192.168.43.160"), 7788);  //这里定义向固定主机发送信息
    private EndPoint server = new IPEndPoint(IPAddress.Any, 0); //这里定义接收信息的范围

好好学习,天天向上

标签: Windows, C#, Server, Socket

仅有一条评论

  1. 凌寒 凌寒

    小能好厉害qwq

添加新评论