| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 960 人关注过本帖
标题:UdpClient 的用法问题,愁死了,请大家帮忙。
只看楼主 加入收藏
fjlangzi
Rank: 1
等 级:新手上路
帖 子:28
专家分:0
注 册:2008-1-2
收藏
 问题点数:0 回复次数:1 
UdpClient 的用法问题,愁死了,请大家帮忙。
这是在本机做的一个程序用作测试。本机的IP为127.0.0.1吧。
请大家帮我分析一下,这些问题让我很郁闷,心理很憋屈,这到底是怎么回事啊?
问题都在注释中。

服务器端 :
using System;
using System.Collections.Generic;
using System.Text;
using
using

namespace UDPServer
{
    class Program
    {
        static void Main(string[] args)
        {
            UdpClient receivingUdpClient = new UdpClient(12222);

            IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
            //问题一
            //0不是说只要有效的端口都可以接收数据吗?为什么客户端的端口不等于12222的时候就接收不到数据了呢?
            //问题二
            //IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Parse("124.110.0.1"), 0);//胡乱写的IP地址也能收到数据,郁闷。
            //这里的"124.110.0.1",不是指定只接收这个IP地址的数据吗?那为什么这个可以接收到数据。

            while (true)
            {
                try
                {
                    Byte[] receiveBytes = receivingUdpClient.Receive(ref RemoteIpEndPoint);
                    if (receiveBytes.GetLength(0) > 0)
                    {
                        string returnData = Encoding.ASCII.GetString(receiveBytes);
                        
                        Console.WriteLine("This is the message you received " +
                        returnData.ToString());
                        Console.WriteLine("This message was sent from " +
                        RemoteIpEndPoint.Address.ToString() +
                        " on their port number " +
                        RemoteIpEndPoint.Port.ToString());
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.ToString());
                }
            }

        }
    }

}

客户端:
using System;
using System.Collections.Generic;
using System.Text;
using
using

namespace UDPClient
{
    class Program
    {
        static void Main(string[] args)
        {
            UdpClient udpClient = new UdpClient();
            Byte[] sendBytes = Encoding.ASCII.GetBytes("Is anybody there");
            try
            {
                udpClient.Send(sendBytes, sendBytes.Length,"127.125.100.100", 12222);
                //"127.125.100.100"不是指定的目的机的IP吗?结果发现只要第一个端的地址为127都可以发送成功,服务端也都可以接收到数据。为什么?
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            Console.ReadLine();
        }
    }

}
搜索更多相关主题的帖子: UdpClient 用法 
2008-04-24 10:42
野比
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:24
帖 子:1627
专家分:516
注 册:2007-5-24
收藏
得分:0 
UDP 不在乎 IP 吧
一般是用 Listener 和 Client 一起用,一方用 Listener.AcceptSocket() 阻塞式监听,另一方用 Client.Connect() 连接
我用的 TCP,不知道 UDP

女侠,约吗?
2008-04-24 13:43
快速回复:UdpClient 的用法问题,愁死了,请大家帮忙。
数据加载中...
 
   



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

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