开发者

UDP broadcast of WCF Service address

Im have a lab-environment in VMware with a WS2008R2-server and a W7-client. Im trying to broadcast a WCF-service-address from the server and receive this in the client. Im using System.Net.Sockets in C# .NET and I can successfuly send data from the server. I looks okay with WinDump at least. But when I try to receive this on the client it fails. I cant understand where the problem is..? The client can communicate with the server in other ways and with my WCF-service if I manually enter its address. I have turned of my firewalls in the lab-environment just in case.

[Update]

I checked WinDump on my client-vm and the same udp-message showes up here as well so it seem to be able to receive the broadcast. But why arent the ReceieveFrom-method returning anything? Have I setup the client socket wrong? Should it bind to the Any-address or to its local ip? Neither works...

[/Update]

Heres the server-code:

    public static class MulticastServer
{
    static Socket socket;
    static IPEndPoint ep = new IPEndPoint(IPAddress.Broadcast, 9050);

    public static void Open()
    {
        socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
        socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, true);
    }

    public static void Send(string message)
    {
        socket.SendTo(Encoding.ASCII.GetBytes(message), ep);
    }

    public static void Close()
    {
        socket.Close();
    }
}

And the client:

    public static class MulticastClient
{
    public static string ReceiveOne()
    {
        Socket socket = new Socket(AddressFamily.InterNetwork, SocketType开发者_开发百科.Dgram, ProtocolType.Udp);
        IPEndPoint ep = new IPEndPoint(IPAddress.Any, 9050);
        socket.Bind(ep);
        byte[] data = new byte[1024];
        EndPoint e = (EndPoint)ep;
        int i = socket.ReceiveFrom(data, ref e);
        socket.Close();
        return Encoding.ASCII.GetString(data, 0, i);
    }
}


I'm not sure where your current problem is but by reading your question I immediately knew that you are reinventing a wheel. Upgrade to .NET 4.0 and use WCF Discovery which is exactly for this purpose - UDP based searching for service with given contract and UDP based announcements about services. Moreover it is based on WS-Discovery protocol so I guess it should be interoperable. Isn't it better than custom solution?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜