title: Visual Studio 2017 C# Sockets UDP
body: <p>Im having a hard time figuring this out.
  I'm trying to make a chat where there's one server and multiple clients but I can't seem to get it to work.</p>
  
  <p>Im using Visual Studio 2017 and it's a Windows Forms App .NET Framework project. Now, it works but only 2 clients can connect to each other. If I try to connect two users to one, one gets blocked out of the conversation and can't recive messages from the server.</p>
  
  <p>To the question: Is it possible to make a server that many clients can connect to and the server can send messages to all connected clients?</p>
  
  <p>Code:  </p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.Windows.Forms;
  using System.Net;
  using System.Net.Sockets;
  
  namespace ChatProgram
  {
  public partial class Window : Form
  {
      Socket sck;
      EndPoint epLocal, epRemote;
  
      public Window()
      {
  
          InitializeComponent();
  
          sck = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); //Starts New Socket In InternNetwork Dgram Protocoltype UDP
          sck.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
  
          IPMe.Text = GetLocalIP(); //Sets My IP Bar To My Local IP
  
      }
  
      private string GetLocalIP() //Gets Local IP From Computer
      {
          Send.Enabled = false;
          IPHostEntry host;
          host = Dns.GetHostEntry(Dns.GetHostName());
  
          foreach (IPAddress ip in host.AddressList)
          {
              if (ip.AddressFamily == AddressFamily.InterNetwork)
              {
                  return ip.ToString();
              }
          }
  
          return "127.0.0.1";
      }
  
      private void MessageCallBack(IAsyncResult aResult)
      {
          try
          {
              int size = sck.EndReceiveFrom(aResult, ref epRemote);
              if (size &gt; 0)
              {
                  byte[] recivedData = new byte[1464];
                  recivedData = (byte[])aResult.AsyncState;
                  ASCIIEncoding eEncoding = new ASCIIEncoding();
                  string recivedMessage = eEncoding.GetString(recivedData);
                  MessageBox.Items.Add("" + recivedMessage); //Paste Messages In MessageBox listMessage.Items.Add(""+recivedMessage);
              }
  
              byte[] buffer = new byte[1500];
              sck.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref epRemote, new AsyncCallback(MessageCallBack), buffer);
  
          }
          catch (Exception exp)
          {
              System.Windows.Forms.MessageBox.Show(exp.ToString());
          }
      }
  
      private void Connect_Click(object sender, EventArgs e)
      {
          try
          {
              epLocal = new IPEndPoint(IPAddress.Parse(IPMe.Text), Convert.ToInt32(PortMe.Text)); //MyPort And MyIP Make Port to Int32
              sck.Bind(epLocal); //Socket Bind To epLocal
  
              epRemote = new IPEndPoint(IPAddress.Parse(IpConnectingTo.Text), Convert.ToInt32(PortConnectingTo.Text)); //MyPort And MyIP Make Port to Int32
              sck.Connect(epRemote); //Socket Bind To epRemote
  
              byte[] buffer = new byte[1500];
              sck.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref epRemote, new AsyncCallback(MessageCallBack), buffer);
  
              Connect.Text = "Connected"; //Set Text To Connected
              Connect.Enabled = false; //Disabled (Gray)
              Send.Enabled = true; //Enable Send Button
              TextBoxSendMessages.Focus(); //Set Focus To This!
  
          }
          catch (Exception ex)
          {
              System.Windows.Forms.MessageBox.Show(ex.ToString());
          }
      }
  
      private void Send_Click(object sender, EventArgs e)
      {
          try
          {
              System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
              byte[] msg = new byte[1500];
              string nickandtext = NickNameMe.Text + ": " + TextBoxSendMessages.Text;
  
              msg = enc.GetBytes(nickandtext); //Messages The Sum Of Both
              //msg = enc.GetBytes(NickNameMe.Text + TextBoxSendMessages.Text);
  
              sck.Send(msg); //Send Message In Bytes
              MessageBox.Items.Add(NickNameMe.Text + ": " + TextBoxSendMessages.Text); //See yourself msg in msgbox
              TextBoxSendMessages.Clear(); //Clear Text Message Box
          }
          catch (Exception ex)
          {
              System.Windows.Forms.MessageBox.Show(ex.ToString());
          }
      }
  
      private void IPMe_TextChanged(object sender, EventArgs e)
      {
  
      }
  
      private void MessageBox_SelectedIndexChanged(object sender, EventArgs e)
      {
  
      }
  
      private void label2_Click(object sender, EventArgs e)
      {
  
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Picture of program:
  <a href="https://i.stack.imgur.com/2608X.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/2608X.png" alt="SimChat"></a></p>
  
  <p><a href="https://i.stack.imgur.com/W8qrF.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/W8qrF.png" alt="enter image description here"></a></p>