使用c#从Panasonic PBX捕获数据

时间:2013-11-19 07:26:51

标签: c# sockets pbx

我编写了以下代码来从Panasonic PBX(KX-TDE 100/200)中捕获数据并将其写入文件。   当我尝试运行以下代码时,它在任务管理器中显示“没有响应” 我也尝试调试可能存在问题的地方。行

Socket socket = listener.Accept(); 将在调试时被点击,之后显示“Not Responding”。

PBX连接到我公司的局域网。任何配置都需要在我的局域网上完成?

我尝试使用相同的IP代码:127.0.0.1将字符串发送到客户端应用程序并且工作正常。但是当我尝试从PBX中提取数据时,它无法正常工作。

我的PBX的LAN线连接到交换机。

请让我知道我在做什么错。还请指出使用C#从PBX捕获数据的好样本。

        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); 
        }  
     }

更新此问题。
我已经编写了一个新代码来连接到PBX。使用新代码我可以连接到PBX。请在下面找到。

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);
    }

但我无法从PBX接收任何数据,除了“ - ”加上我在“sendingMessage”变量中发送的内容。例如,如果sendingMessage =“abcde”,我将收到 -abcde < / p>

此外,文档描述了如何配置PBX盒。同样,要让PBX返回数据,我们需要发送有效的凭据。如何发送此有效凭据?

2 个答案:

答案 0 :(得分:0)

不要在GUI事件处理程序中进行阻塞调用(如accept)。

断开服务器线程或使用asynch。

答案 1 :(得分:0)

您应键入“SMDR”并在“ - ”之后输入以进入smdr模式,然后键入“PCCSMDR”作为密码并输入公共Panasonic型号。