简体   繁体   中英

Send message from android to pc in the same WLAN

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?

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 ie pairs of peers. In that case the sender knows the recipient (and also an IP address) - one-to-many, ie 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 (ie their IP adresses) and the sender would use a pre-determined IP to send the message to the "topic", eg 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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM