作为这个问题我已经在本地计算机上运行了解决方案,但没有在网络上的计算机上运行。

除了基础知识之外,我不太了解套接字,所以请耐心等待。目标是让客户端在本地网络上查找服务器,这是一些剪切/粘贴/编辑代码的结果。

这是客户端代码:

IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 10294);

    byte[] data = new byte[1024];
    public Form1()
    {
        InitializeComponent();

    }

    private void button1_Click(object sender, EventArgs e)
    {

        Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
        client.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, 10);
        string welcome = "What's your IP?";
        data = Encoding.ASCII.GetBytes(welcome);
        client.SendTo(data, data.Length, SocketFlags.None, ipep);
        IPEndPoint server = new IPEndPoint(IPAddress.Any, 0);
        EndPoint tmpRemote = (EndPoint)server;
        data = new byte[1024];
        int recv = client.ReceiveFrom(data, ref tmpRemote);
        this.IP.Text = ((IPEndPoint)tmpRemote).Address.ToString(); //set textbox
        this.Port.Text = Encoding.ASCII.GetString(data, 0, recv); //set textbox
        client.Close();
    }

这是服务器代码:

int recv;
        byte[] data = new byte[1024];
        IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 10294);
        Socket newsock = new Socket(AddressFamily.InterNetwork,
                SocketType.Dgram, ProtocolType.Udp);
        newsock.Bind(ipep);
        newsock.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(IPAddress.Any,IPAddress.Parse("127.0.0.1")));

        while (true)
        {
            Console.WriteLine("Waiting for a client...");
            IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
            EndPoint tmpRemote = (EndPoint)(sender);
            data = new byte[1024];
            recv = newsock.ReceiveFrom(data, ref tmpRemote);
            Console.WriteLine("Message received from {0}:", tmpRemote.ToString());
            Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));
            string welcome = "7010";
            data = Encoding.ASCII.GetBytes(welcome);
            newsock.SendTo(data, data.Length, SocketFlags.None, tmpRemote);
        }

它可以在我的本地计算机(服务器和客户端)上查找,但当我在同一网络上尝试另一台计算机时,我得到“现有连接被远程主机强行关闭”

我意识到我需要添加很多try / catch,但我只是想先了解一下这是如何工作的。

有帮助吗?

解决方案

我必须首先说我对C#一无所知,但是......

查看客户端代码中 ipep 的定义,看起来您正在尝试将数据发送给自己,而不是广播它(正如您在其他问题中所建议的那样) 。引起我注意的是“127.0.0.1”。是“localhost”的地址。

这可以解释为什么当你在一台机器上运行客户端和服务器时它运行良好,因为它发送给自己。

我希望正确的端点用于广播地址(例如“255.255.255.255”) - 尽管您也可以选择您所在的本地网络的广播地址,具体取决于您的广泛地址希望播出。

其他提示

IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 10294);

应该成为:

IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("255.255.255.255"), 10294);

newsock.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(IPAddress.Any, IPAddress.Parse("127.0.0.1")));

应该成为

newsock.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(IPAddress.Any, IPAddress.Parse("255.255.255.255")));

我想。

好的,这不起作用,所以还有一些问题。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top