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.
Also I tried to debug where might be the problem.The line
Socket socket = listener.Accept(); will be hit while debugging and after that it shows "Not Responding".
The PBX is connected to LAN in my company.Any configurations need to be done on my LAN?
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.
The LAN wire from my PBX is connected to the switch.
Please let me know what mistake I am doing. Also point me to good samples on capturing data from PBX using C#.
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);
}
}
Update to this question.
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.
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);
}
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 -abcde
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?