简体   繁体   English

在同一WLAN中将消息从Android发送到PC

[英]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. 我需要从我的Android应用程序向Windows C#应用程序发送一些文本消息。 So far I have figured out a TCP connection using: 到目前为止,我已经找到了使用以下内容的TCP连接:

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

On my client side (java), and: 在我的客户端(java)上:

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

On my server side (c#). 在我的服务器端(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. 问题是我无法负担每次安装都检索到的服务器IP ...而静态IP则不是一种选择。 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? 最后,如果我尝试设置UDP连接,是否需要特定的端口? or anyone should be fine? 还是任何人都可以吗?

It all depends on how the sender (the Android app) identifies the recipient. 这完全取决于发件人(Android应用程序)如何识别收件人。 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. 在这种情况下,发件人可以一对多地知道收件人(以及IP地址),即具有订阅模型,其中发件人将消息发送到“主题”,然后该主题将消息转发到多个(已注册)收件人。 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" “主题”知道所有收件人的详细信息(即他们的IP地址),发送者将使用预定的IP将消息发送到“主题”,例如,“云中”的知名主机

Unless you specify more specifics about your application scenario I'm afraid the technical level (broadcast / UDP / ports etc.) won't help. 除非您为应用程序场景指定更多细节,否则恐怕技术水平(广播/ UDP /端口等)将无济于事。

And note: for UDP the sender will need to know the port, too, just as with TCP. 请注意:对于UDP,发送方也需要知道端口,就像使用TCP一样。 The only difference is that UDP doesn't keep a connection open (stateful). 唯一的区别是UDP不会使连接保持打开状态(有状态)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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