0

I need to send some text message from my android app to my windows c# app. So far I have figured out a TCP connection using:

Socket socket = new Socket(serverAddr, Integer.parseInt(myPort));

On my client side (java), and:

IPAddress ipAd = IPAddress.Parse(ip);
myList = new TcpListener(ipAd, Int32.Parse(port));
myList.Start();

On my server side (c#). Here is all the code

The problem is that i cant afford to retrieve the servers ip on each installation... and a static ip is not an option. So i started to search a broadcast solution, but there are a lot of possible option on the web like:

MulticastSocket 
UdpClient

And other ones that i cant tell the difference between them or the best for my case, can any one help me out with this? any explanation or code would be perfect.

For the last, if i am trying to set an UDP connection, do i need an specific port? or anyone should be fine?

Community
  • 1
  • 1
Fernando Santiago
  • 2,128
  • 10
  • 44
  • 75

1 Answers1

0

It all depends on how the sender (the Android app) identifies the recipient. There has to be some logic in your requirements on how to determine the recipient.

A few common scenarios are: - one-to-one i.e. pairs of peers. In that case the sender knows the recipient (and also an IP address) - one-to-many, i.e. with a subscription-model where the sender sends the message to a "topic", which then forwards the message to a number of (registered) recipients. The "topic" knows the details of all recipients (i.e. their IP adresses) and the sender would use a pre-determined IP to send the message to the "topic", e.g. a well-know host 'in the cloud"

Unless you specify more specifics about your application scenario I'm afraid the technical level (broadcast / UDP / ports etc.) won't help.

And note: for UDP the sender will need to know the port, too, just as with TCP. The only difference is that UDP doesn't keep a connection open (stateful).

miw
  • 776
  • 4
  • 11
  • My android apps sends text to my pc app, so windows can print this text in to the printer. I am able to set any port i would like, the only thing i am struggling with is the ip, because it can change at any time and set a static ip to the pc is forbidden – Fernando Santiago Sep 06 '15 at 20:35
  • Oh, so we have a Many-to-one: all Android apps want to use the (same) PC. You'll have to assign a proper name to the PC then in your network, i.e. in your DNS. If it's your router (behind the firewallin your private network) you may be able to the configure it there. In the Internet use a service like dyndns (or a successor) to refer to your (ever-changing) public IP. – miw Sep 06 '15 at 20:43
  • I cant access the router, my clients wont let me. Thats why i need a broadcasting message or find out ips server or something like that – Fernando Santiago Sep 06 '15 at 21:04
  • In your Android code, use the PCs name instead of the IP address InetAddress serverAddr = InetAddress.getByName("Alex's PC"); The name was assigned when Windows was configured. The local DNS server will translate this into the current IP address. – miw Sep 06 '15 at 21:09
  • java.net.ConnectException: failed to connect to /192.168.1.2 (port 4445): connect failed: EHOSTUNREACH (No route to host) – Fernando Santiago Sep 06 '15 at 21:15
  • Check your server, see e.g. here: http://stackoverflow.com/questions/28167054/java-net-connectexception-failed-to-connect-to-192-168-253-3-port-2468-conn – miw Sep 06 '15 at 21:27