开发者

UDP Problem With Non-Reception

I am new to Network Programming, and I am having a problem with some code I've been testing as the basis of a LAN chat program.

Server Code:

public static void Main()
    {

        UdpClient publisher = new UdpClient("230.0.0.1", 8899);
        UdpClient subscriber = new UdpClient("230.0.0.2", 8800);
        IPAddress addr = IPAddress.Parse("230.0.0.1");
        subscriber.JoinMulticastGroup(addr);
        Console.WriteLine("Running chat program at 230.0.0.1:8899");


        while (true)
        {
            IPEndPoint ep = null;
            byte[] chats = subscriber.Receive(ref ep);
            string chatstring = Encoding.ASCII.GetString(chats);
            Console.WriteLine(chatstring);


            string msg = String.Format(chatstring);
            byte[] sdata = Encoding.ASCII.GetBytes(msg);
            publisher.Send(sdata, sdata.Length);



            System.Threading.Thread.Sleep(500);
        }
    }

And the client program:

static void Main(string[] args)
        {

            UdpClient subscriber = new UdpClient("230.0.0.1", 8899);

            IPAddress addr = IPAddress.Parse("230.0.0.1");
            subscriber.JoinMulticastGroup(addr);
            IPEndPoint ep = null;
            Thread SendChats = new Thread(Send);
            SendChats.Start();
            while (true)
            {
                byte[] receivedbytes = subscriber.Receive(ref ep);
                string receivedchats = Encoding.ASCII.GetString(receivedbytes);
                Console.WriteLine(receivedchats);
                Thread.Sleep(500);
            }
        }

        static void Send()
        {
            UdpClient publisher = new UdpClient("230.0.0.2", 8800);



        开发者_如何学C    while (true)
            {
                string msg = Console.ReadLine();
                byte[] sdata = Encoding.ASCII.GetBytes(msg);
                publisher.Send(sdata, sdata.Length);
                Thread.Sleep(400);
            }

        }

By my figuring, the server program SHOULD be receiving data from the client, but alas, after a message is typed in and delivered, there is never anything getting through. Am I missing something?


Is your subscriber joinging the wrong multicast in the server?

UdpClient subscriber = new UdpClient("230.0.0.2", 8800);
IPAddress addr = IPAddress.Parse("230.0.0.1");
subscriber.JoinMulticastGroup(addr);

Possibly should be:

IPAddress addr = IPAddress.Parse("230.0.0.2");

Also - have you got any routers/switches between the client/server as they will prevent multicast traffic unless specifically configured to do so.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜