title: System.Net.Sockets.SocketException (0x80004005): An existing connection was forcibly closed by the remote host
body: <p>This is my code for a simple chat program that works client to client. The problem is that when i click the send button an error is thrown.</p>
  
  <p>The error is...</p>
  
  <p>System.Net.Sockets.SocketException (0x80004005): An existing connection was forcibly closed by the remote host at System.Net.Sockets.SocketEndRecieveFrom(IAsyncResult asyncResult, EndPoint&amp; endPoint) at ChatClientV1.MainForm.MessageCallBack(IAsynResult aResult)...line 36 below which is ...</p>
  
  <p>int size = sck.EndReceiveFrom(aResult, ref epRemote);</p>
  
  <pre><code>    public MainForm()
      {
          InitializeComponent();
  
          sck = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
          sck.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
  
          //localIP = GetLocalIP();
          //friendIP = GetLocalIP();
          localIP = "192.168.0.17";
          friendIP = "192.168.0.17";
          friendPortNumber = "81";
          localPortNumber = "80";
          username = "Shawn";
      }
  
      private string GetLocalIP()
      {
          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[] recieveData = new byte[1464];
  
                  recieveData = (byte[])aResult.AsyncState;
  
                  ASCIIEncoding eEncoding = new ASCIIEncoding();
                  string recieveMessage = eEncoding.GetString(recieveData);
  
                  messageListBox.Items.Add(username+": "+recieveMessage);
              }
  
              byte[] buffer = new byte[1500];
              sck.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref epRemote, new AsyncCallback(MessageCallBack), buffer);
  
          }
          catch (Exception exp)
          {
              MessageBox.Show(exp.ToString());
          }
      }
  
      private void currentSettingsToolStripMenuItem_Click(object sender, EventArgs e)
      {
          CurrentSettingsFrom aCurrentSettingsForm = new CurrentSettingsFrom();
          aCurrentSettingsForm.ShowDialog();
      }
  
      private void sendFileToolStripMenuItem_Click(object sender, EventArgs e)
      {
          SendFileForm aSendFileForm = new SendFileForm();
          aSendFileForm.ShowDialog();
      }
  
      private void exitToolStripMenuItem_Click(object sender, EventArgs e)
      {
          this.Close();
      }
  
      private void appearanceToolStripMenuItem_Click(object sender, EventArgs e)
      {
          AppearanceForm aAppearanceFrom = new AppearanceForm();
          aAppearanceFrom.ShowDialog();
      }
  
      private void connectButton_Click(object sender, EventArgs e)
      {
          try
          {
              eplocal = new IPEndPoint(IPAddress.Parse(localIP), Convert.ToInt32(localPortNumber));
              sck.Bind(eplocal);
  
              epRemote = new IPEndPoint(IPAddress.Parse(friendIP), Convert.ToInt32(friendPortNumber));
              sck.Connect(epRemote);
  
              byte[] buffer = new byte[1500];
              sck.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref epRemote, new AsyncCallback(MessageCallBack), buffer);
  
              connectButton.Text = "Connected";
              connectButton.Enabled = false;
              sendMessageButton.Enabled = true;
              messageEntryField.Focus();
          }
          catch (Exception ex)
          {
              MessageBox.Show(ex.ToString());
          }
      }
  
      private void sendMessageButton_Click(object sender, EventArgs e)
      {
          try
          {
              System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
              byte[] msg = new byte[1500];
              msg = enc.GetBytes(messageEntryField.Text);
  
              sck.Send(msg);
              messageListBox.Items.Add(username + ": " + messageEntryField.Text);
              messageEntryField.Clear();
          }
          catch (Exception ex)
          {
              MessageBox.Show(ex.ToString());
          }
      }
  }
  </code></pre>
  
  <p>}</p>