Multicast Chat : Chat « Network « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » Network » ChatScreenshots 
Multicast Chat
Multicast Chat

/*
C# Network Programming 
by Richard Blum

Publisher: Sybex 
ISBN: 0782141765
*/
using System;
using System.Drawing;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Windows.Forms;

public class MulticastChat : Form
{
   TextBox newText;
   ListBox results;
   Socket sock;
   Thread receiver;
   IPEndPoint multiep = new IPEndPoint(IPAddress.Parse("224.100.0.1")9050);

   public MulticastChat()
   {
      Text = "Multicast Chat Program";
      Size = new Size(400380);

      Label label1 = new Label();
      label1.Parent = this;
      label1.Text = "Enter text string:";
      label1.AutoSize = true;
      label1.Location = new Point(1030);

      newText = new TextBox();
      newText.Parent = this;
      newText.Size = new Size(200* Font.Height);
      newText.Location = new Point(1055);

      results = new ListBox();
      results.Parent = this;
      results.Location = new Point(1085);
      results.Size = new Size(36018 * Font.Height);

      Button sendit = new Button();
      sendit.Parent = this;
      sendit.Text = "Send";
      sendit.Location = new Point(220,52);
      sendit.Size = new Size(* Font.Height, * Font.Height);
      sendit.Click += new EventHandler(ButtonSendOnClick);

      Button closeit = new Button();
      closeit.Parent = this;
      closeit.Text = "Close";
      closeit.Location = new Point(29052);
      closeit.Size = new Size(* Font.Height, * Font.Height);
      closeit.Click += new EventHandler(ButtonCloseOnClick);

      sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
      IPEndPoint iep = new IPEndPoint(IPAddress.Any, 9050);
      sock.Bind(iep);
      sock.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(IPAddress.Parse("224.100.0.1")));
      receiver = new Thread(new ThreadStart(packetReceive));
      receiver.IsBackground = true;
      receiver.Start();
   }

   void ButtonSendOnClick(object obj, EventArgs ea)
   {
      byte[] message = Encoding.ASCII.GetBytes(newText.Text);
      newText.Clear();
      sock.SendTo(message, SocketFlags.None, multiep);
   }

   void ButtonCloseOnClick(object obj, EventArgs ea)
   {
      receiver.Abort();
      sock.Close();
      Close();
   }

   void packetReceive()
   {
      EndPoint ep = (EndPoint)multiep;
      byte[] data = new byte[1024];
      string stringData;
      int recv;
      while (true)
      {
         recv = sock.ReceiveFrom(data, ref ep);
         stringData = Encoding.ASCII.GetString(data, 0, recv);
         results.Items.Add("from " + ep.ToString() ":  " + stringData);
      }
   }

   public static void Main()
   {
      Application.Run(new MulticastChat());
   }
}

           
       
Related examples in the same category
1.Tcp ChatTcp Chat
2.New Tcp ChatNew Tcp Chat
3.Chat ApplicationChat Application
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.