title: capture data from Panasonic PBX using c#
body: <p>I have written the below code to capture data from Panasonic PBX(KX-TDE 100/200) and write it to a file.
    When I try to run the below code, it shows "not responding" in the Task Manager.<br>
  Also I tried to debug where might be the problem.The line  </p>
  
  <p><strong>Socket socket = listener.Accept();</strong> will be hit while debugging and after that it shows "Not Responding".  </p>
  
  <p>The PBX is connected to LAN in my company.Any configurations need to be done on my LAN?</p>
  
  <p>I tried the same code for IP:127.0.0.1 to send a string to client app and it worked.But when I tried to pull data from PBX, its not working. </p>
  
  <p>The LAN wire from my PBX is connected to the switch.   </p>
  
  <p>Please let me know what mistake I am doing. Also point me to good samples on capturing data from PBX using C#.</p>
  
  <pre><code>        private void btnstartserver_Click(object sender, EventArgs e)
      {
  
          int portno = Convert.ToInt32(txtportnum.Text);
          byte[] receivedBytes = new byte[1024];
          IPHostEntry ipHost = Dns.GetHostEntry(Dns.GetHostName());
          IPAddress ipAddress = ipHost.AddressList[0];
          IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, portno);//2112
          txtboxstatus.Text = "Creating socket object...";
  
          Socket listener = new Socket(AddressFamily.InterNetworkV6,
                                      SocketType.Stream,
                                      ProtocolType.Tcp);
  
          listener.Bind(ipEndPoint);
          listener.Listen(10);
          txtboxstatus.AppendText("Listening on " + ipHost.AddressList[0].ToString() + ":" + portno.ToString() + "\r\n");
  
  
          Socket socket = listener.Accept();
  
          txtboxstatus.AppendText ( "\n Connected with ..." + ipEndPoint.Port);
  
          string receivedvalue = string.Empty;
  
  
          receivedvalue = ReadMessage(socket);
          txtboxstatus.AppendText("\n Message read.....trying to write to the file...");
          //writing the received value from client into a file.
          try
          {
              FileStream fs = new FileStream("E:/Demo/IpData/Call.txt", FileMode.Create);
              StreamWriter sw = new StreamWriter(fs);
              sw.Write(receivedvalue);
              sw.Dispose();
              fs.Dispose();
          }
          catch (Exception ex)
          {
              txtclient.AppendText(ex.Message); 
          }  
       }
  </code></pre>
  
  <p>Update to this question.<br>
  I have written a new code to connect to PBX.With the new code I was able to connect to the PBX.Please find below.</p>
  
  <pre><code>private void btnTx_Click(object sender, EventArgs e)
      {
          byte[] receivedBytes = new byte[1024];
  
          int portno = Convert.ToInt32(txtportnum.Text);
          IPHostEntry ipHost = Dns.GetHostByName("192.168.x.yyy");
          IPAddress ipAddress = ipHost.AddressList[0];
          IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, portno);
             txtstatus.Text = "Creating socket object...";
  
          Socket send_soc = new Socket(AddressFamily.InterNetwork,
                                      SocketType.Stream,
                                      ProtocolType.Tcp);
  
          send_soc.Connect(ipEndPoint); 
          txtstatus.AppendText("\nSuccessfully connected to:" + "\t" + send_soc.RemoteEndPoint);
          txtstatus.AppendText("\nConnecting via :" + "\t" + send_soc.LocalEndPoint);
  
          string sendingMessage = "abcde";
  
  
  
  
  
          SendMessage(send_soc, sendingMessage);
  
          int totalBytesReceived = send_soc.Receive(receivedBytes);
  
          string dff = "";
          string s = System.Text.ASCIIEncoding.ASCII.GetString(receivedBytes, 0, totalBytesReceived);
  
          txtRx.AppendText(s);
          send_soc.Shutdown(SocketShutdown.Both);
          send_soc.Close();
          try
          {
              FileStream dr = new FileStream("E:/Demo/IpData/Call.txt", FileMode.OpenOrCreate, FileAccess.Read, FileShare.ReadWrite);
              StreamReader fg = new StreamReader(dr);
              string df = fg.ReadToEnd();
              dff = df;
              fg.Dispose();
              dr.Dispose();
          }
          catch (Exception dfjdfs)
          {
              throw dfjdfs;
          }
          try
          {
              File.Delete("E:/Demo/IpData/Call.txt");
          }
          catch (Exception jhu)
          {
              throw jhu;
          }
          try
          {
              FileStream cd = new FileStream("E:/Demo/IpData/Call.txt", FileMode.Create);
              StreamWriter cdf = new StreamWriter(cd);
              cdf.Write(dff);
              cdf.Write(s);
              cdf.Dispose();
              cd.Dispose();
          }
          catch (Exception hgy)
          {
              throw hgy;
          }
  
      }
  
      static void SendMessage(Socket socket, string msg)
      {
          byte[] data = System.Text.ASCIIEncoding.ASCII.GetBytes(msg);
  
          socket.Send(data);
      }
  </code></pre>
  
  <p>But I am not able to receive any data from PBX except "-" plus whatever I send in "sendingMessage" variable.For example if sendingMessage="abcde",I will receive <strong>-abcde</strong></p>
  
  <p>Also the documentation is describing how to configure PBX box.Also for the PBX to return the data, we need to send valid credentials. How to send this valid credentials?</p>