c#和android之间的TCP双向通信

时间:2014-01-07 08:54:08

标签: android tcp bidirectional

我尝试使用eneter库

通过TCPp连接Pc和Android设备

当我从Android设备向PC发送数据时,数据被传输并立即收到回复给Android但我在将数据从PC传输到Android设备时出现问题

使用以下代码,PC正在向自己发送数据,因为我收到了关于不在android中的C#代码的OnMessageReceived过程的数据

    public NCProgram()
    {
        aReceiverFactory    = new DuplexTypedMessagesFactory();
        myReceiver  = aReceiverFactory.CreateDuplexTypedMessageReceiver<MyResponse, MyRequest>();
        mySender = aReceiverFactory.CreateDuplexTypedMessageSender<MyResponse, MyRequest>();

        // Subscribe to handle messages.
        myReceiver.MessageReceived += OnMessageReceived;
        mySender.ResponseReceived += OnResponseReceived;

        // Create TCP messaging.
        IMessagingSystemFactory aMessaging = new TcpMessagingSystemFactory();
        IDuplexInputChannel anInputChannel = aMessaging.CreateDuplexInputChannel("tcp://192.168.173.1:6060/");
        IDuplexOutputChannel anOutputChannel = aMessaging.CreateDuplexOutputChannel("tcp://192.168.173.1:6060/");

        // Attach the input channel and start to listen to messages.
        myReceiver.AttachDuplexInputChannel(anInputChannel);

        // Attach output channel to the sender and be able to send
        // request messages and receive responses.
        mySender.AttachDuplexOutputChannel(anOutputChannel);

        m_TexteRebut = "Comunicació establerta";
    }

    public void Enviar()
    {
        MyRequest aRequestMessage = new MyRequest();
        aRequestMessage.Text = m_Texte_Enviar;
        mySender.SendRequestMessage(aRequestMessage);
    }

如果我不使用outputchannel并且我发送消息就好像它是来自android设备的通信的答案,那么数据被传输到android设备但是有时需要一秒钟,有时候需要一些一些,有时20或30秒。

    public NCProgram()
    {
        aReceiverFactory    = new DuplexTypedMessagesFactory();
        myReceiver  = aReceiverFactory.CreateDuplexTypedMessageReceiver<MyResponse, MyRequest>();
        //mySender = aReceiverFactory.CreateDuplexTypedMessageSender<MyResponse, MyRequest>();

        // Subscribe to handle messages.
        myReceiver.MessageReceived += OnMessageReceived;
        //mySender.ResponseReceived += OnResponseReceived;

        // Create TCP messaging.
        IMessagingSystemFactory aMessaging = new TcpMessagingSystemFactory();
        IDuplexInputChannel anInputChannel = aMessaging.CreateDuplexInputChannel("tcp://192.168.173.1:6060/");
        //IDuplexOutputChannel anOutputChannel = aMessaging.CreateDuplexOutputChannel("tcp://192.168.173.1:6060/");

        // Attach the input channel and start to listen to messages.
        myReceiver.AttachDuplexInputChannel(anInputChannel);

        // Attach output channel to the sender and be able to send
        // request messages and receive responses.
        //mySender.AttachDuplexOutputChannel(anOutputChannel);

        m_TexteRebut = "Comunicació establerta";
    }

    public void Enviar()
    {
        MyRequest aRequestMessage = new MyRequest();
        aRequestMessage.Text = m_Texte_Enviar;
        myReceiver.SendResponseMessage(IPAddress,aRequestMessage);
    }

有关如何解决从PC到Android设备的通信的任何建议将不胜感激

0 个答案:

没有答案