title: Windows 8.1 StreamSocketListener c#
body: <p>So I'm fairly new to programming and decided to try some networking but I'm stuck.  I tried to create a simple client and server just to establish a connection.  Obviously, it doesnt work, and I've looked around but can't find any <strong>new</strong> information to help me.  So I decided to try here.</p>
  
  <p>Here is what's happening when I run the client and server:</p>
  
  <p>(I'm running the client and server on the same computer)</p>
  
  <p>*Client connects to server (localhost): </p>
  
  <p>-System.Exception: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.</p>
  
  <p>*Client connects to server when server is not running: </p>
  
  <p>-System.Exception: No connection could be made because the target machine actively refused it.</p>
  
  <p>*Client connects to localhost at port 80 (which is not the server):</p>
  
  <p>-Connection established</p>
  
  <p>*Client connect to HostName google.com at port 80:</p>
  
  <p>-Connection established</p>
  
  <p>*Client connects to random ip and port:</p>
  
  <p>-System.Exception: No such host is known.</p>
  
  <p>Servercode:</p>
  
  <pre><code>public sealed partial class MainPage : Page
  {
      StreamSocketListener _listener = new StreamSocketListener();
      string port = "1800";
  
      public MainPage()
      {
          this.InitializeComponent();
          listener();
      }
      private async void listener()
      {
          _listener.ConnectionReceived += listenerConnectionReceived;
          await _listener.BindServiceNameAsync(port);
      }
      void listenerConnectionReceived(StreamSocketListener sender, StreamSocketListenerConnectionReceivedEventArgs args)
      {
          tb_server.Text = "Connection received.";
      }
      private void btn1_Click(object sender, RoutedEventArgs e)
      {
  
      }
  }
  </code></pre>
  
  <p>Clientcode:</p>
  
  <pre><code>public sealed partial class MainPage : Page
  {
      StreamSocketListener _listener = new StreamSocketListener();
      string port = "1800";
  
      public MainPage()
      {
          this.InitializeComponent();
          listener();
      }
      private async void listener()
      {
          _listener.ConnectionReceived += listenerConnectionReceived;
          await _listener.BindServiceNameAsync(port);
      }
      void listenerConnectionReceived(StreamSocketListener sender, StreamSocketListenerConnectionReceivedEventArgs args)
      {
          tb_server.Text = "Connection received.";
      }
      private void btn1_Click(object sender, RoutedEventArgs e)
      {
  
      }
  }
  </code></pre>