views:

61

answers:

1

Hello, 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);



            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?

+1  A: 

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.

Paolo
Prevention by the router? When this is only functioning between two or more computers on LAN... AND TY for the catch. I removed the address in the subscriber, and changed the addr variable.
Bloodyaugust
No probs. Router / switch issues only come into play if your trying to send multicast packets across different LAN segments. If client and server are on the same LAN then this is irrelevant.
Paolo