title: PHP - C# TCP Client / Server How to send Strings ?
body: <p>Hey i have here a a C# Tcp Server and here a PHP Client both are working.</p>
  
  <p>SERVER</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Net;
  using System.Net.Sockets;
  
  namespace ConsoleApplication2
  {
      class Program
      {
  
  
          public static void Main()
          {
              try
              {
                  IPAddress ipAd = IPAddress.Parse("127.0.0.1");
                  TcpListener myList = new TcpListener(ipAd, 8001);
  
                  myList.Start();
  
                  Console.WriteLine("The server is running at port 8001...");
                  Console.WriteLine("The local End point is  :" +
                                    myList.LocalEndpoint);
                  Console.WriteLine("Waiting for a connection.....");
  
  
                  while (true)
                  {
  
                  Socket s = myList.AcceptSocket();
                  Console.WriteLine("Connection accepted from " + s.RemoteEndPoint);
  
                  byte[] b = new byte[100];
                  int k = s.Receive(b);
                  string szReceived = Encoding.ASCII.GetString(b.Take(k).ToArray());
                  Console.WriteLine(szReceived);
                  Console.WriteLine("Recieved..." + szReceived + "---");
  
                  switch (szReceived)
                  {
                      case "ping":
                          Console.WriteLine("Ping wird ausgeführt!");
                          ASCIIEncoding asen = new ASCIIEncoding();
                  s.Send(asen.GetBytes("PONG !"));
                          s.Close();
  
                          break;
  
                      case "logout":
  
                          break;
                  }
  
  
                  }
  
  
  
  
                  /* clean up */
  
                      myList.Stop();
              }
              catch (Exception e)
              {
                  Console.WriteLine("Error..... " + e.StackTrace);
              }
  
  
  
          }
  
  
  
      }
  }
  </code></pre>
  
  <p>CLIENT</p>
  
  <pre><code>    &lt;script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"&gt;&lt;/script&gt;
      &lt;script src="https://code.jquery.com/jquery-1.10.2.js"&gt;&lt;/script&gt;
  &lt;?PHP
  
  error_reporting(E_ALL);
  ini_set("display_errors", 1);
  
  $address="127.0.0.1";
  $service_port = "8001";
  
  /* Create a TCP/IP socket. */
  $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
  if ($socket === false) {
      echo "socket_create() failed: reason: &lt;br&gt;" . 
           socket_strerror(socket_last_error()) . "\n";
  }
  
  echo "Attempting to connect to '$address' on port '$service_port'...&lt;br&gt;";
  $result = socket_connect($socket, $address, $service_port);
  if ($result === false) {
      echo "socket_connect() failed.\nReason: ($result) &lt;br&gt;" . 
            socket_strerror(socket_last_error($socket)) . "\n";
  }else {
  
   echo "connected&lt;br&gt;";
  
  
  }
  
  $in = "ping";
  $out = '';
  
  echo "Client Sended: PING !&lt;br&gt;";
  
  socket_write($socket, $in, strlen($in));
  echo "SENDED!.\n&lt;br&gt;";
  
  echo "Reading response:\n\n&lt;br&gt;";
  
  while ($out = socket_read($socket, 2048)) {
  echo $out;
  }
  
  socket_close($socket);
  
  ?&gt;
  </code></pre>
  
  <p>My Question is now how can i call now my ping .. and then sending 1 or more Strings behind and catch them in my Server to Console them or whateve ? ... </p>
  
  <p>I already did this in VB earlier so i know it is possible i did it with Streamreader and Write but i guess this isnt working here ? 
  Code examples would be very helpfull</p>
  
  <p>Thanks for every Answer :)</p>