Click here to Skip to main content
15,883,870 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
Hi all,

I have implemented Ondrej's method using TCP/IP connection and had it working but lost the source code and started once again from scratch. But this time, I cannot get it to work. Can anyone tell me what I am doing wrong?

Here is the code in C#:

// Request message type
    public class MyRequest
    {
        public string Text { get; set; }
        public bool Status { get; set; }
    }

    // Response message type
    public class MyResponse
    {
        public int Length { get; set; }
    }

    class Program
    {
        private static IDuplexTypedMessageReceiver<MyResponse, MyRequest> myReceiver;

        static void Main(string[] args)
        {
            // Create message receiver receiving 'MyRequest' and receiving 'MyResponse'.
            IDuplexTypedMessagesFactory aReceiverFactory = new DuplexTypedMessagesFactory();
            myReceiver = aReceiverFactory.CreateDuplexTypedMessageReceiver<MyResponse, MyRequest>();

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

            // Create TCP messaging.
            IMessagingSystemFactory aMessaging = new TcpMessagingSystemFactory();
            IDuplexInputChannel anInputChannel =
               aMessaging.CreateDuplexInputChannel("tcp://XXX.XXX.XXX.X:8060/");

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

            Console.WriteLine("The service is running. To stop press enter.");
            Console.ReadLine();

            // Detach the input channel and stop listening.
            // It releases the thread listening to messages.
            myReceiver.DetachDuplexInputChannel();
        }

        // It is called when a message is received.
        private static void OnMessageReceived(object sender,
              TypedRequestReceivedEventArgs<MyRequest> e)
        {
            Console.WriteLine("Received: " + e.RequestMessage.Text);

            // Create the response message.
            MyResponse aResponse = new MyResponse();
            aResponse.Length = e.RequestMessage.Text.Length;

            // Send the response message back to the client.
            myReceiver.SendResponseMessage(e.ResponseReceiverId, aResponse);
        }
    }


This is the java code:

// Request message type
	    // The message must have the same name as declared in the service.
	    // Also, if the message is the inner class, then it must be static.
	    public static class MyRequest
	    {
	        public String Text;
	    }

	    // Response message type
	    // The message must have the same name as declared in the service.
	    // Also, if the message is the inner class, then it must be static.
	    public static class MyResponse
	    {
	        public int Length;
	    }
	    
	    // UI controls
	    private Handler myRefresh = new Handler();
	    private EditText myMessageTextEditText;
	    private EditText myResponseEditText;
	    private Button mySendRequestBtn;
	    
	    
	    // Sender sending MyRequest and as a response receiving MyResponse.
	    private IDuplexTypedMessageSender<MyResponse, MyRequest> mySender;
	    
	    /** Called when the activity is first created. */
	    @Override
	    public void onCreate(Bundle savedInstanceState)
	    {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.main);
	        
	        // Get UI widgets.
	        myMessageTextEditText = (EditText) findViewById(R.id.inputPinNo);
	        myResponseEditText = (EditText) findViewById(R.id.editText);
	        mySendRequestBtn = (Button) findViewById(R.id.SendDetailsBtn);
	        
	        // Subscribe to handle the button click.
	        mySendRequestBtn.setOnClickListener(myOnSendRequestClickHandler);
	        
	        // Open the connection in another thread.
	        // Note: From Android 3.1 (Honeycomb) or higher
	        //       it is not possible to open TCP connection
	        //       from the main thread.
	        Thread anOpenConnectionThread = new Thread(new Runnable()
	            {
	                @Override
	                public void run()
	                {
	                    try
	                    {
	                        openConnection();
	                    }
	                    catch (Exception err)
	                    {
	                        EneterTrace.error("Open connection failed.", err);
	                    }
	                }
	            });
	        anOpenConnectionThread.start();
	    }
	    
	    @Override
	    public void onDestroy()
	    {
	        // Stop listening to response messages.
	        mySender.detachDuplexOutputChannel();
	        
	        super.onDestroy();
	    } 
	    
	    private void openConnection() throws Exception
	    {
	        // Create sender sending MyRequest and as a response receiving MyResponse
	        IDuplexTypedMessagesFactory aSenderFactory =
	           new DuplexTypedMessagesFactory();
	        mySender = aSenderFactory.createDuplexTypedMessageSender(MyResponse.class, MyRequest.class);
	        
	        // Subscribe to receive response messages.
	        mySender.responseReceived().subscribe(myOnResponseHandler);
	        
	        // Create TCP messaging for the communication.
	        // Note: 10.0.2.2 is a special alias to the loopback (127.0.0.1)
	        //       on the development machine
	        IMessagingSystemFactory aMessaging = new TcpMessagingSystemFactory();
	        IDuplexOutputChannel anOutputChannel = 
	           aMessaging.createDuplexOutputChannel("tcp://XXX.XXX.XXX.X:8060/");
	        
	        // Attach the output channel to the sender and be able to send
	        // messages and receive responses.
	        mySender.attachDuplexOutputChannel(anOutputChannel);
	    }
	    
	    private void onSendRequest(View v)
	    {
	        // Create the request message.
	        MyRequest aRequestMsg = new MyRequest();
	        aRequestMsg.Text = myMessageTextEditText.getText().toString();
	        
	        // Send the request message.
	        try
	        {
	            mySender.sendRequestMessage(aRequestMsg);
	        }
	        catch (Exception err)
	        {
	            EneterTrace.error("Sending the message failed.", err);
	        }
	    }
	    
	    private void onResponseReceived(Object sender, final TypedResponseReceivedEventArgs<MyResponse> e)
	    {
	        // Display the result - returned number of characters.
	        // Note: Marshal displaying to the correct UI thread.
	        myRefresh.post(new Runnable()
	            {
	                @Override
	                public void run()
	                {
	                    myResponseEditText.setText(Integer.toString(e.getResponseMessage().Length));
	                }
	            });
	    }
	    
	    private EventHandler<TypedResponseReceivedEventArgs<MyResponse>> myOnResponseHandler
	            
	         = new EventHandler<TypedResponseReceivedEventArgs<MyResponse>>()
	    {
	        @Override
	        public void onEvent(Object sender,
	                            TypedResponseReceivedEventArgs<MyResponse> e)
	        {
	            onResponseReceived(sender, e);
	        }
	    };
	    
	    private OnClickListener myOnSendRequestClickHandler = new OnClickListener()
	    {
	        @Override
	        public void onClick(View v)
	        {
	            onSendRequest(v);
	        }
	    };
	}


I used the same IP address in both projects.

Thanks! Any help will be immensely appreciated!
Posted
Comments
Sergey Alexandrovich Kryukov 8-Feb-15 4:38am    
Same IP in what projects? How? :-)
—SA
Richard MacCutchan 8-Feb-15 5:01am    
You are asking for someone to analyse your code, without knowing what the issue is. It would be better if you gathered some more information and explained exactly what you mean by "I cannot get it to work".
developerjm 9-Feb-15 8:20am    
I just cannot get it to work. I have been trying to get it to communicate but every time I run both projects, when I try to send the message through the android app, the C# program does not receive it. I have decoded the old apk and used the same code that used to work but I cannot figure out what is wrong.

Sergey by projects I mean the C# program and Android app.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900